UNPKG

@azure/identity

Version:

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

118 lines (117 loc) 5.09 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 usernamePasswordCredential_exports = {}; __export(usernamePasswordCredential_exports, { UsernamePasswordCredential: () => UsernamePasswordCredential }); module.exports = __toCommonJS(usernamePasswordCredential_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)("UsernamePasswordCredential"); class UsernamePasswordCredential { tenantId; additionallyAllowedTenantIds; msalClient; username; password; /** * Creates an instance of the UsernamePasswordCredential with the details * needed to authenticate against Microsoft Entra ID with a username * and password. * * @param tenantId - The Microsoft Entra tenant (directory). * @param clientId - The client (application) ID of an App Registration in the tenant. * @param username - The user account's e-mail address (user name). * @param password - The user account's account password * @param options - Options for configuring the client which makes the authentication request. */ constructor(tenantId, clientId, username, password, options = {}) { if (!tenantId) { throw new import_errors.CredentialUnavailableError( "UsernamePasswordCredential: tenantId is a required parameter. To troubleshoot, visit https://aka.ms/azsdk/js/identity/usernamepasswordcredential/troubleshoot." ); } if (!clientId) { throw new import_errors.CredentialUnavailableError( "UsernamePasswordCredential: clientId is a required parameter. To troubleshoot, visit https://aka.ms/azsdk/js/identity/usernamepasswordcredential/troubleshoot." ); } if (!username) { throw new import_errors.CredentialUnavailableError( "UsernamePasswordCredential: username is a required parameter. To troubleshoot, visit https://aka.ms/azsdk/js/identity/usernamepasswordcredential/troubleshoot." ); } if (!password) { throw new import_errors.CredentialUnavailableError( "UsernamePasswordCredential: password is a required parameter. To troubleshoot, visit https://aka.ms/azsdk/js/identity/usernamepasswordcredential/troubleshoot." ); } this.tenantId = tenantId; this.additionallyAllowedTenantIds = (0, import_tenantIdUtils.resolveAdditionallyAllowedTenantIds)( options?.additionallyAllowedTenants ); this.username = username; this.password = password; this.msalClient = (0, import_msalClient.createMsalClient)(clientId, this.tenantId, { ...options, 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. * * If the user provided the option `disableAutomaticAuthentication`, * once the token can't be retrieved silently, * this method won't attempt to request user interaction to retrieve the token. * * @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.getTokenByUsernamePassword( arrayScopes, this.username, this.password, newOptions ); } ); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { UsernamePasswordCredential }); //# sourceMappingURL=usernamePasswordCredential.js.map