diff --git a/src/vs/workbench/api/common/extHostAuthentication.ts b/src/vs/workbench/api/common/extHostAuthentication.ts index afc9355c25a..ad9749b474e 100644 --- a/src/vs/workbench/api/common/extHostAuthentication.ts +++ b/src/vs/workbench/api/common/extHostAuthentication.ts @@ -11,6 +11,7 @@ import { IExtensionDescription, ExtensionIdentifier } from 'vs/platform/extensio interface GetSessionsRequest { scopes: string; + providerId: string; result: Promise; } @@ -48,13 +49,14 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape { const extensionId = ExtensionIdentifier.toKey(requestingExtension.identifier); const inFlightRequests = this._inFlightRequests.get(extensionId) || []; const sortedScopes = [...scopes].sort().join(' '); - let inFlightRequest: GetSessionsRequest | undefined = inFlightRequests.find(request => request.scopes === sortedScopes); + let inFlightRequest: GetSessionsRequest | undefined = inFlightRequests.find(request => request.providerId === providerId && request.scopes === sortedScopes); if (inFlightRequest) { return inFlightRequest.result; } else { const session = this._getSession(requestingExtension, extensionId, providerId, scopes, options); inFlightRequest = { + providerId, scopes: sortedScopes, result: session }; @@ -65,7 +67,7 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape { try { await session; } finally { - const requestIndex = inFlightRequests.findIndex(request => request.scopes === sortedScopes); + const requestIndex = inFlightRequests.findIndex(request => request.providerId === providerId && request.scopes === sortedScopes); if (requestIndex > -1) { inFlightRequests.splice(requestIndex); this._inFlightRequests.set(extensionId, inFlightRequests); diff --git a/src/vs/workbench/api/test/browser/extHostAuthentication.test.ts b/src/vs/workbench/api/test/browser/extHostAuthentication.test.ts index dd62b92214a..25ffc1d627f 100644 --- a/src/vs/workbench/api/test/browser/extHostAuthentication.test.ts +++ b/src/vs/workbench/api/test/browser/extHostAuthentication.test.ts @@ -59,6 +59,7 @@ class TestAuthProvider implements AuthenticationProvider { private id = 1; private sessions = new Map(); onDidChangeSessions = () => { return { dispose() { } }; }; + constructor(private readonly authProviderName: string) { } async getSessions(scopes?: readonly string[]): Promise { if (!scopes) { return [...this.sessions.values()]; @@ -76,7 +77,7 @@ class TestAuthProvider implements AuthenticationProvider { scopes, id: `${this.id}`, account: { - label: `${this.id}`, + label: this.authProviderName, id: `${this.id}`, }, accessToken: Math.random() + '', @@ -118,11 +119,11 @@ suite('ExtHostAuthentication', () => { setup(async () => { disposables = new DisposableStore(); - disposables.add(extHostAuthentication.registerAuthenticationProvider('test', 'test provider', new TestAuthProvider())); + disposables.add(extHostAuthentication.registerAuthenticationProvider('test', 'test provider', new TestAuthProvider('test'))); disposables.add(extHostAuthentication.registerAuthenticationProvider( 'test-multiple', 'test multiple provider', - new TestAuthProvider(), + new TestAuthProvider('test-multiple'), { supportsMultipleAccounts: true })); }); @@ -441,5 +442,63 @@ suite('ExtHostAuthentication', () => { assert.strictEqual(session?.scopes[0], 'foo'); }); + test('Can get multiple sessions (from different providers) in one extension', async () => { + let session: AuthenticationSession | undefined = await extHostAuthentication.getSession( + extensionDescription, + 'test-multiple', + ['foo'], + { + createIfNone: true + }); + session = await extHostAuthentication.getSession( + extensionDescription, + 'test', + ['foo'], + { + createIfNone: true + }); + assert.strictEqual(session?.id, '1'); + assert.strictEqual(session?.scopes[0], 'foo'); + assert.strictEqual(session?.account.label, 'test'); + + const session2 = await extHostAuthentication.getSession( + extensionDescription, + 'test-multiple', + ['foo'], + { + createIfNone: false + }); + assert.strictEqual(session2?.id, '1'); + assert.strictEqual(session2?.scopes[0], 'foo'); + assert.strictEqual(session2?.account.label, 'test-multiple'); + }); + + test('Can get multiple sessions (from different providers) in one extension at the same time', async () => { + let sessionP: Promise = extHostAuthentication.getSession( + extensionDescription, + 'test', + ['foo'], + { + createIfNone: true + }); + let session2P: Promise = extHostAuthentication.getSession( + extensionDescription, + 'test-multiple', + ['foo'], + { + createIfNone: true + }); + const session = await sessionP; + assert.strictEqual(session?.id, '1'); + assert.strictEqual(session?.scopes[0], 'foo'); + assert.strictEqual(session?.account.label, 'test'); + + const session2 = await session2P; + assert.strictEqual(session2?.id, '1'); + assert.strictEqual(session2?.scopes[0], 'foo'); + assert.strictEqual(session2?.account.label, 'test-multiple'); + }); + + //#endregion });