Path: blob/main/extensions/copilot/src/platform/authentication/common/staticGitHubAuthenticationService.ts
13401 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 type { AuthenticationGetSessionOptions, AuthenticationSession } from 'vscode';6import { IConfigurationService } from '../../configuration/common/configurationService';7import { ILogService } from '../../log/common/logService';8import { BaseAuthenticationService, GITHUB_SCOPE_ALIGNED, GITHUB_SCOPE_USER_EMAIL, IAuthenticationService, MinimalModeError, StrictAuthenticationPresentationOptions } from './authentication';9import { CopilotToken } from './copilotToken';10import { ICopilotTokenManager } from './copilotTokenManager';11import { ICopilotTokenStore } from './copilotTokenStore';1213export class StaticGitHubAuthenticationService extends BaseAuthenticationService {14constructor(15private readonly tokenProvider: { (): string } | undefined,16@ILogService logService: ILogService,17@ICopilotTokenStore tokenStore: ICopilotTokenStore,18@ICopilotTokenManager tokenManager: ICopilotTokenManager,19@IConfigurationService configurationService: IConfigurationService20) {21super(logService, tokenStore, tokenManager, configurationService);2223const that = this;24this._anyGitHubSession = tokenProvider ? {25get id() { return that.tokenProvider!(); },26get accessToken() { return that.tokenProvider!(); },27scopes: GITHUB_SCOPE_USER_EMAIL,28account: {29id: 'user',30label: 'User'31}32} : undefined;3334this._permissiveGitHubSession = tokenProvider ? {35get id() { return that.tokenProvider!(); },36get accessToken() { return that.tokenProvider!(); },37scopes: GITHUB_SCOPE_ALIGNED,38account: {39id: 'user',40label: 'User'41}42} : undefined;43}4445override async getGitHubSession(kind: 'permissive' | 'any', options: AuthenticationGetSessionOptions & { createIfNone: StrictAuthenticationPresentationOptions }): Promise<AuthenticationSession>;46override async getGitHubSession(kind: 'permissive' | 'any', options: AuthenticationGetSessionOptions & { forceNewSession: StrictAuthenticationPresentationOptions }): Promise<AuthenticationSession>;47override async getGitHubSession(kind: 'permissive' | 'any', options: AuthenticationGetSessionOptions): Promise<AuthenticationSession | undefined>;48override async getGitHubSession(kind: 'permissive' | 'any', options: AuthenticationGetSessionOptions): Promise<AuthenticationSession | undefined> {49if (kind === 'permissive') {50if (this.isMinimalMode) {51if (options.createIfNone || options.forceNewSession) {52throw new MinimalModeError();53}54return undefined;55}56return this._permissiveGitHubSession;57} else {58return this._anyGitHubSession;59}60}6162override async getCopilotToken(force?: boolean): Promise<CopilotToken> {63return await super.getCopilotToken(force);64}6566setCopilotToken(token: CopilotToken): void {67this._tokenStore.copilotToken = token;68this.fireAuthenticationChange('setCopilotToken');69}707172override getAnyAdoSession(_options?: AuthenticationGetSessionOptions): Promise<AuthenticationSession | undefined> {73return Promise.resolve(undefined);74}7576override getAdoAccessTokenBase64(options?: AuthenticationGetSessionOptions): Promise<string | undefined> {77return Promise.resolve(undefined);78}79}8081export function setCopilotToken(authenticationService: IAuthenticationService, token: CopilotToken): void {82if (!(authenticationService instanceof StaticGitHubAuthenticationService)) {83throw new Error('This function should only be used with StaticGitHubAuthenticationService');84}85(authenticationService as StaticGitHubAuthenticationService).setCopilotToken(token);86}878889