UNPKG

@azure/identity

Version:

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

107 lines (106 loc) 4.29 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 clientAssertionCredential_exports = {}; __export(clientAssertionCredential_exports, { ClientAssertionCredential: () => ClientAssertionCredential }); module.exports = __toCommonJS(clientAssertionCredential_exports); var import_msalClient = require("../msal/nodeFlows/msalClient.js"); var import_tenantIdUtils = require("../util/tenantIdUtils.js"); 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)("ClientAssertionCredential"); class ClientAssertionCredential { msalClient; tenantId; additionallyAllowedTenantIds; getAssertion; options; /** * Creates an instance of the ClientAssertionCredential with the details * needed to authenticate against Microsoft Entra ID with a client * assertion provided by the developer through the `getAssertion` function parameter. * * @param tenantId - The Microsoft Entra tenant (directory) ID. * @param clientId - The client (application) ID of an App Registration in the tenant. * @param getAssertion - A function that retrieves the assertion for the credential to use. * @param options - Options for configuring the client which makes the authentication request. */ constructor(tenantId, clientId, getAssertion, options = {}) { if (!tenantId) { throw new import_errors.CredentialUnavailableError( "ClientAssertionCredential: tenantId is a required parameter." ); } if (!clientId) { throw new import_errors.CredentialUnavailableError( "ClientAssertionCredential: clientId is a required parameter." ); } if (!getAssertion) { throw new import_errors.CredentialUnavailableError( "ClientAssertionCredential: clientAssertion is a required parameter." ); } this.tenantId = tenantId; this.additionallyAllowedTenantIds = (0, import_tenantIdUtils.resolveAdditionallyAllowedTenantIds)( options?.additionallyAllowedTenants ); this.options = options; this.getAssertion = getAssertion; this.msalClient = (0, import_msalClient.createMsalClient)(clientId, tenantId, { ...options, logger, tokenCredentialOptions: this.options }); } /** * Authenticates with Microsoft Entra ID and returns an access token if successful. * If authentication fails, a {@link CredentialUnavailableError} will be thrown with the details of the failure. * * @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 = {}) { return import_tracing.tracingClient.withSpan( `${this.constructor.name}.getToken`, options, async (newOptions) => { newOptions.tenantId = (0, import_tenantIdUtils.processMultiTenantRequest)( this.tenantId, newOptions, this.additionallyAllowedTenantIds, logger ); const arrayScopes = Array.isArray(scopes) ? scopes : [scopes]; return this.msalClient.getTokenByClientAssertion( arrayScopes, this.getAssertion, newOptions ); } ); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ClientAssertionCredential }); //# sourceMappingURL=clientAssertionCredential.js.map