Path: blob/main/extensions/copilot/src/extension/authentication/vscode-node/authentication.contribution.ts
13399 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*--------------------------------------------------------------------------------------------*/4import { commands, window } from 'vscode';5import { IAuthenticationService } from '../../../platform/authentication/common/authentication';6import { IAuthenticationChatUpgradeService } from '../../../platform/authentication/common/authenticationUpgrade';7import { IVSCodeExtensionContext } from '../../../platform/extContext/common/extensionContext';8import { ILogService } from '../../../platform/log/common/logService';9import { Event } from '../../../util/vs/base/common/event';10import { Disposable } from '../../../util/vs/base/common/lifecycle';11import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';1213/**14* The main entry point for the authentication contribution.15*/16export class AuthenticationContrib extends Disposable {17constructor(@IInstantiationService private readonly instantiationService: IInstantiationService) {18super();19this.askToUpgradeAuthPermissions();20}21private async askToUpgradeAuthPermissions() {22const authUpgradeAsk = this._register(this.instantiationService.createInstance(AuthUpgradeAsk));23await authUpgradeAsk.run();24}25}2627/**28* This contribution ensures we have a token that is good enough for making API calls for current workspace.29*/30class AuthUpgradeAsk extends Disposable {31private static readonly AUTH_UPGRADE_ASK_KEY = 'copilot.shownPermissiveTokenModal';3233constructor(34@IAuthenticationService private readonly _authenticationService: IAuthenticationService,35@ILogService private readonly _logService: ILogService,36@IVSCodeExtensionContext private readonly _extensionContext: IVSCodeExtensionContext,37@IAuthenticationChatUpgradeService private readonly _authenticationChatUpgradeService: IAuthenticationChatUpgradeService,38) {39super();40this._register(commands.registerCommand('github.copilot.chat.triggerPermissiveSignIn', async () => {41await this._authenticationChatUpgradeService.showPermissiveSessionModal(true);42}));43}4445async run() {46await this.waitForChatEnabled();47this.registerListeners();48await this.showPrompt();49}5051private async waitForChatEnabled() {52try {53await this._authenticationService.getCopilotToken();54} catch (error) {55// likely due to the user canceling the auth flow56this._logService.error(error, 'Failed to get copilot token');57}5859await Event.toPromise(60Event.filter(61this._authenticationService.onDidAuthenticationChange,62() => this._authenticationService.copilotToken !== undefined63)64);65}6667private registerListeners() {68this._register(this._authenticationService.onDidAuthenticationChange(async () => {69if (this._authenticationService.permissiveGitHubSession) {70return;71}72if (!this._authenticationService.anyGitHubSession) {73// We signed out, so we should show the prompt again74this._extensionContext.globalState.update(AuthUpgradeAsk.AUTH_UPGRADE_ASK_KEY, false);75return;76}77if (window.state.focused) {78await this.showPrompt();79} else {80// Wait for the window to get focus before trying to show the prompt81const disposable = window.onDidChangeWindowState(async (e) => {82if (e.focused) {83disposable.dispose();84await this.showPrompt();85}86});87}88}));89}9091private async showPrompt() {92if (93// Already asked in a previous session94this._extensionContext.globalState.get(AuthUpgradeAsk.AUTH_UPGRADE_ASK_KEY, false)95// Some other criteria for not showing the prompt96|| !(await this._authenticationChatUpgradeService.shouldRequestPermissiveSessionUpgrade())97) {98return;99}100if (await this._authenticationChatUpgradeService.showPermissiveSessionModal()) {101this._logService.debug('Got permissive GitHub token');102} else {103this._logService.debug('Did not get permissive GitHub token');104}105this._extensionContext.globalState.update(AuthUpgradeAsk.AUTH_UPGRADE_ASK_KEY, true);106}107}108109110