All files / src client-secret-credential.ts

100% Statements 13/13
85.71% Branches 6/7
100% Functions 3/3
100% Lines 13/13

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                                            18x                     18x 18x             39x   39x   39x 39x 39x 39x 18x   26x                 18x     37x            
import { resolveRequiredScopes } from './internal/request-core'
import type { RetryOptions } from './internal/request-core'
import { cacheToken } from './internal/token'
import type { CachedTokenRequest } from './internal/token'
import { challengeClaims, resolveTenant, tokenCacheKey } from './internal/token-options'
import { getServicePrincipalToken } from './service-principal'
import type { ServicePrincipalOptions } from './service-principal'
import type { AccessToken, GetTokenOptions } from './types'
 
export interface ClientSecretCredentialOptions {
  authorityHost?: string
  /** Tenants besides `tenantId` that `getToken` may request; `*` allows any tenant. */
  additionallyAllowedTenants?: string[]
  retryOptions?: RetryOptions
  fetch?: typeof globalThis.fetch
}
 
/** Class form of `getServicePrincipalToken` with per-scope token caching. */
export class ClientSecretCredential {
  // ES private, so the secret stays out of logs and serialization.
  readonly #options: Omit<ServicePrincipalOptions, 'scope'>
  readonly #additionallyAllowedTenants: string[]
  readonly #tokens = new Map<
    string,
    (request: CachedTokenRequest<string>) => Promise<AccessToken>
  >()
 
  public constructor(
    tenantId: string,
    clientId: string,
    clientSecret: string,
    { additionallyAllowedTenants = [], ...options }: ClientSecretCredentialOptions = {},
  ) {
    this.#options = { ...options, tenantId, clientId, clientSecret }
    this.#additionallyAllowedTenants = additionallyAllowedTenants
  }
 
  public async getToken(
    scopes: string | string[],
    options: GetTokenOptions = {},
  ): Promise<AccessToken> {
    const scope = resolveRequiredScopes(scopes)
    const tenantId =
      resolveTenant(this.#options.tenantId, options.tenantId, this.#additionallyAllowedTenants) ??
      this.#options.tenantId
    const { enableCae } = options
    const key = tokenCacheKey(scope, tenantId, enableCae)
    let getToken = this.#tokens.get(key)
    if (getToken === undefined) {
      getToken = cacheToken<string>(
        async (abortSignal, claims) =>
          await getServicePrincipalToken({
            ...this.#options,
            tenantId,
            scope,
            abortSignal,
            claims,
            enableCae,
          }),
      )
      this.#tokens.set(key, getToken)
    }
 
    return await getToken({
      abortSignal: options.abortSignal,
      challenge: challengeClaims(options.claims),
    })
  }
}