UNPKG

@azure/identity

Version:

Provides credential implementations for Azure SDK libraries that can authenticate with Microsoft Entra ID

117 lines (116 loc) 4.71 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var chainedTokenCredential_exports = {}; __export(chainedTokenCredential_exports, { ChainedTokenCredential: () => ChainedTokenCredential, logger: () => logger }); module.exports = __toCommonJS(chainedTokenCredential_exports); var import_errors = require("../errors.js"); var import_logging = require("../util/logging.js"); var import_tracing = require("../util/tracing.js"); const logger = (0, import_logging.credentialLogger)("ChainedTokenCredential"); class ChainedTokenCredential { _sources = []; /** * Creates an instance of ChainedTokenCredential using the given credentials. * * @param sources - `TokenCredential` implementations to be tried in order. * * Example usage: * ```ts snippet:chained_token_credential_example * import { ClientSecretCredential, ChainedTokenCredential } from "@azure/identity"; * * const tenantId = "<tenant-id>"; * const clientId = "<client-id>"; * const clientSecret = "<client-secret>"; * const anotherClientId = "<another-client-id>"; * const anotherSecret = "<another-client-secret>"; * * const firstCredential = new ClientSecretCredential(tenantId, clientId, clientSecret); * const secondCredential = new ClientSecretCredential(tenantId, anotherClientId, anotherSecret); * * const credentialChain = new ChainedTokenCredential(firstCredential, secondCredential); * ``` */ constructor(...sources) { this._sources = sources; } /** * Returns the first access token returned by one of the chained * `TokenCredential` implementations. Throws an {@link AggregateAuthenticationError} * when one or more credentials throws an {@link AuthenticationError} and * no credentials have returned an access token. * * This method is called automatically by Azure SDK client libraries. You may call this method * directly, but you must also handle token caching and token refreshing. * * @param scopes - The list of scopes for which the token will have access. * @param options - The options used to configure any requests this * `TokenCredential` implementation might make. */ async getToken(scopes, options = {}) { const { token } = await this.getTokenInternal(scopes, options); return token; } async getTokenInternal(scopes, options = {}) { let token = null; let successfulCredential; const errors = []; return import_tracing.tracingClient.withSpan( "ChainedTokenCredential.getToken", options, async (updatedOptions) => { for (let i = 0; i < this._sources.length && token === null; i++) { try { token = await this._sources[i].getToken(scopes, updatedOptions); successfulCredential = this._sources[i]; } catch (err) { if (err.name === "CredentialUnavailableError" || err.name === "AuthenticationRequiredError") { errors.push(err); } else { logger.getToken.info((0, import_logging.formatError)(scopes, err)); throw err; } } } if (!token && errors.length > 0) { const err = new import_errors.AggregateAuthenticationError( errors, "ChainedTokenCredential authentication failed." ); logger.getToken.info((0, import_logging.formatError)(scopes, err)); throw err; } logger.getToken.info( `Result for ${successfulCredential.constructor.name}: ${(0, import_logging.formatSuccess)(scopes)}` ); if (token === null) { throw new import_errors.CredentialUnavailableError("Failed to retrieve a valid token"); } return { token, successfulCredential }; } ); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ChainedTokenCredential, logger }); //# sourceMappingURL=chainedTokenCredential.js.map