From 14669c2e45731995833b9598ee087020fa006972 Mon Sep 17 00:00:00 2001 From: Rachel Macfarlane Date: Fri, 12 Feb 2021 09:05:31 -0800 Subject: [PATCH] Make scopes parameter optional to getSessions and remove getAllSessions --- .../github-authentication/src/extension.ts | 3 +-- .../github-authentication/src/github.ts | 6 ++++-- .../microsoft-authentication/src/AADHelper.ts | 6 +++++- .../microsoft-authentication/src/extension.ts | 1 - src/vs/vscode.proposed.d.ts | 20 ++++++++----------- .../api/browser/mainThreadAuthentication.ts | 6 +----- .../workbench/api/common/extHost.protocol.ts | 3 +-- .../api/common/extHostAuthentication.ts | 11 +--------- .../parts/activitybar/activitybarActions.ts | 2 +- .../issue/electron-sandbox/issueService.ts | 2 +- .../contrib/url/browser/trustedDomains.ts | 2 +- .../browser/authenticationService.ts | 14 ++----------- .../browser/userDataSyncWorkbenchService.ts | 2 +- 13 files changed, 27 insertions(+), 51 deletions(-) diff --git a/extensions/github-authentication/src/extension.ts b/extensions/github-authentication/src/extension.ts index c9b76d81670..df80913cd22 100644 --- a/extensions/github-authentication/src/extension.ts +++ b/extensions/github-authentication/src/extension.ts @@ -24,8 +24,7 @@ export async function activate(context: vscode.ExtensionContext) { context.subscriptions.push(vscode.authentication.registerAuthenticationProvider('github', 'GitHub', { onDidChangeSessions: onDidChangeSessions.event, - getAllSessions: () => Promise.resolve(loginService.sessions), - getSessions: (scopes: string[]) => loginService.getSessions(scopes), + getSessions: (scopes?: string[]) => loginService.getSessions(scopes), createSession: async (scopeList: string[]) => { try { /* __GDPR__ diff --git a/extensions/github-authentication/src/github.ts b/extensions/github-authentication/src/github.ts index 89893c94ccd..bf6cee27132 100644 --- a/extensions/github-authentication/src/github.ts +++ b/extensions/github-authentication/src/github.ts @@ -44,8 +44,10 @@ export class GitHubAuthenticationProvider { context.subscriptions.push(context.secrets.onDidChange(() => this.checkForUpdates())); } - async getSessions(scopes: string[]): Promise { - return this._sessions.filter(session => arrayEquals(session.scopes, scopes)); + async getSessions(scopes?: string[]): Promise { + return scopes + ? this._sessions.filter(session => arrayEquals(session.scopes, scopes)) + : this._sessions; } private async verifySessions(): Promise { diff --git a/extensions/microsoft-authentication/src/AADHelper.ts b/extensions/microsoft-authentication/src/AADHelper.ts index fbb4e10a0e8..b0631344346 100644 --- a/extensions/microsoft-authentication/src/AADHelper.ts +++ b/extensions/microsoft-authentication/src/AADHelper.ts @@ -300,7 +300,11 @@ export class AzureActiveDirectoryService { return Promise.all(this._tokens.map(token => this.convertToSession(token))); } - async getSessions(scopes: string[]): Promise { + async getSessions(scopes?: string[]): Promise { + if (!scopes) { + return this.sessions; + } + const orderedScopes = scopes.sort().join(' '); const matchingTokens = this._tokens.filter(token => token.scope === orderedScopes); return Promise.all(matchingTokens.map(token => this.convertToSession(token))); diff --git a/extensions/microsoft-authentication/src/extension.ts b/extensions/microsoft-authentication/src/extension.ts index 584a4027b64..41b8690f344 100644 --- a/extensions/microsoft-authentication/src/extension.ts +++ b/extensions/microsoft-authentication/src/extension.ts @@ -20,7 +20,6 @@ export async function activate(context: vscode.ExtensionContext) { context.subscriptions.push(vscode.authentication.registerAuthenticationProvider('microsoft', 'Microsoft', { onDidChangeSessions: onDidChangeSessions.event, - getAllSessions: () => Promise.resolve(loginService.sessions), getSessions: (scopes: string[]) => loginService.getSessions(scopes), createSession: async (scopes: string[]) => { try { diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index febf42232e6..dbad76952cb 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -64,29 +64,25 @@ declare module 'vscode' { readonly onDidChangeSessions: Event; /** - * Returns an array of current sessions. - * - * TODO @RMacfarlane finish deprecating this and remove it + * Get a list of sessions. + * @param scopes An optional list of scopes. If provided, the sessions returned should match + * these permissions, otherwise all sessions should be returned. + * @returns A promise that resolves to an array of authentication sessions. */ // eslint-disable-next-line vscode-dts-provider-naming - getAllSessions(): Thenable>; - - - /** - * Returns an array of current sessions. - */ - // eslint-disable-next-line vscode-dts-provider-naming - getSessions(scopes: string[]): Thenable>; + getSessions(scopes?: string[]): Thenable>; /** * Prompts a user to login. + * @param scopes A list of scopes, permissions, that the new session should be created with. + * @returns A promise that resolves to an authentication session. */ // eslint-disable-next-line vscode-dts-provider-naming createSession(scopes: string[]): Thenable; /** * Removes the session corresponding to session id. - * @param sessionId The session id to log out of + * @param sessionId The id of the session to remove. */ // eslint-disable-next-line vscode-dts-provider-naming removeSession(sessionId: string): Thenable; diff --git a/src/vs/workbench/api/browser/mainThreadAuthentication.ts b/src/vs/workbench/api/browser/mainThreadAuthentication.ts index dfad0388642..64f7d14880f 100644 --- a/src/vs/workbench/api/browser/mainThreadAuthentication.ts +++ b/src/vs/workbench/api/browser/mainThreadAuthentication.ts @@ -89,14 +89,10 @@ export class MainThreadAuthenticationProvider extends Disposable { } } - async getSessions(scopes: string[]) { + async getSessions(scopes?: string[]) { return this._proxy.$getSessions(this.id, scopes); } - async getAllSessions(): Promise> { - return this._proxy.$getAllSessions(this.id); - } - createSession(scopes: string[]): Promise { return this._proxy.$createSession(this.id, scopes); } diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index 9e121b2c822..a55a0c041a1 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -1125,8 +1125,7 @@ export interface ExtHostLabelServiceShape { } export interface ExtHostAuthenticationShape { - $getAllSessions(id: string): Promise>; - $getSessions(id: string, scopes: string[]): Promise>; + $getSessions(id: string, scopes?: string[]): Promise>; $createSession(id: string, scopes: string[]): Promise; $removeSession(id: string, sessionId: string): Promise; $onDidChangeAuthenticationSessions(id: string, label: string, event: modes.AuthenticationSessionsChangeEvent): Promise; diff --git a/src/vs/workbench/api/common/extHostAuthentication.ts b/src/vs/workbench/api/common/extHostAuthentication.ts index 988f93a5c9f..b4445864a7c 100644 --- a/src/vs/workbench/api/common/extHostAuthentication.ts +++ b/src/vs/workbench/api/common/extHostAuthentication.ts @@ -147,16 +147,7 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape { throw new Error(`Unable to find authentication provider with handle: ${providerId}`); } - $getAllSessions(providerId: string): Promise> { - const providerData = this._authenticationProviders.get(providerId); - if (providerData) { - return Promise.resolve(providerData.provider.getAllSessions()); - } - - throw new Error(`Unable to find authentication provider with handle: ${providerId}`); - } - - $getSessions(providerId: string, scopes: string[]): Promise> { + $getSessions(providerId: string, scopes?: string[]): Promise> { const providerData = this._authenticationProviders.get(providerId); if (providerData) { return Promise.resolve(providerData.provider.getSessions(scopes)); diff --git a/src/vs/workbench/browser/parts/activitybar/activitybarActions.ts b/src/vs/workbench/browser/parts/activitybar/activitybarActions.ts index c3f7817a47f..8b62c5a88de 100644 --- a/src/vs/workbench/browser/parts/activitybar/activitybarActions.ts +++ b/src/vs/workbench/browser/parts/activitybar/activitybarActions.ts @@ -210,7 +210,7 @@ export class AccountsActivityActionViewItem extends MenuActivityActionViewItem { const providers = this.authenticationService.getProviderIds(); const allSessions = providers.map(async providerId => { try { - const sessions = await this.authenticationService.getAllSessions(providerId); + const sessions = await this.authenticationService.getSessions(providerId); const groupedSessions: { [label: string]: AuthenticationSession[]; } = {}; sessions.forEach(session => { diff --git a/src/vs/workbench/contrib/issue/electron-sandbox/issueService.ts b/src/vs/workbench/contrib/issue/electron-sandbox/issueService.ts index 1deb2d639b0..94beb1646ae 100644 --- a/src/vs/workbench/contrib/issue/electron-sandbox/issueService.ts +++ b/src/vs/workbench/contrib/issue/electron-sandbox/issueService.ts @@ -54,7 +54,7 @@ export class WorkbenchIssueService implements IWorkbenchIssueService { }; }); const experiments = await this.experimentService.getCurrentExperiments(); - const githubSessions = await this.authenticationService.getAllSessions('github'); + const githubSessions = await this.authenticationService.getSessions('github'); const potentialSessions = githubSessions.filter(session => session.scopes.includes('repo')); const theme = this.themeService.getColorTheme(); const issueReporterData: IssueReporterData = Object.assign({ diff --git a/src/vs/workbench/contrib/url/browser/trustedDomains.ts b/src/vs/workbench/contrib/url/browser/trustedDomains.ts index b5188bfb036..145afdf032c 100644 --- a/src/vs/workbench/contrib/url/browser/trustedDomains.ts +++ b/src/vs/workbench/contrib/url/browser/trustedDomains.ts @@ -206,7 +206,7 @@ export async function readWorkspaceTrustedDomains(accessor: ServicesAccessor): P export async function readAuthenticationTrustedDomains(accessor: ServicesAccessor): Promise { const authenticationService = accessor.get(IAuthenticationService); - return authenticationService.isAuthenticationProviderRegistered('github') && ((await authenticationService.getAllSessions('github')) ?? []).length > 0 + return authenticationService.isAuthenticationProviderRegistered('github') && ((await authenticationService.getSessions('github')) ?? []).length > 0 ? [`https://github.com`] : []; } diff --git a/src/vs/workbench/services/authentication/browser/authenticationService.ts b/src/vs/workbench/services/authentication/browser/authenticationService.ts index 5d6de318988..6c1efc704be 100644 --- a/src/vs/workbench/services/authentication/browser/authenticationService.ts +++ b/src/vs/workbench/services/authentication/browser/authenticationService.ts @@ -124,8 +124,7 @@ export interface IAuthenticationService { declaredProviders: AuthenticationProviderInformation[]; readonly onDidChangeDeclaredProviders: Event; - getSessions(id: string, scopes: string[], activateImmediate?: boolean): Promise>; - getAllSessions(providerId: string, activateImmediate?: boolean): Promise>; + getSessions(id: string, scopes?: string[], activateImmediate?: boolean): Promise>; getLabel(providerId: string): string; supportsMultipleAccounts(providerId: string): boolean; createSession(providerId: string, scopes: string[], activateImmediate?: boolean): Promise; @@ -694,16 +693,7 @@ export class AuthenticationService extends Disposable implements IAuthentication return Promise.race([didRegister, didTimeout]); } - async getAllSessions(id: string, activateImmediate: boolean = false): Promise> { - try { - const authProvider = this._authenticationProviders.get(id) || await this.tryActivateProvider(id, activateImmediate); - return await authProvider.getAllSessions(); - } catch (_) { - throw new Error(`No authentication provider '${id}' is currently registered.`); - } - } - - async getSessions(id: string, scopes: string[], activateImmediate: boolean = false): Promise> { + async getSessions(id: string, scopes?: string[], activateImmediate: boolean = false): Promise> { try { const authProvider = this._authenticationProviders.get(id) || await this.tryActivateProvider(id, activateImmediate); return await authProvider.getSessions(scopes); diff --git a/src/vs/workbench/services/userDataSync/browser/userDataSyncWorkbenchService.ts b/src/vs/workbench/services/userDataSync/browser/userDataSyncWorkbenchService.ts index 32228c4c3b0..81afe84f97c 100644 --- a/src/vs/workbench/services/userDataSync/browser/userDataSyncWorkbenchService.ts +++ b/src/vs/workbench/services/userDataSync/browser/userDataSyncWorkbenchService.ts @@ -202,7 +202,7 @@ export class UserDataSyncWorkbenchService extends Disposable implements IUserDat let accounts: Map = new Map(); let currentAccount: UserDataSyncAccount | null = null; - const sessions = await this.authenticationService.getAllSessions(authenticationProviderId) || []; + const sessions = await this.authenticationService.getSessions(authenticationProviderId) || []; for (const session of sessions) { const account: UserDataSyncAccount = new UserDataSyncAccount(authenticationProviderId, session); accounts.set(account.accountName, account);