Path: blob/main/extensions/microsoft-authentication/src/extension.ts
5237 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Environment, EnvironmentParameters } from '@azure/ms-rest-azure-env';6import Logger from './logger';7import { MsalAuthProvider } from './node/authProvider';8import { UriEventHandler } from './UriEventHandler';9import { authentication, commands, ExtensionContext, l10n, window, workspace, Disposable, Uri } from 'vscode';10import { MicrosoftAuthenticationTelemetryReporter, MicrosoftSovereignCloudAuthenticationTelemetryReporter } from './common/telemetryReporter';1112let implementation: 'msal' | 'msal-no-broker' = 'msal';13const getImplementation = () => workspace.getConfiguration('microsoft-authentication').get<'msal' | 'msal-no-broker'>('implementation') ?? 'msal';1415async function initMicrosoftSovereignCloudAuthProvider(16context: ExtensionContext,17uriHandler: UriEventHandler18): Promise<Disposable | undefined> {19const environment = workspace.getConfiguration('microsoft-sovereign-cloud').get<string | undefined>('environment');20let authProviderName: string | undefined;21if (!environment) {22return undefined;23}2425if (environment === 'custom') {26const customEnv = workspace.getConfiguration('microsoft-sovereign-cloud').get<EnvironmentParameters>('customEnvironment');27if (!customEnv) {28const res = await window.showErrorMessage(l10n.t('You must also specify a custom environment in order to use the custom environment auth provider.'), l10n.t('Open settings'));29if (res) {30await commands.executeCommand('workbench.action.openSettingsJson', 'microsoft-sovereign-cloud.customEnvironment');31}32return undefined;33}34try {35Environment.add(customEnv);36} catch (e) {37const res = await window.showErrorMessage(l10n.t('Error validating custom environment setting: {0}', e.message), l10n.t('Open settings'));38if (res) {39await commands.executeCommand('workbench.action.openSettings', 'microsoft-sovereign-cloud.customEnvironment');40}41return undefined;42}43authProviderName = customEnv.name;44} else {45authProviderName = environment;46}4748const env = Environment.get(authProviderName);49if (!env) {50await window.showErrorMessage(l10n.t('The environment `{0}` is not a valid environment.', authProviderName), l10n.t('Open settings'));51return undefined;52}5354const authProvider = await MsalAuthProvider.create(55context,56new MicrosoftSovereignCloudAuthenticationTelemetryReporter(context.extension.packageJSON.aiKey),57window.createOutputChannel(l10n.t('Microsoft Sovereign Cloud Authentication'), { log: true }),58uriHandler,59env60);61const disposable = authentication.registerAuthenticationProvider(62'microsoft-sovereign-cloud',63authProviderName,64authProvider,65{ supportsMultipleAccounts: true, supportsChallenges: true }66);67context.subscriptions.push(disposable);68return disposable;69}7071export async function activate(context: ExtensionContext) {72const mainTelemetryReporter = new MicrosoftAuthenticationTelemetryReporter(context.extension.packageJSON.aiKey);73implementation = getImplementation();74context.subscriptions.push(workspace.onDidChangeConfiguration(async e => {75if (!e.affectsConfiguration('microsoft-authentication')) {76return;77}78if (implementation === getImplementation()) {79return;80}8182// Allow for the migration to be re-attempted if the user switches back to the MSAL implementation83context.globalState.update('msalMigration', undefined);8485const reload = l10n.t('Reload');86const result = await window.showInformationMessage(87'Reload required',88{89modal: true,90detail: l10n.t('Microsoft Account configuration has been changed.'),91},92reload93);9495if (result === reload) {96commands.executeCommand('workbench.action.reloadWindow');97}98}));99100switch (implementation) {101case 'msal-no-broker':102mainTelemetryReporter.sendActivatedWithMsalNoBrokerEvent();103break;104case 'msal':105default:106break;107}108109const uriHandler = new UriEventHandler();110context.subscriptions.push(uriHandler);111const authProvider = await MsalAuthProvider.create(112context,113mainTelemetryReporter,114Logger,115uriHandler116);117context.subscriptions.push(authentication.registerAuthenticationProvider(118'microsoft',119'Microsoft',120authProvider,121{122supportsMultipleAccounts: true,123supportsChallenges: true,124supportedAuthorizationServers: [125Uri.parse('https://login.microsoftonline.com/*'),126Uri.parse('https://login.microsoftonline.com/*/v2.0')127]128}129));130131let microsoftSovereignCloudAuthProviderDisposable = await initMicrosoftSovereignCloudAuthProvider(context, uriHandler);132133context.subscriptions.push(workspace.onDidChangeConfiguration(async e => {134if (e.affectsConfiguration('microsoft-sovereign-cloud')) {135microsoftSovereignCloudAuthProviderDisposable?.dispose();136microsoftSovereignCloudAuthProviderDisposable = await initMicrosoftSovereignCloudAuthProvider(context, uriHandler);137}138}));139}140141export function deactivate() {142Logger.info('Microsoft Authentication is deactivating...');143}144145146