diff --git a/src/vs/workbench/api/browser/mainThreadAuthentication.ts b/src/vs/workbench/api/browser/mainThreadAuthentication.ts index ef09b13eb17..eae11fc5843 100644 --- a/src/vs/workbench/api/browser/mainThreadAuthentication.ts +++ b/src/vs/workbench/api/browser/mainThreadAuthentication.ts @@ -251,7 +251,9 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu throw new Error('User did not consent to login.'); } - const session = await this.authenticationService.createSession(providerId, scopes, true); + const session = sessions?.length && !options.forceNewSession + ? await this.authenticationService.selectSession(providerId, extensionId, extensionName, scopes, sessions) + : await this.authenticationService.createSession(providerId, scopes, true); await this.setTrustedExtensionAndAccountPreference(providerId, session.account.label, extensionId, extensionName, session.id); return session; } diff --git a/src/vs/workbench/test/browser/api/extHostAuthentication.test.ts b/src/vs/workbench/test/browser/api/extHostAuthentication.test.ts index aa5e63baa4d..260ea31ca98 100644 --- a/src/vs/workbench/test/browser/api/extHostAuthentication.test.ts +++ b/src/vs/workbench/test/browser/api/extHostAuthentication.test.ts @@ -63,6 +63,9 @@ class TestAuthProvider implements AuthenticationProvider { return [...this.sessions.values()]; } + if (scopes[0] === 'return multiple') { + return [...this.sessions.values()]; + } const sessions = this.sessions.get(scopes.join(' ')); return sessions ? [sessions] : []; } @@ -70,7 +73,7 @@ class TestAuthProvider implements AuthenticationProvider { const scopesStr = scopes.join(' '); const session = { scopes, - id: 'test', + id: 'test' + scopesStr, account: { label: scopesStr, id: scopesStr, @@ -126,22 +129,24 @@ suite('ExtHostAuthentication', () => { }); test('createIfNone - true', async () => { + const scopes = ['foo']; const session = await extHostAuthentication.getSession( extensionDescription, 'test', - ['foo'], + scopes, { createIfNone: true }); - assert.strictEqual(session?.id, 'test'); + assert.strictEqual(session?.id, 'test' + scopes.join(' ')); assert.strictEqual(session?.scopes[0], 'foo'); }); test('createIfNone - false', async () => { + const scopes = ['foo']; const nosession = await extHostAuthentication.getSession( extensionDescription, 'test', - ['foo'], + scopes, {}); assert.strictEqual(nosession, undefined); @@ -149,18 +154,18 @@ suite('ExtHostAuthentication', () => { const session = await extHostAuthentication.getSession( extensionDescription, 'test', - ['foo'], + scopes, { createIfNone: true }); - assert.strictEqual(session?.id, 'test'); + assert.strictEqual(session?.id, 'test' + scopes.join(' ')); assert.strictEqual(session?.scopes[0], 'foo'); const session2 = await extHostAuthentication.getSession( extensionDescription, 'test', - ['foo'], + scopes, {}); assert.strictEqual(session.id, session2?.id); @@ -170,10 +175,11 @@ suite('ExtHostAuthentication', () => { // should behave the same as createIfNone: false test('silent - true', async () => { + const scopes = ['foo']; const nosession = await extHostAuthentication.getSession( extensionDescription, 'test', - ['foo'], + scopes, { silent: true }); @@ -183,18 +189,18 @@ suite('ExtHostAuthentication', () => { const session = await extHostAuthentication.getSession( extensionDescription, 'test', - ['foo'], + scopes, { createIfNone: true }); - assert.strictEqual(session?.id, 'test'); + assert.strictEqual(session?.id, 'test' + scopes.join(' ')); assert.strictEqual(session?.scopes[0], 'foo'); const session2 = await extHostAuthentication.getSession( extensionDescription, 'test', - ['foo'], + scopes, { silent: true }); @@ -204,10 +210,11 @@ suite('ExtHostAuthentication', () => { }); test('forceNewSession - true', async () => { + const scopes = ['foo']; const session1 = await extHostAuthentication.getSession( extensionDescription, 'test', - ['foo'], + scopes, { createIfNone: true }); @@ -216,21 +223,22 @@ suite('ExtHostAuthentication', () => { const session2 = await extHostAuthentication.getSession( extensionDescription, 'test', - ['foo'], + scopes, { forceNewSession: true }); - assert.strictEqual(session2?.id, 'test'); + assert.strictEqual(session2?.id, 'test' + scopes.join(' ')); assert.strictEqual(session2?.scopes[0], 'foo'); assert.notStrictEqual(session1.accessToken, session2?.accessToken); }); test('forceNewSession - detail', async () => { + const scopes = ['foo']; const session1 = await extHostAuthentication.getSession( extensionDescription, 'test', - ['foo'], + scopes, { createIfNone: true }); @@ -239,41 +247,55 @@ suite('ExtHostAuthentication', () => { const session2 = await extHostAuthentication.getSession( extensionDescription, 'test', - ['foo'], + scopes, { forceNewSession: { detail: 'bar' } }); - assert.strictEqual(session2?.id, 'test'); + assert.strictEqual(session2?.id, 'test' + scopes.join(' ')); assert.strictEqual(session2?.scopes[0], 'foo'); assert.notStrictEqual(session1.accessToken, session2?.accessToken); }); test('clearSessionPreference - true', async () => { + const scopes = ['foo']; // Now create the session const session = await extHostAuthentication.getSession( extensionDescription, 'test-multiple', - ['foo'], + scopes, { createIfNone: true }); - assert.strictEqual(session?.id, 'test'); - assert.strictEqual(session?.scopes[0], 'foo'); + assert.strictEqual(session?.id, 'test' + scopes.join(' ')); + assert.strictEqual(session?.scopes[0], scopes[0]); + const scopes2 = ['bar']; const session2 = await extHostAuthentication.getSession( extensionDescription, 'test-multiple', - ['foo'], + scopes2, + { + createIfNone: true + }); + assert.strictEqual(session2?.id, 'test' + scopes2.join(' ')); + assert.strictEqual(session2?.scopes[0], scopes2[0]); + + const session3 = await extHostAuthentication.getSession( + extensionDescription, + 'test-multiple', + ['return multiple'], { clearSessionPreference: true, createIfNone: true }); - assert.strictEqual(session.id, session2?.id); - assert.strictEqual(session.scopes[0], session2?.scopes[0]); - assert.notStrictEqual(session.accessToken, session2?.accessToken); + // clearing session preference causes us to get the first session + // because it would normally show a quick pick for the user to choose + assert.strictEqual(session.id, session3?.id); + assert.strictEqual(session.scopes[0], session3?.scopes[0]); + assert.strictEqual(session.accessToken, session3?.accessToken); }); //#region error cases