UNPKG

@azure/identity

Version:

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

102 lines (101 loc) 4.44 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 clientSecretCredential_exports = {}; __export(clientSecretCredential_exports, { ClientSecretCredential: () => ClientSecretCredential }); module.exports = __toCommonJS(clientSecretCredential_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_scopeUtils = require("../util/scopeUtils.js"); var import_tracing = require("../util/tracing.js"); const logger = (0, import_logging.credentialLogger)("ClientSecretCredential"); class ClientSecretCredential { tenantId; additionallyAllowedTenantIds; msalClient; clientSecret; /** * Creates an instance of the ClientSecretCredential with the details * needed to authenticate against Microsoft Entra ID with a client * secret. * * @param tenantId - The Microsoft Entra tenant (directory) ID. * @param clientId - The client (application) ID of an App Registration in the tenant. * @param clientSecret - A client secret that was generated for the App Registration. * @param options - Options for configuring the client which makes the authentication request. */ constructor(tenantId, clientId, clientSecret, options = {}) { if (!tenantId) { throw new import_errors.CredentialUnavailableError( "ClientSecretCredential: tenantId is a required parameter. To troubleshoot, visit https://aka.ms/azsdk/js/identity/serviceprincipalauthentication/troubleshoot." ); } if (!clientId) { throw new import_errors.CredentialUnavailableError( "ClientSecretCredential: clientId is a required parameter. To troubleshoot, visit https://aka.ms/azsdk/js/identity/serviceprincipalauthentication/troubleshoot." ); } if (!clientSecret) { throw new import_errors.CredentialUnavailableError( "ClientSecretCredential: clientSecret is a required parameter. To troubleshoot, visit https://aka.ms/azsdk/js/identity/serviceprincipalauthentication/troubleshoot." ); } this.clientSecret = clientSecret; this.tenantId = tenantId; this.additionallyAllowedTenantIds = (0, import_tenantIdUtils.resolveAdditionallyAllowedTenantIds)( options?.additionallyAllowedTenants ); this.msalClient = (0, import_msalClient.createMsalClient)(clientId, tenantId, { ...options, logger, tokenCredentialOptions: 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 = (0, import_scopeUtils.ensureScopes)(scopes); return this.msalClient.getTokenByClientSecret(arrayScopes, this.clientSecret, newOptions); } ); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ClientSecretCredential }); //# sourceMappingURL=clientSecretCredential.js.map