UNPKG

@azure/identity

Version:

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

158 lines (157 loc) • 6.94 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 visualStudioCodeCredential_exports = {}; __export(visualStudioCodeCredential_exports, { VisualStudioCodeCredential: () => VisualStudioCodeCredential }); module.exports = __toCommonJS(visualStudioCodeCredential_exports); var import_logging = require("../util/logging.js"); var import_tenantIdUtils = require("../util/tenantIdUtils.js"); var import_errors = require("../errors.js"); var import_tenantIdUtils2 = require("../util/tenantIdUtils.js"); var import_msalClient = require("../msal/nodeFlows/msalClient.js"); var import_scopeUtils = require("../util/scopeUtils.js"); var import_msalPlugins = require("../msal/nodeFlows/msalPlugins.js"); var import_utils = require("../msal/utils.js"); var import_promises = require("node:fs/promises"); const CommonTenantId = "common"; const VSCodeClientId = "aebc6443-996d-45c2-90f0-388ff96faa56"; const logger = (0, import_logging.credentialLogger)("VisualStudioCodeCredential"); const unsupportedTenantIds = { adfs: "The VisualStudioCodeCredential does not support authentication with ADFS tenants." }; function checkUnsupportedTenant(tenantId) { const unsupportedTenantError = unsupportedTenantIds[tenantId]; if (unsupportedTenantError) { throw new import_errors.CredentialUnavailableError(unsupportedTenantError); } } class VisualStudioCodeCredential { tenantId; additionallyAllowedTenantIds; msalClient; options; /** * Creates an instance of VisualStudioCodeCredential to use for automatically authenticating via VSCode. * * **Note**: `VisualStudioCodeCredential` is provided by a plugin package: * `@azure/identity-vscode`. If this package is not installed, then authentication using * `VisualStudioCodeCredential` will not be available. * * @param options - Options for configuring the client which makes the authentication request. */ constructor(options) { this.options = options || {}; if (options && options.tenantId) { (0, import_tenantIdUtils2.checkTenantId)(logger, options.tenantId); this.tenantId = options.tenantId; } else { this.tenantId = CommonTenantId; } this.additionallyAllowedTenantIds = (0, import_tenantIdUtils.resolveAdditionallyAllowedTenantIds)( options?.additionallyAllowedTenants ); checkUnsupportedTenant(this.tenantId); } /** * Runs preparations for any further getToken request: * - Validates that the plugin is available. * - Loads the authentication record from VSCode if available. * - Creates the MSAL client with the loaded plugin and authentication record. */ async prepare(scopes) { const tenantId = (0, import_tenantIdUtils.processMultiTenantRequest)( this.tenantId, this.options, this.additionallyAllowedTenantIds, logger ) || this.tenantId; if (!(0, import_msalPlugins.hasVSCodePlugin)() || !import_msalPlugins.vsCodeAuthRecordPath) { throw new import_errors.CredentialUnavailableError( "Visual Studio Code Authentication is not available. Ensure you have have Azure Resources Extension installed in VS Code, signed into Azure via VS Code, installed the @azure/identity-vscode package, and properly configured the extension." ); } const authenticationRecord = await this.loadAuthRecord(import_msalPlugins.vsCodeAuthRecordPath, scopes); this.msalClient = (0, import_msalClient.createMsalClient)(VSCodeClientId, tenantId, { ...this.options, isVSCodeCredential: true, brokerOptions: { enabled: true, parentWindowHandle: new Uint8Array(0), useDefaultBrokerAccount: true }, authenticationRecord }); } /** * The promise of the single preparation that will be executed at the first getToken request for an instance of this class. */ preparePromise; /** * Runs preparations for any further getToken, but only once. */ prepareOnce(scopes) { if (!this.preparePromise) { this.preparePromise = this.prepare(scopes); } return this.preparePromise; } /** * Returns the token found by searching VSCode's authentication cache or * returns null if no token could be found. * * @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) { const scopeArray = (0, import_scopeUtils.ensureScopes)(scopes); await this.prepareOnce(scopeArray); if (!this.msalClient) { throw new import_errors.CredentialUnavailableError( "Visual Studio Code Authentication failed to initialize. Ensure you have have Azure Resources Extension installed in VS Code, signed into Azure via VS Code, installed the @azure/identity-vscode package, and properly configured the extension." ); } return this.msalClient.getTokenByInteractiveRequest(scopeArray, { ...options, disableAutomaticAuthentication: true }); } /** * Loads the authentication record from the specified path. * @param authRecordPath - The path to the authentication record file. * @param scopes - The list of scopes for which the token will have access. * @returns The authentication record or undefined if loading fails. */ async loadAuthRecord(authRecordPath, scopes) { try { const authRecordContent = await (0, import_promises.readFile)(authRecordPath, { encoding: "utf8" }); return (0, import_utils.deserializeAuthenticationRecord)(authRecordContent); } catch (error) { logger.getToken.info((0, import_logging.formatError)(scopes, error)); throw new import_errors.CredentialUnavailableError( "Cannot load authentication record in Visual Studio Code. Ensure you have have Azure Resources Extension installed in VS Code, signed into Azure via VS Code, installed the @azure/identity-vscode package, and properly configured the extension." ); } } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { VisualStudioCodeCredential }); //# sourceMappingURL=visualStudioCodeCredential.js.map