|
1 | | -import { |
2 | | - authentication, |
3 | | - type AuthenticationProvider, |
4 | | - type AuthenticationProviderAuthenticationSessionsChangeEvent, |
5 | | - type AuthenticationSession, |
6 | | - commands, |
7 | | - Disposable, |
8 | | - type Event, |
9 | | - EventEmitter, |
10 | | - type SecretStorage, |
11 | | -} from 'vscode' |
| 1 | +import { commands, type SecretStorage } from 'vscode' |
12 | 2 |
|
13 | | -import polyfillEventSource from '@sourcegraph/shared/src/polyfills/vendor/eventSource' |
14 | | - |
15 | | -import { getProxyAgent } from '../../backend/fetch' |
16 | | -import { endpointRequestHeadersSetting, endpointSetting, setEndpoint } from '../../settings/endpointSetting' |
| 3 | +import { setEndpoint } from '../../settings/endpointSetting' |
17 | 4 |
|
18 | 5 | export const secretTokenKey = 'SOURCEGRAPH_AUTH' |
19 | 6 |
|
20 | | -class SourcegraphAuthSession implements AuthenticationSession { |
21 | | - public readonly account = { |
22 | | - id: SourcegraphAuthProvider.id, |
23 | | - label: SourcegraphAuthProvider.label, |
24 | | - } |
25 | | - public readonly id = SourcegraphAuthProvider.id |
26 | | - public readonly scopes = [] |
27 | | - |
28 | | - constructor(public readonly accessToken: string) {} |
29 | | -} |
30 | | - |
31 | | -export class SourcegraphAuthProvider implements AuthenticationProvider, Disposable { |
32 | | - public static id = endpointSetting() |
33 | | - private static secretKey = secretTokenKey |
34 | | - public static label = secretTokenKey |
35 | | - |
36 | | - // Kept track of token changes through out the session |
37 | | - private currentToken: string | undefined |
38 | | - private initializedDisposable: Disposable | undefined |
39 | | - private _onDidChangeSessions = new EventEmitter<AuthenticationProviderAuthenticationSessionsChangeEvent>() |
40 | | - public get onDidChangeSessions(): Event<AuthenticationProviderAuthenticationSessionsChangeEvent> { |
41 | | - return this._onDidChangeSessions.event |
42 | | - } |
43 | | - |
44 | | - constructor(private readonly secretStorage: SecretStorage) {} |
45 | | - |
46 | | - public dispose(): void { |
47 | | - this.initializedDisposable?.dispose() |
48 | | - } |
49 | | - |
50 | | - private async ensureInitialized(): Promise<void> { |
51 | | - if (this.initializedDisposable === undefined) { |
52 | | - await this.cacheTokenFromStorage() |
53 | | - this.initializedDisposable = Disposable.from( |
54 | | - this.secretStorage.onDidChange(async event => { |
55 | | - if (event.key === SourcegraphAuthProvider.secretKey) { |
56 | | - await this.checkForUpdates() |
57 | | - } |
58 | | - }), |
59 | | - authentication.onDidChangeSessions(async event => { |
60 | | - if (event.provider.id === SourcegraphAuthProvider.id) { |
61 | | - await this.checkForUpdates() |
62 | | - } |
63 | | - }) |
64 | | - ) |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - // Check if token has been updated across VS Code |
69 | | - private async checkForUpdates(): Promise<void> { |
70 | | - const added: AuthenticationSession[] = [] |
71 | | - const removed: AuthenticationSession[] = [] |
72 | | - const changed: AuthenticationSession[] = [] |
73 | | - const previousToken = this.currentToken |
74 | | - const session = (await this.getSessions())[0] |
75 | | - if (session?.accessToken && !previousToken) { |
76 | | - added.push(session) |
77 | | - } else if (!session?.accessToken && previousToken) { |
78 | | - removed.push(session) |
79 | | - } else if (session?.accessToken !== previousToken) { |
80 | | - changed.push(session) |
81 | | - } else { |
82 | | - return |
83 | | - } |
84 | | - await this.cacheTokenFromStorage() |
85 | | - // Update the polyfillEventSource on token changes |
86 | | - polyfillEventSource( |
87 | | - this.currentToken |
88 | | - ? { Authorization: `token ${this.currentToken}`, ...endpointRequestHeadersSetting() } |
89 | | - : {}, |
90 | | - getProxyAgent() |
91 | | - ) |
92 | | - this._onDidChangeSessions.fire({ added, removed, changed }) |
93 | | - } |
94 | | - |
95 | | - // Get token from Storage |
96 | | - private async cacheTokenFromStorage(): Promise<string | undefined> { |
97 | | - const token = await this.secretStorage.get(SourcegraphAuthProvider.secretKey) |
98 | | - this.currentToken = token |
99 | | - return this.currentToken |
100 | | - } |
101 | | - |
102 | | - // This is called first when `vscode.authentication.getSessions` is called. |
103 | | - public async getSessions(_scopes?: string[]): Promise<readonly AuthenticationSession[]> { |
104 | | - await this.ensureInitialized() |
105 | | - const token = await this.cacheTokenFromStorage() |
106 | | - return token ? [new SourcegraphAuthSession(token)] : [] |
107 | | - } |
108 | | - |
109 | | - // This is called after `this.getSessions` is called, |
110 | | - // and only when `createIfNone` or `forceNewSession` are set to true |
111 | | - public async createSession(_scopes: string[]): Promise<AuthenticationSession> { |
112 | | - await this.ensureInitialized() |
113 | | - // Get token from scret storage |
114 | | - let token = await this.secretStorage.get(SourcegraphAuthProvider.secretKey) |
115 | | - if (token) { |
116 | | - console.log('Successfully logged in to Sourcegraph', token) |
117 | | - await this.secretStorage.store(SourcegraphAuthProvider.secretKey, token) |
118 | | - } |
119 | | - if (!token) { |
120 | | - token = '' |
121 | | - } |
122 | | - return new SourcegraphAuthSession(token) |
123 | | - } |
124 | | - |
125 | | - // To sign out |
126 | | - public async removeSession(_sessionId: string): Promise<void> { |
127 | | - await this.secretStorage.delete(SourcegraphAuthProvider.secretKey) |
128 | | - console.log('Successfully logged out of Sourcegraph') |
129 | | - } |
130 | | -} |
131 | | - |
132 | 7 | export class SourcegraphAuthActions { |
133 | | - private currentEndpoint = endpointSetting() |
134 | | - |
135 | 8 | constructor(private readonly secretStorage: SecretStorage) {} |
136 | 9 |
|
137 | 10 | public async login(newtoken: string, newuri: string): Promise<void> { |
138 | 11 | try { |
139 | 12 | await this.secretStorage.store(secretTokenKey, newtoken) |
140 | | - if (this.currentEndpoint !== newuri) { |
141 | | - setEndpoint(newuri) |
142 | | - } |
| 13 | + setEndpoint(newuri) |
143 | 14 | return |
144 | 15 | } catch (error) { |
145 | 16 | console.error(error) |
|
0 commit comments