Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade authentication API to latest vscode #9498

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 57 additions & 15 deletions packages/core/src/browser/authentication-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ import { ACCOUNTS_MENU, ACCOUNTS_SUBMENU, MenuModelRegistry } from '../common/me
import { Command, CommandRegistry } from '../common/command';
import { DisposableCollection } from '../common/disposable';

export interface AuthenticationSessionsChangeEvent {
added: ReadonlyArray<string>;
removed: ReadonlyArray<string>;
changed: ReadonlyArray<string>;
}

export interface AuthenticationSession {
id: string;
accessToken: string;
Expand All @@ -49,6 +43,26 @@ export interface AuthenticationProviderInformation {
label: string;
}

/**
* An [event](#Event) which fires when an [AuthenticationSession](#AuthenticationSession) is added, removed, or changed.
*/
export interface AuthenticationProviderAuthenticationSessionsChangeEvent {
/**
* The [AuthenticationSession](#AuthenticationSession)s of the [AuthenticationProvider](#AuthentiationProvider) that have been added.
*/
readonly added: ReadonlyArray<AuthenticationSession>;

/**
* The [AuthenticationSession](#AuthenticationSession)s of the [AuthenticationProvider](#AuthentiationProvider) that have been removed.
*/
readonly removed: ReadonlyArray<AuthenticationSession>;

/**
* The [AuthenticationSession](#AuthenticationSession)s of the [AuthenticationProvider](#AuthentiationProvider) that have been changed.
*/
readonly changed: ReadonlyArray<AuthenticationSession>;
}

export interface SessionRequest {
disposables: Disposable[];
requestingExtensionIds: string[];
Expand All @@ -71,11 +85,38 @@ export interface AuthenticationProvider {

getSessions(): Promise<ReadonlyArray<AuthenticationSession>>;

updateSessionItems(event: AuthenticationSessionsChangeEvent): Promise<void>;
updateSessionItems(event: AuthenticationProviderAuthenticationSessionsChangeEvent): Promise<void>;

login(scopes: string[]): Promise<AuthenticationSession>;

logout(sessionId: string): Promise<void>;

/**
* An [event](#Event) which fires when the array of sessions has changed, or data
* within a session has changed.
*/
readonly onDidChangeSessions: Omit<Event<AuthenticationProviderAuthenticationSessionsChangeEvent>, 'maxListeners'>;

/**
* 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.
*/
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.
*/
createSession(scopes: string[]): Thenable<AuthenticationSession>;

/**
* Removes the session corresponding to session id.
* @param sessionId The id of the session to remove.
*/
removeSession(sessionId: string): Thenable<void>;
}
export const AuthenticationService = Symbol('AuthenticationService');

Expand All @@ -85,12 +126,12 @@ export interface AuthenticationService {
registerAuthenticationProvider(id: string, provider: AuthenticationProvider): void;
unregisterAuthenticationProvider(id: string): void;
requestNewSession(id: string, scopes: string[], extensionId: string, extensionName: string): void;
updateSessions(providerId: string, event: AuthenticationSessionsChangeEvent): void;
updateSessions(providerId: string, event: AuthenticationProviderAuthenticationSessionsChangeEvent): void;

readonly onDidRegisterAuthenticationProvider: Event<AuthenticationProviderInformation>;
readonly onDidUnregisterAuthenticationProvider: Event<AuthenticationProviderInformation>;

readonly onDidChangeSessions: Event<{ providerId: string, label: string, event: AuthenticationSessionsChangeEvent }>;
readonly onDidChangeSessions: Event<{ providerId: string, label: string, event: AuthenticationProviderAuthenticationSessionsChangeEvent }>;
getSessions(providerId: string): Promise<ReadonlyArray<AuthenticationSession>>;
getLabel(providerId: string): string;
supportsMultipleAccounts(providerId: string): boolean;
Expand All @@ -114,9 +155,10 @@ export class AuthenticationServiceImpl implements AuthenticationService {
private onDidUnregisterAuthenticationProviderEmitter: Emitter<AuthenticationProviderInformation> = new Emitter<AuthenticationProviderInformation>();
readonly onDidUnregisterAuthenticationProvider: Event<AuthenticationProviderInformation> = this.onDidUnregisterAuthenticationProviderEmitter.event;

private onDidChangeSessionsEmitter: Emitter<{ providerId: string, label: string, event: AuthenticationSessionsChangeEvent }> =
new Emitter<{ providerId: string, label: string, event: AuthenticationSessionsChangeEvent }>();
readonly onDidChangeSessions: Event<{ providerId: string, label: string, event: AuthenticationSessionsChangeEvent }> = this.onDidChangeSessionsEmitter.event;
private onDidChangeSessionsEmitter: Emitter<{ providerId: string, label: string, event: AuthenticationProviderAuthenticationSessionsChangeEvent }> =
new Emitter<{ providerId: string, label: string, event: AuthenticationProviderAuthenticationSessionsChangeEvent }>();
readonly onDidChangeSessions: Event<{ providerId: string, label: string, event: AuthenticationProviderAuthenticationSessionsChangeEvent }> =
this.onDidChangeSessionsEmitter.event;

@inject(MenuModelRegistry) protected readonly menus: MenuModelRegistry;
@inject(CommandRegistry) protected readonly commands: CommandRegistry;
Expand Down Expand Up @@ -152,10 +194,10 @@ export class AuthenticationServiceImpl implements AuthenticationService {
}
if (e.event.removed.length > 0) {
e.event.removed.forEach(removed => {
const toDispose = disposableMap.get(removed);
const toDispose = disposableMap.get(removed.id);
if (toDispose) {
toDispose.dispose();
disposableMap.delete(removed);
disposableMap.delete(removed.id);
}
});
}
Expand Down Expand Up @@ -220,7 +262,7 @@ export class AuthenticationServiceImpl implements AuthenticationService {
}
}

async updateSessions(id: string, event: AuthenticationSessionsChangeEvent): Promise<void> {
async updateSessions(id: string, event: AuthenticationProviderAuthenticationSessionsChangeEvent): Promise<void> {
const provider = this.authenticationProviders.get(id);
if (provider) {
await provider.updateSessionItems(event);
Expand Down
25 changes: 15 additions & 10 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ import {
CallHierarchyReference,
SearchInWorkspaceResult,
AuthenticationSession,
AuthenticationSessionsChangeEvent,
AuthenticationProviderInformation,
Comment,
CommentOptions,
Expand All @@ -76,7 +75,12 @@ import {
} from './plugin-api-rpc-model';
import { ExtPluginApi } from './plugin-ext-api-contribution';
import { KeysToAnyValues, KeysToKeysToAnyValue } from './types';
import { CancellationToken, Progress, ProgressOptions } from '@theia/plugin';
import {
AuthenticationProviderAuthenticationSessionsChangeEvent,
CancellationToken,
Progress,
ProgressOptions,
} from '@theia/plugin';
import { DebuggerDescription } from '@theia/debug/lib/common/debug-service';
import { DebugProtocol } from 'vscode-debugprotocol';
import { SymbolInformation } from '@theia/core/shared/vscode-languageserver-types';
Expand Down Expand Up @@ -1791,21 +1795,22 @@ export interface TasksMain {
}

export interface AuthenticationExt {
$getSessions(id: string): Promise<ReadonlyArray<AuthenticationSession>>;
$login(id: string, scopes: string[]): Promise<AuthenticationSession>;
$logout(id: string, sessionId: string): Promise<void>;
$onDidChangeAuthenticationSessions(id: string, label: string, event: AuthenticationSessionsChangeEvent): Promise<void>;
$getSessions(id: string, scopes?: string[]): Promise<ReadonlyArray<AuthenticationSession>>;
$createSession(id: string, scopes: string[]): Promise<AuthenticationSession>;
$removeSession(id: string, sessionId: string): Promise<void>;
$onDidChangeAuthenticationSessions(id: string, label: string): Promise<void>;
$onDidChangeAuthenticationProviders(added: AuthenticationProviderInformation[], removed: AuthenticationProviderInformation[]): Promise<void>;
$setProviders(providers: AuthenticationProviderInformation[]): Promise<void>;
}

export interface AuthenticationMain {
$registerAuthenticationProvider(id: string, label: string, supportsMultipleAccounts: boolean): void;
$unregisterAuthenticationProvider(id: string): void;
$getProviderIds(): Promise<string[]>;
$updateSessions(providerId: string, event: AuthenticationSessionsChangeEvent): void;
$ensureProvider(id: string): Promise<void>;
$sendDidChangeSessions(providerId: string, event: AuthenticationProviderAuthenticationSessionsChangeEvent): void;
$getSession(providerId: string, scopes: string[], extensionId: string, extensionName: string,
options: { createIfNone?: boolean, clearSessionPreference?: boolean }): Promise<theia.AuthenticationSession | undefined>;
$logout(providerId: string, sessionId: string): Promise<void>;
options: { createIfNone?: boolean, clearSessionPreference?: boolean }): Promise<AuthenticationSession | undefined>;
$removeSession(providerId: string, sessionId: string): Promise<void>;
}

export interface RawColorInfo {
Expand Down
47 changes: 37 additions & 10 deletions packages/plugin-ext/src/main/browser/authentication-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// code copied and modified from https://github.com/microsoft/vscode/blob/1.47.3/src/vs/workbench/api/browser/mainThreadAuthentication.ts

import { interfaces } from '@theia/core/shared/inversify';
import { AuthenticationExt, AuthenticationMain, MAIN_RPC_CONTEXT } from '../../common/plugin-api-rpc';
import {AuthenticationExt, AuthenticationMain, PluginManagerExt, MAIN_RPC_CONTEXT} from '../../common/plugin-api-rpc';
import { RPCProtocol } from '../../common/rpc-protocol';
import { MessageService } from '@theia/core/lib/common/message-service';
import { StorageService } from '@theia/core/lib/browser';
Expand All @@ -32,26 +32,30 @@ import {
} from '@theia/core/lib/browser/authentication-service';
import { QuickPickService } from '@theia/core/lib/common/quick-pick-service';
import {
AuthenticationSession,
AuthenticationSessionsChangeEvent
AuthenticationSession
} from '../../common/plugin-api-rpc-model';
import {AuthenticationProviderAuthenticationSessionsChangeEvent, Event} from '@theia/plugin';
import { QuickPickValue } from '@theia/core/lib/browser/quick-input/quick-input-service';

export function getAuthenticationProviderActivationEvent(id: string): string { return `onAuthenticationRequest:${id}`; }

export class AuthenticationMainImpl implements AuthenticationMain {
private readonly proxy: AuthenticationExt;
private readonly messageService: MessageService;
private readonly storageService: StorageService;
private readonly authenticationService: AuthenticationService;
private readonly quickPickService: QuickPickService;
private readonly extensionService: PluginManagerExt;
constructor(rpc: RPCProtocol, container: interfaces.Container) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.AUTHENTICATION_EXT);
this.messageService = container.get(MessageService);
this.storageService = container.get(StorageService);
this.authenticationService = container.get(AuthenticationService);
this.quickPickService = container.get(QuickPickService);
this.extensionService = rpc.getProxy(MAIN_RPC_CONTEXT.HOSTED_PLUGIN_MANAGER_EXT);

this.authenticationService.onDidChangeSessions(e => {
this.proxy.$onDidChangeAuthenticationSessions(e.providerId, e.label, e.event);
this.proxy.$onDidChangeAuthenticationSessions(e.providerId, e.label);
});
this.authenticationService.onDidRegisterAuthenticationProvider(info => {
this.proxy.$onDidChangeAuthenticationProviders([info], []);
Expand All @@ -73,7 +77,7 @@ export class AuthenticationMainImpl implements AuthenticationMain {
this.authenticationService.unregisterAuthenticationProvider(id);
}

async $updateSessions(id: string, event: AuthenticationSessionsChangeEvent): Promise<void> {
async $updateSessions(id: string, event: AuthenticationProviderAuthenticationSessionsChangeEvent): Promise<void> {
this.authenticationService.updateSessions(id, event);
}

Expand Down Expand Up @@ -210,6 +214,18 @@ export class AuthenticationMainImpl implements AuthenticationMain {

this.storageService.setData(`authentication-session-${extensionName}-${providerId}`, sessionId);
}

$ensureProvider(id: string): Promise<void> {
return this.extensionService.$activateByEvent(getAuthenticationProviderActivationEvent(id));
}

$removeSession(providerId: string, sessionId: string): Promise<void> {
return this.authenticationService.logout(providerId, sessionId);
}

$sendDidChangeSessions(providerId: string, event: AuthenticationProviderAuthenticationSessionsChangeEvent): void {
this.authenticationService.updateSessions(providerId, event);
}
}

async function addAccountUsage(storageService: StorageService, providerId: string, accountName: string, extensionId: string, extensionName: string): Promise<void> {
Expand Down Expand Up @@ -285,12 +301,13 @@ export class AuthenticationProviderImpl implements AuthenticationProvider {
return this.proxy.$getSessions(this.id);
}

async updateSessionItems(event: AuthenticationSessionsChangeEvent): Promise<void> {
async updateSessionItems(event: AuthenticationProviderAuthenticationSessionsChangeEvent): Promise<void> {
const { added, removed } = event;
const session = await this.proxy.$getSessions(this.id);
const addedSessions = session.filter(s => added.some(id => id === s.id));
const addedSessions = session.filter(s => added.some(addedSession => addedSession.id === s.id));

removed.forEach(sessionId => {
removed.forEach(removedSession => {
const sessionId = removedSession.id;
const accountName = this.sessions.get(sessionId);
if (accountName) {
this.sessions.delete(sessionId);
Expand All @@ -308,13 +325,23 @@ export class AuthenticationProviderImpl implements AuthenticationProvider {
}

login(scopes: string[]): Promise<AuthenticationSession> {
return this.proxy.$login(this.id, scopes);
return this.proxy.$createSession(this.id, scopes);
}

async logout(sessionId: string): Promise<void> {
await this.proxy.$logout(this.id, sessionId);
await this.proxy.$removeSession(this.id, sessionId);
this.messageService.info('Successfully signed out.');
}

readonly onDidChangeSessions: Event<AuthenticationProviderAuthenticationSessionsChangeEvent>;

createSession(scopes: string[]): Thenable<AuthenticationSession> {
return this.login(scopes);
}

removeSession(sessionId: string): Thenable<void> {
return this.logout(sessionId);
}
}

async function readAccountUsages(storageService: StorageService, providerId: string, accountName: string): Promise<AccountUsage[]> {
Expand Down
Loading