UNPKG

@azure/identity

Version:

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

204 lines (203 loc) • 7.25 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 utils_exports = {}; __export(utils_exports, { defaultLoggerCallback: () => defaultLoggerCallback, deserializeAuthenticationRecord: () => deserializeAuthenticationRecord, ensureValidMsalToken: () => ensureValidMsalToken, getAuthority: () => getAuthority, getAuthorityHost: () => getAuthorityHost, getKnownAuthorities: () => getKnownAuthorities, getMSALLogLevel: () => getMSALLogLevel, handleMsalError: () => handleMsalError, msalToPublic: () => msalToPublic, publicToMsal: () => publicToMsal, randomUUID: () => randomUUID, serializeAuthenticationRecord: () => serializeAuthenticationRecord }); module.exports = __toCommonJS(utils_exports); var import_errors = require("../errors.js"); var import_logging = require("../util/logging.js"); var import_constants = require("../constants.js"); var import_core_util = require("@azure/core-util"); var import_abort_controller = require("@azure/abort-controller"); var import_msal = require("./msal.js"); const logger = (0, import_logging.credentialLogger)("IdentityUtils"); const LatestAuthenticationRecordVersion = "1.0"; function ensureValidMsalToken(scopes, msalToken, getTokenOptions) { const error = (message) => { logger.getToken.info(message); return new import_errors.AuthenticationRequiredError({ scopes: Array.isArray(scopes) ? scopes : [scopes], getTokenOptions, message }); }; if (!msalToken) { throw error("No response"); } if (!msalToken.expiresOn) { throw error(`Response had no "expiresOn" property.`); } if (!msalToken.accessToken) { throw error(`Response had no "accessToken" property.`); } } function getAuthorityHost(options) { let authorityHost = options?.authorityHost; if (!authorityHost && import_core_util.isNodeLike) { authorityHost = process.env.AZURE_AUTHORITY_HOST; } return authorityHost ?? import_constants.DefaultAuthorityHost; } function getAuthority(tenantId, host) { if (!host) { host = import_constants.DefaultAuthorityHost; } if (new RegExp(`${tenantId}/?$`).test(host)) { return host; } if (host.endsWith("/")) { return host + tenantId; } else { return `${host}/${tenantId}`; } } function getKnownAuthorities(tenantId, authorityHost, disableInstanceDiscovery) { if (tenantId === "adfs" && authorityHost || disableInstanceDiscovery) { return [authorityHost]; } return []; } const defaultLoggerCallback = (credLogger, platform = import_core_util.isNode ? "Node" : "Browser") => (level, message, containsPii) => { if (containsPii) { return; } switch (level) { case import_msal.msalCommon.LogLevel.Error: credLogger.info(`MSAL ${platform} V2 error: ${message}`); return; case import_msal.msalCommon.LogLevel.Info: credLogger.info(`MSAL ${platform} V2 info message: ${message}`); return; case import_msal.msalCommon.LogLevel.Verbose: credLogger.info(`MSAL ${platform} V2 verbose message: ${message}`); return; case import_msal.msalCommon.LogLevel.Warning: credLogger.info(`MSAL ${platform} V2 warning: ${message}`); return; } }; function getMSALLogLevel(logLevel) { switch (logLevel) { case "error": return import_msal.msalCommon.LogLevel.Error; case "info": return import_msal.msalCommon.LogLevel.Info; case "verbose": return import_msal.msalCommon.LogLevel.Verbose; case "warning": return import_msal.msalCommon.LogLevel.Warning; default: return import_msal.msalCommon.LogLevel.Info; } } function randomUUID() { return (0, import_core_util.randomUUID)(); } function handleMsalError(scopes, error, getTokenOptions) { if (error.name === "AuthError" || error.name === "ClientAuthError" || error.name === "BrowserAuthError") { const msalError = error; switch (msalError.errorCode) { case "endpoints_resolution_error": logger.info((0, import_logging.formatError)(scopes, error.message)); return new import_errors.CredentialUnavailableError(error.message); case "device_code_polling_cancelled": return new import_abort_controller.AbortError("The authentication has been aborted by the caller."); case "consent_required": case "interaction_required": case "login_required": logger.info( (0, import_logging.formatError)(scopes, `Authentication returned errorCode ${msalError.errorCode}`) ); break; default: logger.info((0, import_logging.formatError)(scopes, `Failed to acquire token: ${error.message}`)); break; } } if (error.name === "ClientConfigurationError" || error.name === "BrowserConfigurationAuthError" || error.name === "AbortError" || error.name === "AuthenticationError") { return error; } if (error.name === "NativeAuthError") { logger.info( (0, import_logging.formatError)( scopes, `Error from the native broker: ${error.message} with status code: ${error.statusCode}` ) ); return error; } return new import_errors.AuthenticationRequiredError({ scopes, getTokenOptions, message: error.message }); } function publicToMsal(account) { return { localAccountId: account.homeAccountId, environment: account.authority, username: account.username, homeAccountId: account.homeAccountId, tenantId: account.tenantId }; } function msalToPublic(clientId, account) { const record = { authority: account.environment ?? import_constants.DefaultAuthority, homeAccountId: account.homeAccountId, tenantId: account.tenantId || import_constants.DefaultTenantId, username: account.username, clientId, version: LatestAuthenticationRecordVersion }; return record; } function serializeAuthenticationRecord(record) { return JSON.stringify(record); } function deserializeAuthenticationRecord(serializedRecord) { const parsed = JSON.parse(serializedRecord); if (parsed.version && parsed.version !== LatestAuthenticationRecordVersion) { throw Error("Unsupported AuthenticationRecord version"); } return parsed; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { defaultLoggerCallback, deserializeAuthenticationRecord, ensureValidMsalToken, getAuthority, getAuthorityHost, getKnownAuthorities, getMSALLogLevel, handleMsalError, msalToPublic, publicToMsal, randomUUID, serializeAuthenticationRecord }); //# sourceMappingURL=utils.js.map