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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | 11x 11x 13x 2x 11x 152x 2x 150x 83x 83x 3x 80x 80x 5x 74x 124x 31x 73x 67x 59x 8x 67x 8x 8x 73x 3x 3x 3x 3x 3x 3x | import { TokenUnavailableError } from '../token-unavailable-error'
import { jsonProperty } from './json'
import { getEnv } from './runtime'
import { encodeBytesToBase64, encodeUtf8 } from './storage-encoding'
// The tenant also reaches URLs, the Windows shell and a PowerShell script.
const safeTenantId = /^[\w.-]+$/u
const safeDeveloperScope = /^(?!-)[\w.:/-]+$/u
export function requireDeveloperInputs(scopes: string[], tenantId: string | undefined): void {
if (!scopes.every((scope) => safeDeveloperScope.test(scope))) {
throw new TypeError(
'Developer credential scopes may only contain letters, digits and _ . : / - and cannot begin with a dash',
)
}
Iif (tenantId?.startsWith('-') === true) {
throw new TypeError('Developer credential tenantId cannot begin with a dash')
}
}
export function requireTenantId(tenantId: string): string {
if (!safeTenantId.test(tenantId) || tenantId === '.' || tenantId === '..') {
throw new TypeError('tenantId may only contain letters, digits and _ . -')
}
return tenantId
}
/**
* Picks the tenant of one token request. A per-call tenant other than the configured one must be
* allowed, unless multi-tenant authentication is disabled or the configured tenant is ADFS.
*/
export function resolveTenant(
configured: string | undefined,
requested: string | undefined,
allowed: readonly string[] = [],
): string | undefined {
const tenant = selectTenant(configured, requested)
if (tenant === undefined) {
return undefined
}
requireTenantId(tenant)
if (
configured !== undefined &&
tenant !== configured &&
!allowed.includes('*') &&
!allowed.includes(tenant)
) {
throw new TokenUnavailableError(
`The credential is not configured to acquire tokens for tenant ${tenant}. Add it to additionallyAllowedTenants, or add "*" to allow any tenant.`,
)
}
return tenant
}
/** The tenant a request uses before the allowlist check; it also keys token caches. */
export function selectTenant(
configured: string | undefined,
requested: string | undefined,
): string | undefined {
return requested === undefined ||
configured === 'adfs' ||
getEnv('AZURE_IDENTITY_DISABLE_MULTITENANTAUTH') !== undefined
? configured
: requested
}
/** Tenants the environment credential may serve besides AZURE_TENANT_ID, separated by semicolons. */
export function environmentAllowedTenants(): string[] {
return getEnv('AZURE_ADDITIONALLY_ALLOWED_TENANTS')?.split(';') ?? []
}
/** A blank claims challenge is no challenge. */
export function challengeClaims(claims: string | undefined): string | undefined {
return claims?.trim() === '' ? undefined : claims
}
/** The `claims` parameter of a token request; CAE adds the `cp1` client capability. */
export function tokenRequestClaims(
claims: string | undefined,
enableCae: boolean | undefined,
): string | undefined {
if (enableCae !== true) {
return claims
}
const challenge: unknown = claims === undefined ? {} : JSON.parse(claims)
Iif (typeof challenge !== 'object' || challenge === null || Array.isArray(challenge)) {
throw new TypeError('claims must be a JSON object')
}
const accessToken = jsonProperty(challenge, 'access_token')
return JSON.stringify({
...challenge,
access_token: {
...(typeof accessToken === 'object' && accessToken !== null ? accessToken : {}),
xms_cc: { values: ['cp1'] },
},
})
}
export function tokenCacheKey(
scopes: string[],
tenantId: string | undefined,
enableCae: boolean | undefined,
): string {
return JSON.stringify([scopes, tenantId ?? null, enableCae === true])
}
/** Developer credentials can't answer a claims challenge; the user has to sign in again. */
export function claimsLoginInstructions(
claims: string,
scopes: string[],
tenantId: string | undefined,
): string {
Iif (
!scopes.every((scope) => safeDeveloperScope.test(scope)) ||
(tenantId !== undefined && (!safeTenantId.test(tenantId) || tenantId.startsWith('-')))
) {
return 'Azure CLI and Azure PowerShell require signing in again with the claims challenge.'
}
const encoded = encodeBytesToBase64(encodeUtf8(claims))
const azureCli = `az login --claims-challenge ${encoded} --scope ${scopes.join(' ')}${tenantId === undefined ? '' : ` --tenant ${tenantId}`}`
const powerShell = `Connect-AzAccount -ClaimsChallenge ${encoded}${tenantId === undefined ? '' : ` -Tenant ${tenantId}`}`
return `Azure CLI and Azure PowerShell don't support claims challenges. To authenticate with the required claims, run ${azureCli} or ${powerShell}`
}
|