UNPKG

@azure/identity-vscode

Version:

Use the Azure Account extension for Visual Studio Code to authenticate with Azure Identity

65 lines 2.67 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); exports.vsCodePlugin = void 0; const tslib_1 = require("tslib"); const msal_node_extensions_1 = require("@azure/msal-node-extensions"); const node_fs_1 = require("node:fs"); const node_os_1 = tslib_1.__importDefault(require("node:os")); const node_path_1 = tslib_1.__importDefault(require("node:path")); /** * Finds the path to the VS Code authentication record file. * @returns The path to the authentication record file, or undefined if not found. */ function findAuthRecordPath() { const homedir = node_os_1.default.homedir(); const azureDirs = [".azure", ".Azure"]; for (const azureDir of azureDirs) { const authPath = node_path_1.default.join(homedir, azureDir, "ms-azuretools.vscode-azureresourcegroups", "authRecord.json"); if ((0, node_fs_1.existsSync)(authPath)) { return authPath; } } return undefined; } /** * A plugin that provides the dependencies of `VisualStudioCodeCredential` * and enables it within `@azure/identity`. The plugin API is compatible with * `@azure/identity` versions 4.11.0 and later. Load this plugin using the * `useIdentityPlugin` function, imported from `@azure/identity`. * * To use this functionality, import `VisualStudioCodeCredential` or * `DefaultAzureCredential` from `@azure/identity`. If this plugin is not * enabled, then `VisualStudioCodeCredential` will throw a * `CredentialUnavailableError`, and `DefaultAzureCredential` will not be able * to use authentication through Visual Studio Code. * * Example: * * ```ts snippet:ReadmeSampleVisualStudioCodeCredential * import { useIdentityPlugin, VisualStudioCodeCredential } from "@azure/identity"; * import { vsCodePlugin } from "@azure/identity-vscode"; * * useIdentityPlugin(vsCodePlugin); * * const credential = new VisualStudioCodeCredential(); * * // The graph.microsoft.com scope is used as an example * const scope = "https://graph.microsoft.com/.default"; * * // Print out part of the access token * console.log((await credential.getToken(scope)).token.substr(0, 10), "..."); * ``` */ const vsCodePlugin = (context) => { const { vsCodeCredentialControl } = context; const authRecordPath = findAuthRecordPath(); if (authRecordPath) { vsCodeCredentialControl.setVSCodeAuthRecordPath(authRecordPath); } const brokerPlugin = new msal_node_extensions_1.NativeBrokerPlugin(); vsCodeCredentialControl.setVSCodeBroker(brokerPlugin); }; exports.vsCodePlugin = vsCodePlugin; //# sourceMappingURL=index.js.map