Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/authentication/common/staticGitHubAuthenticationService.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type { AuthenticationGetSessionOptions, AuthenticationSession } from 'vscode';
7
import { IConfigurationService } from '../../configuration/common/configurationService';
8
import { ILogService } from '../../log/common/logService';
9
import { BaseAuthenticationService, GITHUB_SCOPE_ALIGNED, GITHUB_SCOPE_USER_EMAIL, IAuthenticationService, MinimalModeError, StrictAuthenticationPresentationOptions } from './authentication';
10
import { CopilotToken } from './copilotToken';
11
import { ICopilotTokenManager } from './copilotTokenManager';
12
import { ICopilotTokenStore } from './copilotTokenStore';
13
14
export class StaticGitHubAuthenticationService extends BaseAuthenticationService {
15
constructor(
16
private readonly tokenProvider: { (): string } | undefined,
17
@ILogService logService: ILogService,
18
@ICopilotTokenStore tokenStore: ICopilotTokenStore,
19
@ICopilotTokenManager tokenManager: ICopilotTokenManager,
20
@IConfigurationService configurationService: IConfigurationService
21
) {
22
super(logService, tokenStore, tokenManager, configurationService);
23
24
const that = this;
25
this._anyGitHubSession = tokenProvider ? {
26
get id() { return that.tokenProvider!(); },
27
get accessToken() { return that.tokenProvider!(); },
28
scopes: GITHUB_SCOPE_USER_EMAIL,
29
account: {
30
id: 'user',
31
label: 'User'
32
}
33
} : undefined;
34
35
this._permissiveGitHubSession = tokenProvider ? {
36
get id() { return that.tokenProvider!(); },
37
get accessToken() { return that.tokenProvider!(); },
38
scopes: GITHUB_SCOPE_ALIGNED,
39
account: {
40
id: 'user',
41
label: 'User'
42
}
43
} : undefined;
44
}
45
46
override async getGitHubSession(kind: 'permissive' | 'any', options: AuthenticationGetSessionOptions & { createIfNone: StrictAuthenticationPresentationOptions }): Promise<AuthenticationSession>;
47
override async getGitHubSession(kind: 'permissive' | 'any', options: AuthenticationGetSessionOptions & { forceNewSession: StrictAuthenticationPresentationOptions }): Promise<AuthenticationSession>;
48
override async getGitHubSession(kind: 'permissive' | 'any', options: AuthenticationGetSessionOptions): Promise<AuthenticationSession | undefined>;
49
override async getGitHubSession(kind: 'permissive' | 'any', options: AuthenticationGetSessionOptions): Promise<AuthenticationSession | undefined> {
50
if (kind === 'permissive') {
51
if (this.isMinimalMode) {
52
if (options.createIfNone || options.forceNewSession) {
53
throw new MinimalModeError();
54
}
55
return undefined;
56
}
57
return this._permissiveGitHubSession;
58
} else {
59
return this._anyGitHubSession;
60
}
61
}
62
63
override async getCopilotToken(force?: boolean): Promise<CopilotToken> {
64
return await super.getCopilotToken(force);
65
}
66
67
setCopilotToken(token: CopilotToken): void {
68
this._tokenStore.copilotToken = token;
69
this.fireAuthenticationChange('setCopilotToken');
70
}
71
72
73
override getAnyAdoSession(_options?: AuthenticationGetSessionOptions): Promise<AuthenticationSession | undefined> {
74
return Promise.resolve(undefined);
75
}
76
77
override getAdoAccessTokenBase64(options?: AuthenticationGetSessionOptions): Promise<string | undefined> {
78
return Promise.resolve(undefined);
79
}
80
}
81
82
export function setCopilotToken(authenticationService: IAuthenticationService, token: CopilotToken): void {
83
if (!(authenticationService instanceof StaticGitHubAuthenticationService)) {
84
throw new Error('This function should only be used with StaticGitHubAuthenticationService');
85
}
86
(authenticationService as StaticGitHubAuthenticationService).setCopilotToken(token);
87
}
88
89