Make scopes parameter optional to getSessions and remove getAllSessions

This commit is contained in:
Rachel Macfarlane 2021-02-12 09:05:31 -08:00
parent a16f5d2c4c
commit 14669c2e45
13 changed files with 27 additions and 51 deletions

View file

@ -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__

View file

@ -44,8 +44,10 @@ export class GitHubAuthenticationProvider {
context.subscriptions.push(context.secrets.onDidChange(() => this.checkForUpdates()));
}
async getSessions(scopes: string[]): Promise<vscode.AuthenticationSession[]> {
return this._sessions.filter(session => arrayEquals(session.scopes, scopes));
async getSessions(scopes?: string[]): Promise<vscode.AuthenticationSession[]> {
return scopes
? this._sessions.filter(session => arrayEquals(session.scopes, scopes))
: this._sessions;
}
private async verifySessions(): Promise<void> {

View file

@ -300,7 +300,11 @@ export class AzureActiveDirectoryService {
return Promise.all(this._tokens.map(token => this.convertToSession(token)));
}
async getSessions(scopes: string[]): Promise<vscode.AuthenticationSession[]> {
async getSessions(scopes?: string[]): Promise<vscode.AuthenticationSession[]> {
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)));

View file

@ -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 {

View file

@ -64,29 +64,25 @@ declare module 'vscode' {
readonly onDidChangeSessions: Event<AuthenticationProviderAuthenticationSessionsChangeEvent>;
/**
* 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<ReadonlyArray<AuthenticationSession>>;
/**
* Returns an array of current sessions.
*/
// eslint-disable-next-line vscode-dts-provider-naming
getSessions(scopes: string[]): Thenable<ReadonlyArray<AuthenticationSession>>;
getSessions(scopes?: string[]): Thenable<ReadonlyArray<AuthenticationSession>>;
/**
* 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<AuthenticationSession>;
/**
* 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<void>;

View file

@ -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<ReadonlyArray<modes.AuthenticationSession>> {
return this._proxy.$getAllSessions(this.id);
}
createSession(scopes: string[]): Promise<modes.AuthenticationSession> {
return this._proxy.$createSession(this.id, scopes);
}

View file

@ -1125,8 +1125,7 @@ export interface ExtHostLabelServiceShape {
}
export interface ExtHostAuthenticationShape {
$getAllSessions(id: string): Promise<ReadonlyArray<modes.AuthenticationSession>>;
$getSessions(id: string, scopes: string[]): Promise<ReadonlyArray<modes.AuthenticationSession>>;
$getSessions(id: string, scopes?: string[]): Promise<ReadonlyArray<modes.AuthenticationSession>>;
$createSession(id: string, scopes: string[]): Promise<modes.AuthenticationSession>;
$removeSession(id: string, sessionId: string): Promise<void>;
$onDidChangeAuthenticationSessions(id: string, label: string, event: modes.AuthenticationSessionsChangeEvent): Promise<void>;

View file

@ -147,16 +147,7 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
throw new Error(`Unable to find authentication provider with handle: ${providerId}`);
}
$getAllSessions(providerId: string): Promise<ReadonlyArray<modes.AuthenticationSession>> {
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<ReadonlyArray<modes.AuthenticationSession>> {
$getSessions(providerId: string, scopes?: string[]): Promise<ReadonlyArray<modes.AuthenticationSession>> {
const providerData = this._authenticationProviders.get(providerId);
if (providerData) {
return Promise.resolve(providerData.provider.getSessions(scopes));

View file

@ -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 => {

View file

@ -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({

View file

@ -206,7 +206,7 @@ export async function readWorkspaceTrustedDomains(accessor: ServicesAccessor): P
export async function readAuthenticationTrustedDomains(accessor: ServicesAccessor): Promise<string[]> {
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`]
: [];
}

View file

@ -124,8 +124,7 @@ export interface IAuthenticationService {
declaredProviders: AuthenticationProviderInformation[];
readonly onDidChangeDeclaredProviders: Event<AuthenticationProviderInformation[]>;
getSessions(id: string, scopes: string[], activateImmediate?: boolean): Promise<ReadonlyArray<AuthenticationSession>>;
getAllSessions(providerId: string, activateImmediate?: boolean): Promise<ReadonlyArray<AuthenticationSession>>;
getSessions(id: string, scopes?: string[], activateImmediate?: boolean): Promise<ReadonlyArray<AuthenticationSession>>;
getLabel(providerId: string): string;
supportsMultipleAccounts(providerId: string): boolean;
createSession(providerId: string, scopes: string[], activateImmediate?: boolean): Promise<AuthenticationSession>;
@ -694,16 +693,7 @@ export class AuthenticationService extends Disposable implements IAuthentication
return Promise.race([didRegister, didTimeout]);
}
async getAllSessions(id: string, activateImmediate: boolean = false): Promise<ReadonlyArray<AuthenticationSession>> {
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<ReadonlyArray<AuthenticationSession>> {
async getSessions(id: string, scopes?: string[], activateImmediate: boolean = false): Promise<ReadonlyArray<AuthenticationSession>> {
try {
const authProvider = this._authenticationProviders.get(id) || await this.tryActivateProvider(id, activateImmediate);
return await authProvider.getSessions(scopes);

View file

@ -202,7 +202,7 @@ export class UserDataSyncWorkbenchService extends Disposable implements IUserDat
let accounts: Map<string, UserDataSyncAccount> = new Map<string, UserDataSyncAccount>();
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);