public async getAccessToken()

in Composer/packages/electron-server/src/auth/oneAuthService.ts [116:189]


  public async getAccessToken(
    params: ElectronAuthParameters
  ): Promise<{ accessToken: string; acquiredAt: number; expiryTime: number }> {
    try {
      if (!this.initialized) {
        this.initialize();
      }
      log('Getting access token...');
      if (!params.targetResource) {
        throw 'Target resource required to get access token.';
      }

      // Temporary until we properly configure local Mac dev experience
      if (isMac() && isDevelopment) {
        log('Mac development env detected. Getting access token using interactive sign in instead of silently.');
        return this.TEMPORARY_getAccessTokenOnMacDev(params);
      }
      // if Account not exist, use arm account. in case the window popup again
      if (!this.signedInAccount) {
        this.signedInAccount = this.signedInARMAccount;
      }

      if (!this.signedInAccount) {
        // we need to sign in
        log('No signed in account found. Signing user in before getting access token.');
        await this.signIn();
      }
      if (!this.signedInAccount?.id) {
        throw 'Signed in account does not have an id.';
      }
      // use the signed in account to acquire a token
      const reqParams = new this.oneAuth.AuthParameters(
        DEFAULT_AUTH_SCHEME,
        params.authority || DEFAULT_AUTH_AUTHORITY,
        params.targetResource,
        this.signedInAccount.realm,
        ''
      );
      const result = await this.oneAuth.acquireCredentialSilently(this.signedInAccount?.id, reqParams, '');
      if (result.credential && result.credential.value) {
        log('Acquired access token. %s', result.credential.value);
        return {
          accessToken: result.credential.value,
          acquiredAt: Date.now(),
          expiryTime: result.credential.expiresOn,
        };
      }
      throw 'Could not acquire an access token.';
    } catch (e) {
      if (e.error?.status === Status.InteractionRequired && this.signedInAccount) {
        // try again but interactively
        log('Interaction required. Trying again interactively to get access token.');
        // use the signed in account to acquire a token
        const reqParams = new this.oneAuth.AuthParameters(
          DEFAULT_AUTH_SCHEME,
          params.authority || DEFAULT_AUTH_AUTHORITY,
          params.targetResource,
          this.signedInAccount.realm,
          ''
        );
        const result = await this.oneAuth.acquireCredentialInteractively(this.signedInAccount?.id, reqParams, '');
        if (result.credential && result.credential.value) {
          log('Acquired access token interactively. %s', result.credential.value);
          return {
            accessToken: result.credential.value,
            acquiredAt: Date.now(),
            expiryTime: result.credential.expiresOn,
          };
        }
      }
      log('Error while trying to get an access token: %O', e);
      throw e;
    }
  }