All files / src default-azure-credential.ts

100% Statements 16/16
100% Branches 11/11
100% Functions 4/4
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                                              24x           24x 2x   23x             36x 36x 36x 36x 36x         36x 36x 26x     31x         26x     36x                   4x      
import { acquireDefaultAzureCredentialToken, defaultChainTenant } from './default-credential'
import type { DefaultAzureCredentialOptions } from './default-credential'
import { resolveRequiredScopes, storageOAuthScope } from './internal/request-core'
import { cacheToken, formatAuthorizationHeader } from './internal/token'
import type { CachedTokenRequest } from './internal/token'
import {
  challengeClaims,
  requireTenantId,
  selectTenant,
  tokenCacheKey,
} from './internal/token-options'
import { requireHttpsUrl } from './internal/url'
import type { AccessToken, GetTokenOptions } from './types'
 
export type { DefaultAzureCredentialOptions } from './default-credential'
 
interface ClaimsChallenge {
  claims: string
  tenantId: string | undefined
}
 
export class DefaultAzureCredential {
  private readonly options: DefaultAzureCredentialOptions
  private readonly tokens = new Map<
    string,
    (request: CachedTokenRequest<ClaimsChallenge>) => Promise<AccessToken>
  >()
 
  public constructor(options: DefaultAzureCredentialOptions = {}) {
    if (options.authorityHost !== undefined) {
      requireHttpsUrl(options.authorityHost, 'authorityHost')
    }
    this.options = options
  }
 
  public async getToken(
    scopes: string | string[],
    options: GetTokenOptions = {},
  ): Promise<AccessToken> {
    const scope = resolveRequiredScopes(scopes)
    const tenantId = options.tenantId === undefined ? undefined : requireTenantId(options.tenantId)
    const { enableCae } = options
    const claims = challengeClaims(options.claims)
    const key = tokenCacheKey(
      scope,
      selectTenant(defaultChainTenant(this.options), tenantId),
      enableCae,
    )
    let getToken = this.tokens.get(key)
    if (getToken === undefined) {
      getToken = cacheToken<ClaimsChallenge>(
        // Calls share this loader across equivalent tenants; a challenge brings its own call's tenant.
        async (signal, challenge) =>
          await acquireDefaultAzureCredentialToken(
            { ...this.options, scope },
            { tenantId, ...challenge, enableCae, signal },
          ),
      )
      this.tokens.set(key, getToken)
    }
 
    return await getToken({
      abortSignal: options.abortSignal,
      challenge: claims === undefined ? undefined : { claims, tenantId },
    })
  }
 
  public async getAuthorizationHeader(
    scopes: string | string[] = storageOAuthScope,
    options?: GetTokenOptions,
  ): Promise<string> {
    return formatAuthorizationHeader(await this.getToken(scopes, options))
  }
}