async getCredentials()

in src/core/swa-cli-persistence-plugin/impl/secret-storage.ts [21:51]


  async getCredentials(machineId: string, key: string): Promise<string | undefined> {
    logger.silly(`Getting credentials`);

    const fullKey = await this.getFullKey(machineId);

    let credentials = await this.credentialsService.getPassword(fullKey, key);
    logger.silly(`Credentials: ${credentials ? "<hidden>" : "<empty>"}`);

    if (this.options.unsafeAllowUnencryptedStorage === false) {
      credentials = credentials && (await this.encryptionService.decrypt(credentials));
      logger.silly(`Decrypted credentials: ${credentials ? "<hidden>" : "<empty>"}`);
    }

    if (credentials) {
      try {
        const value = JSON.parse(credentials);
        logger.silly(`Credentials content: ${value.content ? "<hidden>" : "<empty>"}`);

        if (value.machineId === machineId) {
          return value.content;
        }
      } catch (error: any) {
        logger.silly(`Error while trying to parse credentials: ${error.message}`);
        // Delete corrupted credentials or else we're stuck with them forever
        await this.deleteCredentials(machineId, key);
        throw new Error("Cannot load credentials, please try again.");
      }
    }

    return undefined;
  }