From 7f21b1af48502bdf710e84f9fb7d90db09c24945 Mon Sep 17 00:00:00 2001 From: Tyler James Leonhardt Date: Wed, 26 May 2021 10:42:20 -0700 Subject: [PATCH] Revert "Allow extensions to create multiple sessions from the same provider (#124640)" (#124705) This reverts commit a18ea9c9ec25c59e599ba3c2eb668315546dd269. --- .../api/browser/mainThreadAuthentication.ts | 13 +- .../api/common/extHostAuthentication.ts | 5 +- .../browser/authenticationService.ts | 4 +- .../api/mainThreadAuthentication.test.ts | 136 ------------------ .../test/common/workbenchTestServices.ts | 19 --- 5 files changed, 14 insertions(+), 163 deletions(-) delete mode 100644 src/vs/workbench/test/browser/api/mainThreadAuthentication.test.ts diff --git a/src/vs/workbench/api/browser/mainThreadAuthentication.ts b/src/vs/workbench/api/browser/mainThreadAuthentication.ts index 401db52faa2..19e704dff86 100644 --- a/src/vs/workbench/api/browser/mainThreadAuthentication.ts +++ b/src/vs/workbench/api/browser/mainThreadAuthentication.ts @@ -183,15 +183,21 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu return choice === 0; } + private async setTrustedExtensionAndAccountPreference(providerId: string, accountName: string, extensionId: string, extensionName: string, sessionId: string): Promise { + this.authenticationService.updatedAllowedExtension(providerId, accountName, extensionId, extensionName, true); + this.storageService.store(`${extensionName}-${providerId}`, sessionId, StorageScope.GLOBAL, StorageTarget.MACHINE); + + } + private async selectSession(providerId: string, extensionId: string, extensionName: string, scopes: string[], potentialSessions: readonly modes.AuthenticationSession[], clearSessionPreference: boolean, silent: boolean): Promise { if (!potentialSessions.length) { throw new Error('No potential sessions found'); } if (clearSessionPreference) { - this.storageService.remove(`${extensionName}-${providerId}-${scopes.join('-')}`, StorageScope.GLOBAL); + this.storageService.remove(`${extensionName}-${providerId}`, StorageScope.GLOBAL); } else { - const existingSessionPreference = this.storageService.get(`${extensionName}-${providerId}-${scopes.join('-')}`, StorageScope.GLOBAL); + const existingSessionPreference = this.storageService.get(`${extensionName}-${providerId}`, StorageScope.GLOBAL); if (existingSessionPreference) { const matchingSession = potentialSessions.find(session => session.id === existingSessionPreference); if (matchingSession) { @@ -255,8 +261,7 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu } session = await this.authenticationService.createSession(providerId, scopes, true); - await this.authenticationService.updatedAllowedExtension(providerId, session.account.label, extensionId, extensionName, true); - this.storageService.store(`${extensionName}-${providerId}-${scopes.join('-')}`, session.id, StorageScope.GLOBAL, StorageTarget.MACHINE); + await this.setTrustedExtensionAndAccountPreference(providerId, session.account.label, extensionId, extensionName, session.id); } else { await this.authenticationService.requestNewSession(providerId, scopes, extensionId, extensionName); } diff --git a/src/vs/workbench/api/common/extHostAuthentication.ts b/src/vs/workbench/api/common/extHostAuthentication.ts index 14acdb4343c..c134a357403 100644 --- a/src/vs/workbench/api/common/extHostAuthentication.ts +++ b/src/vs/workbench/api/common/extHostAuthentication.ts @@ -68,8 +68,7 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape { this._inFlightRequests.set(extensionId, inFlightRequests); try { - const s = await session; - return s; + await session; } finally { const requestIndex = inFlightRequests.findIndex(request => request.scopes === sortedScopes); if (requestIndex > -1) { @@ -77,6 +76,8 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape { this._inFlightRequests.set(extensionId, inFlightRequests); } } + + return session; } } diff --git a/src/vs/workbench/services/authentication/browser/authenticationService.ts b/src/vs/workbench/services/authentication/browser/authenticationService.ts index 1654f175077..b5da23219ea 100644 --- a/src/vs/workbench/services/authentication/browser/authenticationService.ts +++ b/src/vs/workbench/services/authentication/browser/authenticationService.ts @@ -503,7 +503,7 @@ export class AuthenticationService extends Disposable implements IAuthentication this.updatedAllowedExtension(providerId, accountName, extensionId, extensionName, true); this.removeAccessRequest(providerId, extensionId); - this.storageService.store(`${extensionName}-${providerId}-${scopes.join('-')}`, session.id, StorageScope.GLOBAL, StorageTarget.MACHINE); + this.storageService.store(`${extensionName}-${providerId}`, session.id, StorageScope.GLOBAL, StorageTarget.MACHINE); quickPick.dispose(); resolve(session); @@ -638,7 +638,7 @@ export class AuthenticationService extends Disposable implements IAuthentication this.updatedAllowedExtension(providerId, session.account.label, extensionId, extensionName, true); // And also set it as the preferred account for the extension - storageService.store(`${extensionName}-${providerId}-${scopes.join('-')}`, session.id, StorageScope.GLOBAL, StorageTarget.MACHINE); + storageService.store(`${extensionName}-${providerId}`, session.id, StorageScope.GLOBAL, StorageTarget.MACHINE); } }); diff --git a/src/vs/workbench/test/browser/api/mainThreadAuthentication.test.ts b/src/vs/workbench/test/browser/api/mainThreadAuthentication.test.ts deleted file mode 100644 index e4bd24e47b9..00000000000 --- a/src/vs/workbench/test/browser/api/mainThreadAuthentication.test.ts +++ /dev/null @@ -1,136 +0,0 @@ -/*--------------------------------------------------------------------------------------------- - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See License.txt in the project root for license information. - *--------------------------------------------------------------------------------------------*/ - -import * as assert from 'assert'; -import { AuthenticationProviderInformation } from 'vs/editor/common/modes'; -import { IDialogService } from 'vs/platform/dialogs/common/dialogs'; -import { TestDialogService } from 'vs/platform/dialogs/test/common/testDialogService'; -import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService'; -import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection'; -import { INotificationService } from 'vs/platform/notification/common/notification'; -import { TestNotificationService } from 'vs/platform/notification/test/common/testNotificationService'; -import { IQuickInputHideEvent, IQuickInputService, IQuickPickDidAcceptEvent } from 'vs/platform/quickinput/common/quickInput'; -import { IStorageService } from 'vs/platform/storage/common/storage'; -import { MainThreadAuthentication } from 'vs/workbench/api/browser/mainThreadAuthentication'; -import { IExtHostContext } from 'vs/workbench/api/common/extHost.protocol'; -import { IActivityService } from 'vs/workbench/services/activity/common/activity'; -import { AuthenticationService, IAuthenticationService } from 'vs/workbench/services/authentication/browser/authenticationService'; -import { ExtensionHostKind, IExtensionService } from 'vs/workbench/services/extensions/common/extensions'; -import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService'; -import { TestRemoteAgentService } from 'vs/workbench/services/remote/test/common/testServices'; -import { TestQuickInputService } from 'vs/workbench/test/browser/workbenchTestServices'; -import { TestActivityService, TestExtensionService, TestStorageService } from 'vs/workbench/test/common/workbenchTestServices'; - -function createSession(id: string = '1234', scope: string[] = []) { - return { - accessToken: '1234', - account: { - id: 'test@test.com', - label: 'Test Person' - }, - id: id, - scopes: scope - }; -} - -class AuthQuickPick { - private listener: ((e: IQuickPickDidAcceptEvent) => any) | undefined; - public items = []; - public get selectedItems(): string[] { - return this.items; - } - - onDidAccept(listener: (e: IQuickPickDidAcceptEvent) => any) { - this.listener = listener; - } - onDidHide(listener: (e: IQuickInputHideEvent) => any) { - - } - dispose() { - - } - show() { - this.listener!({ - inBackground: false - }); - } -} -class AuthTestQuickInputService extends TestQuickInputService { - override createQuickPick() { - return new AuthQuickPick(); - } -} - -suite('MainThreadAuthentication', () => { - let mainThreadAuthentication: MainThreadAuthentication; - suiteSetup(async () => { - // extHostContext: IExtHostContext, - const services = new ServiceCollection(); - const dialogService = new TestDialogService(); - const storageService = new TestStorageService(); - const quickInputService = new AuthTestQuickInputService(); - const extensionService = new TestExtensionService(); - - const activityService = new TestActivityService(); - const remoteAgentService = new TestRemoteAgentService(); - - services.set(IDialogService, dialogService); - services.set(IStorageService, storageService); - services.set(INotificationService, new TestNotificationService()); - services.set(IQuickInputService, quickInputService); - services.set(IExtensionService, extensionService); - services.set(IActivityService, activityService); - services.set(IRemoteAgentService, remoteAgentService); - - const instaService = new InstantiationService(services); - services.set(IAuthenticationService, instaService.createInstance(AuthenticationService)); - - mainThreadAuthentication = instaService.createInstance(MainThreadAuthentication, - new class implements IExtHostContext { - remoteAuthority = ''; - extensionHostKind = ExtensionHostKind.LocalProcess; - assertRegistered() { } - set(v: any): any { return null; } - getProxy(): any { - return { - $getSessions(id: string, scopes: string[]) { - return Promise.resolve([createSession(id, scopes)]); - }, - $createSession(id: string, scopes: string[]) { - return Promise.resolve(createSession(id, scopes)); - }, - $removeSession(id: string, sessionId: string) { return Promise.resolve(); }, - $onDidChangeAuthenticationSessions(id: string, label: string) { return Promise.resolve(); }, - $onDidChangeAuthenticationProviders(added: AuthenticationProviderInformation[], removed: AuthenticationProviderInformation[]) { return Promise.resolve(); }, - $setProviders(providers: AuthenticationProviderInformation[]) { return Promise.resolve(); } - }; - } - drain(): any { return null; } - }); - - await mainThreadAuthentication.$registerAuthenticationProvider('test', 'test provider', true); - }); - - suiteTeardown(() => { - mainThreadAuthentication.$unregisterAuthenticationProvider('test'); - }); - - test('Can get a session', async () => { - const session = await mainThreadAuthentication.$getSession('test', ['foo'], 'testextension', 'test extension', { createIfNone: true, clearSessionPreference: false }); - assert.strictEqual(session?.id, 'test'); - assert.strictEqual(session?.scopes[0], 'foo'); - }); - - test('Can get multiple sessions (with different scopes) in one extension', async () => { - let session = await mainThreadAuthentication.$getSession('test', ['foo'], 'testextension', 'test extension', { createIfNone: true, clearSessionPreference: false }); - session = await mainThreadAuthentication.$getSession('test', ['bar'], 'testextension', 'test extension', { createIfNone: true, clearSessionPreference: false }); - assert.strictEqual(session?.id, 'test'); - assert.strictEqual(session?.scopes[0], 'bar'); - - session = await mainThreadAuthentication.$getSession('test', ['foo'], 'testextension', 'test extension', { createIfNone: false, clearSessionPreference: false }); - assert.strictEqual(session?.id, 'test'); - assert.strictEqual(session?.scopes[0], 'foo'); - }); -}); diff --git a/src/vs/workbench/test/common/workbenchTestServices.ts b/src/vs/workbench/test/common/workbenchTestServices.ts index f4450ecfcd5..b2ddf026545 100644 --- a/src/vs/workbench/test/common/workbenchTestServices.ts +++ b/src/vs/workbench/test/common/workbenchTestServices.ts @@ -22,7 +22,6 @@ import { IFileStatWithMetadata } from 'vs/platform/files/common/files'; import { ISaveOptions, IRevertOptions } from 'vs/workbench/common/editor'; import { CancellationToken } from 'vs/base/common/cancellation'; import product from 'vs/platform/product/common/product'; -import { IActivity, IActivityService } from 'vs/workbench/services/activity/common/activity'; export class TestTextResourcePropertiesService implements ITextResourcePropertiesService { @@ -215,21 +214,3 @@ export interface Ctor { export class TestExtensionService extends NullExtensionService { } export const TestProductService = { _serviceBrand: undefined, ...product }; - -export class TestActivityService implements IActivityService { - _serviceBrand: undefined; - showViewContainerActivity(viewContainerId: string, badge: IActivity): IDisposable { - return this; - } - showViewActivity(viewId: string, badge: IActivity): IDisposable { - return this; - } - showAccountsActivity(activity: IActivity): IDisposable { - return this; - } - showGlobalActivity(activity: IActivity): IDisposable { - return this; - } - - dispose() { } -}