Auth provider tweaks, change types of change events

This commit is contained in:
Rachel Macfarlane 2020-07-07 17:28:05 -07:00
parent 138e255728
commit 40324ee577
9 changed files with 99 additions and 41 deletions

View file

@ -75,7 +75,7 @@ export class GitHubApi implements Disposable {
if (!providers.includes('github')) {
await new Promise(resolve => {
authentication.onDidChangeAuthenticationProviders(e => {
if (e.added.includes('github')) {
if (e.added.find(provider => provider.id === 'github')) {
resolve();
}
});

View file

@ -1428,6 +1428,14 @@ export interface AuthenticationSessionsChangeEvent {
changed: ReadonlyArray<string>;
}
/**
* @internal
*/
export interface AuthenticationProviderInformation {
id: string;
label: string;
}
export interface Command {
id: string;
title: string;

View file

@ -49,15 +49,30 @@ declare module 'vscode' {
* The information of an account associated with an authentication session.
*/
export interface AuthenticationSessionAccountInformation {
/**
* The human-readable name of the account.
*/
readonly label: string;
/**
* The unique identifier of the account.
*/
readonly id: string;
/**
* The human-readable name of the account.
*/
readonly label: string;
}
/**
* Basic information about an[authenticationProvider](#AuthenticationProvider)
*/
export interface AuthenticationProviderInformation {
/**
* The unique identifier of the authentication provider.
*/
readonly id: string;
/**
* The human-readable name of the authentication provider.
*/
readonly label: string;
}
/**
@ -67,12 +82,12 @@ declare module 'vscode' {
/**
* The ids of the [authenticationProvider](#AuthenticationProvider)s that have been added.
*/
readonly added: ReadonlyArray<string>;
readonly added: ReadonlyArray<AuthenticationProviderInformation>;
/**
* The ids of the [authenticationProvider](#AuthenticationProvider)s that have been removed.
*/
readonly removed: ReadonlyArray<string>;
readonly removed: ReadonlyArray<AuthenticationProviderInformation>;
}
/**
@ -91,6 +106,13 @@ declare module 'vscode' {
clearSessionPreference?: boolean;
}
export interface AuthenticationProviderAuthenticationSessionsChangeEvent extends AuthenticationSessionsChangeEvent {
/**
* The [authenticationProvider](#AuthenticationProvider) that has had its sessions change.
*/
readonly provider: AuthenticationProviderInformation;
}
/**
* An [event](#Event) which fires when an [AuthenticationSession](#AuthenticationSession) is added, removed, or changed.
*/
@ -182,10 +204,16 @@ declare module 'vscode' {
export function getProviderIds(): Thenable<ReadonlyArray<string>>;
/**
* @deprecated
* An array of the ids of authentication providers that are currently registered.
*/
export const providerIds: ReadonlyArray<string>;
/**
* An array of the information of authentication providers that are currently registered.
*/
export const providers: ReadonlyArray<AuthenticationProviderInformation>;
/**
* Returns whether a provider has any sessions matching the requested scopes. This request
* is transparent to the user, no UI is shown. Rejects if a provider with providerId is not
@ -235,7 +263,7 @@ declare module 'vscode' {
* within a session has changed for a provider. Fires with the ids of the providers
* that have had session data change.
*/
export const onDidChangeSessions: Event<{ [providerId: string]: AuthenticationSessionsChangeEvent; }>;
export const onDidChangeSessions: Event<AuthenticationProviderAuthenticationSessionsChangeEvent>;
}
//#endregion

View file

@ -219,15 +219,15 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostAuthentication);
this._register(this.authenticationService.onDidChangeSessions(e => {
this._proxy.$onDidChangeAuthenticationSessions(e.providerId, e.event);
this._proxy.$onDidChangeAuthenticationSessions(e.providerId, e.label, e.event);
}));
this._register(this.authenticationService.onDidRegisterAuthenticationProvider(providerId => {
this._proxy.$onDidChangeAuthenticationProviders([providerId], []);
this._register(this.authenticationService.onDidRegisterAuthenticationProvider(info => {
this._proxy.$onDidChangeAuthenticationProviders([info], []);
}));
this._register(this.authenticationService.onDidUnregisterAuthenticationProvider(providerId => {
this._proxy.$onDidChangeAuthenticationProviders([], [providerId]);
this._register(this.authenticationService.onDidUnregisterAuthenticationProvider(info => {
this._proxy.$onDidChangeAuthenticationProviders([], [info]);
}));
}

View file

@ -202,6 +202,9 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
get providerIds(): string[] {
return extHostAuthentication.providerIds;
},
get providers(): ReadonlyArray<vscode.AuthenticationProviderInformation> {
return extHostAuthentication.providers;
},
hasSessions(providerId: string, scopes: string[]): Thenable<boolean> {
return extHostAuthentication.hasSessions(providerId, scopes);
},
@ -211,7 +214,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
logout(providerId: string, sessionId: string): Thenable<void> {
return extHostAuthentication.logout(providerId, sessionId);
},
get onDidChangeSessions(): Event<{ [providerId: string]: vscode.AuthenticationSessionsChangeEvent }> {
get onDidChangeSessions(): Event<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent> {
return extHostAuthentication.onDidChangeSessions;
},
};

View file

@ -1025,8 +1025,8 @@ export interface ExtHostAuthenticationShape {
$getSessionAccessToken(id: string, sessionId: string): Promise<string>;
$login(id: string, scopes: string[]): Promise<modes.AuthenticationSession>;
$logout(id: string, sessionId: string): Promise<void>;
$onDidChangeAuthenticationSessions(providerId: string, event: modes.AuthenticationSessionsChangeEvent): Promise<void>;
$onDidChangeAuthenticationProviders(added: string[], removed: string[]): Promise<void>;
$onDidChangeAuthenticationSessions(id: string, label: string, event: modes.AuthenticationSessionsChangeEvent): Promise<void>;
$onDidChangeAuthenticationProviders(added: modes.AuthenticationProviderInformation[], removed: modes.AuthenticationProviderInformation[]): Promise<void>;
}
export interface ExtHostSearchShape {

View file

@ -16,11 +16,13 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
private _providerIds: string[] = [];
private _providers: vscode.AuthenticationProviderInformation[] = [];
private _onDidChangeAuthenticationProviders = new Emitter<vscode.AuthenticationProvidersChangeEvent>();
readonly onDidChangeAuthenticationProviders: Event<vscode.AuthenticationProvidersChangeEvent> = this._onDidChangeAuthenticationProviders.event;
private _onDidChangeSessions = new Emitter<{ [providerId: string]: vscode.AuthenticationSessionsChangeEvent }>();
readonly onDidChangeSessions: Event<{ [providerId: string]: vscode.AuthenticationSessionsChangeEvent }> = this._onDidChangeSessions.event;
private _onDidChangeSessions = new Emitter<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>();
readonly onDidChangeSessions: Event<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent> = this._onDidChangeSessions.event;
constructor(mainContext: IMainContext) {
this._proxy = mainContext.getProxy(MainContext.MainThreadAuthentication);
@ -34,6 +36,10 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
return this._providerIds;
}
get providers(): ReadonlyArray<vscode.AuthenticationProviderInformation> {
return Object.freeze(this._providers);
}
private async resolveSessions(providerId: string): Promise<ReadonlyArray<modes.AuthenticationSession>> {
const provider = this._authenticationProviders.get(providerId);
@ -116,6 +122,13 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
this._providerIds.push(provider.id);
}
if (!this._providers.find(p => p.id === provider.id)) {
this._providers.push({
id: provider.id,
label: provider.label
});
}
const listener = provider.onDidChangeSessions(e => {
this._proxy.$sendDidChangeSessions(provider.id, e);
});
@ -129,6 +142,12 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
if (index > -1) {
this._providerIds.splice(index);
}
const i = this._providers.findIndex(p => p.id === provider.id);
if (i > -1) {
this._providers.splice(i);
}
this._proxy.$unregisterAuthenticationProvider(provider.id);
});
}
@ -175,22 +194,22 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
throw new Error(`Unable to find authentication provider with handle: ${providerId}`);
}
$onDidChangeAuthenticationSessions(providerId: string, event: modes.AuthenticationSessionsChangeEvent) {
this._onDidChangeSessions.fire({ [providerId]: event });
$onDidChangeAuthenticationSessions(id: string, label: string, event: modes.AuthenticationSessionsChangeEvent) {
this._onDidChangeSessions.fire({ provider: { id, label }, ...event });
return Promise.resolve();
}
$onDidChangeAuthenticationProviders(added: string[], removed: string[]) {
$onDidChangeAuthenticationProviders(added: modes.AuthenticationProviderInformation[], removed: modes.AuthenticationProviderInformation[]) {
added.forEach(id => {
if (!this._providerIds.includes(id)) {
this._providerIds.push(id);
if (!this._providers.includes(id)) {
this._providers.push(id);
}
});
removed.forEach(id => {
const index = this._providerIds.findIndex(provider => provider === id);
removed.forEach(p => {
const index = this._providers.findIndex(provider => provider.id === p.id);
if (index > -1) {
this._providerIds.splice(index);
this._providers.splice(index);
}
});

View file

@ -6,7 +6,7 @@
import * as nls from 'vs/nls';
import { Emitter, Event } from 'vs/base/common/event';
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
import { AuthenticationSession, AuthenticationSessionsChangeEvent } from 'vs/editor/common/modes';
import { AuthenticationSession, AuthenticationSessionsChangeEvent, AuthenticationProviderInformation } from 'vs/editor/common/modes';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { MainThreadAuthenticationProvider } from 'vs/workbench/api/browser/mainThreadAuthentication';
@ -28,10 +28,10 @@ export interface IAuthenticationService {
requestNewSession(id: string, scopes: string[], extensionId: string, extensionName: string): void;
sessionsUpdate(providerId: string, event: AuthenticationSessionsChangeEvent): void;
readonly onDidRegisterAuthenticationProvider: Event<string>;
readonly onDidUnregisterAuthenticationProvider: Event<string>;
readonly onDidRegisterAuthenticationProvider: Event<AuthenticationProviderInformation>;
readonly onDidUnregisterAuthenticationProvider: Event<AuthenticationProviderInformation>;
readonly onDidChangeSessions: Event<{ providerId: string, event: AuthenticationSessionsChangeEvent }>;
readonly onDidChangeSessions: Event<{ providerId: string, label: string, event: AuthenticationSessionsChangeEvent }>;
getSessions(providerId: string): Promise<ReadonlyArray<AuthenticationSession>>;
getLabel(providerId: string): string;
supportsMultipleAccounts(providerId: string): boolean;
@ -77,14 +77,14 @@ export class AuthenticationService extends Disposable implements IAuthentication
private _authenticationProviders: Map<string, MainThreadAuthenticationProvider> = new Map<string, MainThreadAuthenticationProvider>();
private _onDidRegisterAuthenticationProvider: Emitter<string> = this._register(new Emitter<string>());
readonly onDidRegisterAuthenticationProvider: Event<string> = this._onDidRegisterAuthenticationProvider.event;
private _onDidRegisterAuthenticationProvider: Emitter<AuthenticationProviderInformation> = this._register(new Emitter<AuthenticationProviderInformation>());
readonly onDidRegisterAuthenticationProvider: Event<AuthenticationProviderInformation> = this._onDidRegisterAuthenticationProvider.event;
private _onDidUnregisterAuthenticationProvider: Emitter<string> = this._register(new Emitter<string>());
readonly onDidUnregisterAuthenticationProvider: Event<string> = this._onDidUnregisterAuthenticationProvider.event;
private _onDidUnregisterAuthenticationProvider: Emitter<AuthenticationProviderInformation> = this._register(new Emitter<AuthenticationProviderInformation>());
readonly onDidUnregisterAuthenticationProvider: Event<AuthenticationProviderInformation> = this._onDidUnregisterAuthenticationProvider.event;
private _onDidChangeSessions: Emitter<{ providerId: string, event: AuthenticationSessionsChangeEvent }> = this._register(new Emitter<{ providerId: string, event: AuthenticationSessionsChangeEvent }>());
readonly onDidChangeSessions: Event<{ providerId: string, event: AuthenticationSessionsChangeEvent }> = this._onDidChangeSessions.event;
private _onDidChangeSessions: Emitter<{ providerId: string, label: string, event: AuthenticationSessionsChangeEvent }> = this._register(new Emitter<{ providerId: string, label: string, event: AuthenticationSessionsChangeEvent }>());
readonly onDidChangeSessions: Event<{ providerId: string, label: string, event: AuthenticationSessionsChangeEvent }> = this._onDidChangeSessions.event;
constructor(@IActivityService private readonly activityService: IActivityService) {
super();
@ -134,7 +134,7 @@ export class AuthenticationService extends Disposable implements IAuthentication
registerAuthenticationProvider(id: string, authenticationProvider: MainThreadAuthenticationProvider): void {
this._authenticationProviders.set(id, authenticationProvider);
this._onDidRegisterAuthenticationProvider.fire(id);
this._onDidRegisterAuthenticationProvider.fire({ id, label: authenticationProvider.label });
if (this._placeholderMenuItem) {
this._placeholderMenuItem.dispose();
@ -149,7 +149,7 @@ export class AuthenticationService extends Disposable implements IAuthentication
if (provider) {
provider.dispose();
this._authenticationProviders.delete(id);
this._onDidUnregisterAuthenticationProvider.fire(id);
this._onDidUnregisterAuthenticationProvider.fire({ id, label: provider.label });
this.updateAccountsMenuItem();
}
@ -165,9 +165,9 @@ export class AuthenticationService extends Disposable implements IAuthentication
}
async sessionsUpdate(id: string, event: AuthenticationSessionsChangeEvent): Promise<void> {
this._onDidChangeSessions.fire({ providerId: id, event: event });
const provider = this._authenticationProviders.get(id);
if (provider) {
this._onDidChangeSessions.fire({ providerId: id, label: provider.label, event: event });
await provider.updateSessionItems(event);
this.updateAccountsMenuItem();

View file

@ -137,7 +137,7 @@ export class UserDataSyncWorkbenchService extends Disposable implements IUserDat
Event.any(
this.authenticationService.onDidRegisterAuthenticationProvider,
this.authenticationService.onDidUnregisterAuthenticationProvider,
), authenticationProviderId => this.isSupportedAuthenticationProviderId(authenticationProviderId)),
), info => this.isSupportedAuthenticationProviderId(info.id)),
Event.filter(this.userDataSyncAccountService.onTokenFailed, isSuccessive => !isSuccessive))
(() => this.update()));