UNPKG

@azure/identity

Version:

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

168 lines (167 loc) • 8.63 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 environmentCredential_exports = {}; __export(environmentCredential_exports, { AllSupportedEnvironmentVariables: () => AllSupportedEnvironmentVariables, EnvironmentCredential: () => EnvironmentCredential, getSendCertificateChain: () => getSendCertificateChain }); module.exports = __toCommonJS(environmentCredential_exports); var import_errors = require("../errors.js"); var import_logging = require("../util/logging.js"); var import_clientCertificateCredential = require("./clientCertificateCredential.js"); var import_clientSecretCredential = require("./clientSecretCredential.js"); var import_usernamePasswordCredential = require("./usernamePasswordCredential.js"); var import_tenantIdUtils = require("../util/tenantIdUtils.js"); var import_tracing = require("../util/tracing.js"); const AllSupportedEnvironmentVariables = [ "AZURE_TENANT_ID", "AZURE_CLIENT_ID", "AZURE_CLIENT_SECRET", "AZURE_CLIENT_CERTIFICATE_PATH", "AZURE_CLIENT_CERTIFICATE_PASSWORD", "AZURE_USERNAME", "AZURE_PASSWORD", "AZURE_ADDITIONALLY_ALLOWED_TENANTS", "AZURE_CLIENT_SEND_CERTIFICATE_CHAIN" ]; function getAdditionallyAllowedTenants() { const additionallyAllowedValues = process.env.AZURE_ADDITIONALLY_ALLOWED_TENANTS ?? ""; return additionallyAllowedValues.split(";"); } const credentialName = "EnvironmentCredential"; const logger = (0, import_logging.credentialLogger)(credentialName); function getSendCertificateChain() { const sendCertificateChain = (process.env.AZURE_CLIENT_SEND_CERTIFICATE_CHAIN ?? "").toLowerCase(); const result = sendCertificateChain === "true" || sendCertificateChain === "1"; logger.verbose( `AZURE_CLIENT_SEND_CERTIFICATE_CHAIN: ${process.env.AZURE_CLIENT_SEND_CERTIFICATE_CHAIN}; sendCertificateChain: ${result}` ); return result; } class EnvironmentCredential { _credential = void 0; /** * Creates an instance of the EnvironmentCredential class and decides what credential to use depending on the available environment variables. * * Required environment variables: * - `AZURE_TENANT_ID`: The Microsoft Entra tenant (directory) ID. * - `AZURE_CLIENT_ID`: The client (application) ID of an App Registration in the tenant. * * If setting the AZURE_TENANT_ID, then you can also set the additionally allowed tenants * - `AZURE_ADDITIONALLY_ALLOWED_TENANTS`: For multi-tenant applications, specifies additional tenants for which the credential may acquire tokens with a single semicolon delimited string. Use * to allow all tenants. * * Environment variables used for client credential authentication: * - `AZURE_CLIENT_SECRET`: A client secret that was generated for the App Registration. * - `AZURE_CLIENT_CERTIFICATE_PATH`: The path to a PEM certificate to use during the authentication, instead of the client secret. * - `AZURE_CLIENT_CERTIFICATE_PASSWORD`: (optional) password for the certificate file. * - `AZURE_CLIENT_SEND_CERTIFICATE_CHAIN`: (optional) indicates that the certificate chain should be set in x5c header to support subject name / issuer based authentication. * * Username and password authentication is deprecated, since it doesn't support multifactor authentication (MFA). See https://aka.ms/azsdk/identity/mfa for more details. Users can still provide environment variables for this authentication method: * - `AZURE_USERNAME`: Username to authenticate with. * - `AZURE_PASSWORD`: Password to authenticate with. * * If the environment variables required to perform the authentication are missing, a {@link CredentialUnavailableError} will be thrown. * If the authentication fails, or if there's an unknown error, an {@link AuthenticationError} will be thrown. * * @param options - Options for configuring the client which makes the authentication request. */ constructor(options) { const assigned = (0, import_logging.processEnvVars)(AllSupportedEnvironmentVariables).assigned.join(", "); logger.info(`Found the following environment variables: ${assigned}`); const tenantId = process.env.AZURE_TENANT_ID, clientId = process.env.AZURE_CLIENT_ID, clientSecret = process.env.AZURE_CLIENT_SECRET; const additionallyAllowedTenantIds = getAdditionallyAllowedTenants(); const sendCertificateChain = getSendCertificateChain(); const newOptions = { ...options, additionallyAllowedTenantIds, sendCertificateChain }; if (tenantId) { (0, import_tenantIdUtils.checkTenantId)(logger, tenantId); } if (tenantId && clientId && clientSecret) { logger.info( `Invoking ClientSecretCredential with tenant ID: ${tenantId}, clientId: ${clientId} and clientSecret: [REDACTED]` ); this._credential = new import_clientSecretCredential.ClientSecretCredential(tenantId, clientId, clientSecret, newOptions); return; } const certificatePath = process.env.AZURE_CLIENT_CERTIFICATE_PATH; const certificatePassword = process.env.AZURE_CLIENT_CERTIFICATE_PASSWORD; if (tenantId && clientId && certificatePath) { logger.info( `Invoking ClientCertificateCredential with tenant ID: ${tenantId}, clientId: ${clientId} and certificatePath: ${certificatePath}` ); this._credential = new import_clientCertificateCredential.ClientCertificateCredential( tenantId, clientId, { certificatePath, certificatePassword }, newOptions ); return; } const username = process.env.AZURE_USERNAME; const password = process.env.AZURE_PASSWORD; if (tenantId && clientId && username && password) { logger.info( `Invoking UsernamePasswordCredential with tenant ID: ${tenantId}, clientId: ${clientId} and username: ${username}` ); logger.warning( "Environment is configured to use username and password authentication. This authentication method is deprecated, as it doesn't support multifactor authentication (MFA). Use a more secure credential. For more details, see https://aka.ms/azsdk/identity/mfa." ); this._credential = new import_usernamePasswordCredential.UsernamePasswordCredential( tenantId, clientId, username, password, newOptions ); } } /** * Authenticates with Microsoft Entra ID and returns an access token if successful. * * @param scopes - The list of scopes for which the token will have access. * @param options - Optional parameters. See {@link GetTokenOptions}. */ async getToken(scopes, options = {}) { return import_tracing.tracingClient.withSpan(`${credentialName}.getToken`, options, async (newOptions) => { if (this._credential) { try { const result = await this._credential.getToken(scopes, newOptions); logger.getToken.info((0, import_logging.formatSuccess)(scopes)); return result; } catch (err) { const authenticationError = new import_errors.AuthenticationError(400, { error: `${credentialName} authentication failed. To troubleshoot, visit https://aka.ms/azsdk/js/identity/environmentcredential/troubleshoot.`, error_description: err.message.toString().split("More details:").join("") }); logger.getToken.info((0, import_logging.formatError)(scopes, authenticationError)); throw authenticationError; } } throw new import_errors.CredentialUnavailableError( `${credentialName} is unavailable. No underlying credential could be used. To troubleshoot, visit https://aka.ms/azsdk/js/identity/environmentcredential/troubleshoot.` ); }); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AllSupportedEnvironmentVariables, EnvironmentCredential, getSendCertificateChain }); //# sourceMappingURL=environmentCredential.js.map