mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-24 18:49:00 +01:00
Move settings sync auth into built in extension
This commit is contained in:
@@ -59,6 +59,7 @@ import './mainThreadComments';
|
||||
import './mainThreadTask';
|
||||
import './mainThreadLabelService';
|
||||
import './mainThreadTunnelService';
|
||||
import './mainThreadAuthentication';
|
||||
import 'vs/workbench/api/common/apiCommands';
|
||||
|
||||
export class ExtensionPoints implements IWorkbenchContribution {
|
||||
|
||||
69
src/vs/workbench/api/browser/mainThreadAuthentication.ts
Normal file
69
src/vs/workbench/api/browser/mainThreadAuthentication.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import * as modes from 'vs/editor/common/modes';
|
||||
import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
|
||||
import { IAuthenticationService } from 'vs/workbench/services/authentication/browser/authenticationService';
|
||||
import { ExtHostAuthenticationShape, ExtHostContext, IExtHostContext, MainContext, MainThreadAuthenticationShape } from '../common/extHost.protocol';
|
||||
|
||||
export class MainThreadAuthenticationProvider {
|
||||
public readonly handle: number;
|
||||
constructor(
|
||||
private readonly _proxy: ExtHostAuthenticationShape,
|
||||
public readonly id: string,
|
||||
handle: number
|
||||
) {
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
accounts(): Promise<ReadonlyArray<modes.Account>> {
|
||||
return this._proxy.$accounts(this.handle);
|
||||
}
|
||||
|
||||
login(): Promise<modes.Account> {
|
||||
return this._proxy.$login(this.handle);
|
||||
}
|
||||
|
||||
logout(accountId: string): Promise<void> {
|
||||
return this._proxy.$logout(this.handle, accountId);
|
||||
}
|
||||
}
|
||||
|
||||
@extHostNamedCustomer(MainContext.MainThreadAuthentication)
|
||||
export class MainThreadAuthentication extends Disposable implements MainThreadAuthenticationShape {
|
||||
private readonly _proxy: ExtHostAuthenticationShape;
|
||||
private _handlers = new Map<number, string>();
|
||||
|
||||
constructor(
|
||||
extHostContext: IExtHostContext,
|
||||
@IAuthenticationService private readonly authenticationService: IAuthenticationService,
|
||||
) {
|
||||
super();
|
||||
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostAuthentication);
|
||||
}
|
||||
|
||||
$registerAuthenticationProvider(handle: number, id: string): void {
|
||||
const provider = new MainThreadAuthenticationProvider(this._proxy, id, handle);
|
||||
this._handlers.set(handle, id);
|
||||
this.authenticationService.registerAuthenticationProvider(id, provider);
|
||||
}
|
||||
|
||||
$unregisterAuthenticationProvider(handle: number): void {
|
||||
const id = this._handlers.get(handle);
|
||||
if (!id) {
|
||||
throw new Error(`No authentication provider registered with id ${id}`);
|
||||
}
|
||||
|
||||
this.authenticationService.unregisterAuthenticationProvider(id);
|
||||
}
|
||||
|
||||
$onDidChangeAccounts(handle: number, accounts: ReadonlyArray<modes.Account>) {
|
||||
const id = this._handlers.get(handle);
|
||||
if (id) {
|
||||
this.authenticationService.accountsUpdate(id, accounts);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,6 +69,7 @@ import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService';
|
||||
import { IExtHostInitDataService } from 'vs/workbench/api/common/extHostInitDataService';
|
||||
import { ExtHostTheming } from 'vs/workbench/api/common/extHostTheming';
|
||||
import { IExtHostTunnelService } from 'vs/workbench/api/common/extHostTunnelService';
|
||||
import { ExtHostAuthentication } from 'vs/workbench/api/common/extHostAuthentication';
|
||||
|
||||
export interface IExtensionApiFactory {
|
||||
(extension: IExtensionDescription, registry: ExtensionDescriptionRegistry, configProvider: ExtHostConfigProvider): typeof vscode;
|
||||
@@ -128,6 +129,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
|
||||
const extHostProgress = rpcProtocol.set(ExtHostContext.ExtHostProgress, new ExtHostProgress(rpcProtocol.getProxy(MainContext.MainThreadProgress)));
|
||||
const extHostLabelService = rpcProtocol.set(ExtHostContext.ExtHostLabelService, new ExtHostLabelService(rpcProtocol));
|
||||
const extHostTheming = rpcProtocol.set(ExtHostContext.ExtHostTheming, new ExtHostTheming(rpcProtocol));
|
||||
const extHostAuthentication = rpcProtocol.set(ExtHostContext.ExtHostAuthentication, new ExtHostAuthentication(rpcProtocol));
|
||||
|
||||
// Check that no named customers are missing
|
||||
const expected: ProxyIdentifier<any>[] = values(ExtHostContext);
|
||||
@@ -175,6 +177,11 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
|
||||
};
|
||||
})();
|
||||
|
||||
const authentication: typeof vscode.authentication = {
|
||||
registerAuthenticationProvider(provider: vscode.AuthenticationProvider): vscode.Disposable {
|
||||
return extHostAuthentication.registerAuthenticationProvider(provider);
|
||||
}
|
||||
};
|
||||
|
||||
// namespace: commands
|
||||
const commands: typeof vscode.commands = {
|
||||
@@ -830,6 +837,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
|
||||
return <typeof vscode>{
|
||||
version: initData.version,
|
||||
// namespaces
|
||||
authentication,
|
||||
commands,
|
||||
debug,
|
||||
env,
|
||||
|
||||
@@ -147,6 +147,12 @@ export interface MainThreadCommentsShape extends IDisposable {
|
||||
$onDidCommentThreadsChange(handle: number, event: modes.CommentThreadChangedEvent): void;
|
||||
}
|
||||
|
||||
export interface MainThreadAuthenticationShape extends IDisposable {
|
||||
$registerAuthenticationProvider(handle: number, id: string): void;
|
||||
$unregisterAuthenticationProvider(handle: number): void;
|
||||
$onDidChangeAccounts(handle: number, accounts: ReadonlyArray<modes.Account>): void;
|
||||
}
|
||||
|
||||
export interface MainThreadConfigurationShape extends IDisposable {
|
||||
$updateConfigurationOption(target: ConfigurationTarget | null, key: string, value: any, overrides: IConfigurationOverrides | undefined, scopeToLanguage: boolean | undefined): Promise<void>;
|
||||
$removeConfigurationOption(target: ConfigurationTarget | null, key: string, overrides: IConfigurationOverrides | undefined, scopeToLanguage: boolean | undefined): Promise<void>;
|
||||
@@ -891,6 +897,13 @@ export interface ExtHostLabelServiceShape {
|
||||
$registerResourceLabelFormatter(formatter: ResourceLabelFormatter): IDisposable;
|
||||
}
|
||||
|
||||
export interface ExtHostAuthenticationShape {
|
||||
$accounts(handle: number): Promise<ReadonlyArray<modes.Account>>;
|
||||
$login(handle: number): Promise<modes.Account>;
|
||||
$logout(handle: number, accountId: string): Promise<void>;
|
||||
// TODO rmacfarlane
|
||||
}
|
||||
|
||||
export interface ExtHostSearchShape {
|
||||
$provideFileSearchResults(handle: number, session: number, query: search.IRawQuery, token: CancellationToken): Promise<search.ISearchCompleteStats>;
|
||||
$provideTextSearchResults(handle: number, session: number, query: search.IRawTextQuery, token: CancellationToken): Promise<search.ISearchCompleteStats>;
|
||||
@@ -1412,6 +1425,7 @@ export interface ExtHostTunnelServiceShape {
|
||||
// --- proxy identifiers
|
||||
|
||||
export const MainContext = {
|
||||
MainThreadAuthentication: createMainId<MainThreadAuthenticationShape>('MainThreadAuthentication'),
|
||||
MainThreadClipboard: createMainId<MainThreadClipboardShape>('MainThreadClipboard'),
|
||||
MainThreadCommands: createMainId<MainThreadCommandsShape>('MainThreadCommands'),
|
||||
MainThreadComments: createMainId<MainThreadCommentsShape>('MainThreadComments'),
|
||||
@@ -1487,5 +1501,6 @@ export const ExtHostContext = {
|
||||
ExtHostOutputService: createMainId<ExtHostOutputServiceShape>('ExtHostOutputService'),
|
||||
ExtHostLabelService: createMainId<ExtHostLabelServiceShape>('ExtHostLabelService'),
|
||||
ExtHostTheming: createMainId<ExtHostThemingShape>('ExtHostTheming'),
|
||||
ExtHostTunnelService: createMainId<ExtHostTunnelServiceShape>('ExtHostTunnelService')
|
||||
ExtHostTunnelService: createMainId<ExtHostTunnelServiceShape>('ExtHostTunnelService'),
|
||||
ExtHostAuthentication: createMainId<ExtHostAuthenticationShape>('ExtHostAuthentication')
|
||||
};
|
||||
|
||||
83
src/vs/workbench/api/common/extHostAuthentication.ts
Normal file
83
src/vs/workbench/api/common/extHostAuthentication.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import * as modes from 'vs/editor/common/modes';
|
||||
import { Emitter, Event } from 'vs/base/common/event';
|
||||
import { IMainContext, MainContext, MainThreadAuthenticationShape, ExtHostAuthenticationShape } from 'vs/workbench/api/common/extHost.protocol';
|
||||
import { IDisposable } from 'vs/base/common/lifecycle';
|
||||
|
||||
export class ExtHostAuthenticationProvider implements IDisposable {
|
||||
constructor(private _provider: vscode.AuthenticationProvider,
|
||||
private readonly _handle: number,
|
||||
private _proxy: MainThreadAuthenticationShape) {
|
||||
this._provider.onDidChangeAccounts(x => this._proxy.$onDidChangeAccounts(this._handle, this._provider.accounts));
|
||||
}
|
||||
|
||||
get accounts(): ReadonlyArray<vscode.Account> {
|
||||
return this._provider.accounts;
|
||||
}
|
||||
|
||||
login(): Promise<vscode.Account> {
|
||||
return this._provider.login();
|
||||
}
|
||||
|
||||
logout(accountId: string): Promise<void> {
|
||||
return this._provider.logout(accountId);
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this._proxy.$unregisterAuthenticationProvider(this._handle);
|
||||
}
|
||||
}
|
||||
|
||||
export class ExtHostAuthentication implements ExtHostAuthenticationShape {
|
||||
public static _handlePool: number = 0;
|
||||
private _proxy: MainThreadAuthenticationShape;
|
||||
private _authenticationProviders: Map<number, ExtHostAuthenticationProvider> = new Map<number, ExtHostAuthenticationProvider>();
|
||||
|
||||
constructor(mainContext: IMainContext) {
|
||||
this._proxy = mainContext.getProxy(MainContext.MainThreadAuthentication);
|
||||
}
|
||||
|
||||
private readonly _onDidRefreshToken = new Emitter<any>();
|
||||
readonly onDidRefreshToken: Event<any> = this._onDidRefreshToken.event;
|
||||
|
||||
registerAuthenticationProvider(provider: vscode.AuthenticationProvider) {
|
||||
const handle = ExtHostAuthentication._handlePool++;
|
||||
const authenticationProvider = new ExtHostAuthenticationProvider(provider, handle, this._proxy);
|
||||
this._authenticationProviders.set(handle, authenticationProvider);
|
||||
|
||||
this._proxy.$registerAuthenticationProvider(handle, provider.id);
|
||||
return authenticationProvider;
|
||||
}
|
||||
|
||||
$accounts(handle: number): Promise<ReadonlyArray<modes.Account>> {
|
||||
const authProvider = this._authenticationProviders.get(handle);
|
||||
if (authProvider) {
|
||||
return Promise.resolve(authProvider.accounts);
|
||||
}
|
||||
|
||||
throw new Error(`Unable to find authentication provider with handle: ${handle}`);
|
||||
}
|
||||
|
||||
$login(handle: number): Promise<modes.Account> {
|
||||
const authProvider = this._authenticationProviders.get(handle);
|
||||
if (authProvider) {
|
||||
return authProvider.login();
|
||||
}
|
||||
|
||||
throw new Error(`Unable to find authentication provider with handle: ${handle}`);
|
||||
}
|
||||
|
||||
$logout(handle: number, accountId: string): Promise<void> {
|
||||
const authProvider = this._authenticationProviders.get(handle);
|
||||
if (authProvider) {
|
||||
return authProvider.logout(accountId);
|
||||
}
|
||||
|
||||
throw new Error(`Unable to find authentication provider with handle: ${handle}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user