-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply vscode Authentication plugin API
Signed-off-by: Igor Vinokur <ivinokur@redhat.com>
- Loading branch information
Showing
9 changed files
with
1,053 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2020 Red Hat, Inc. and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
// code copied and modified from https://github.com/microsoft/vscode/blob/1.47.3/src/vs/workbench/services/authentication/browser/authenticationService.ts | ||
|
||
import { injectable } from 'inversify'; | ||
import { Emitter, Event } from '../common/event'; | ||
import { StorageService } from '../browser/storage-service'; | ||
|
||
export interface AuthenticationSession { | ||
id: string; | ||
accessToken: string; | ||
account: { | ||
displayName: string; | ||
id: string; | ||
} | ||
scopes: string[]; | ||
} | ||
|
||
export interface AuthenticationSessionsChangeEvent { | ||
added: string[]; | ||
removed: string[]; | ||
changed: string[]; | ||
} | ||
|
||
export interface AuthenticationProvider { | ||
id: string; | ||
|
||
supportsMultipleAccounts: boolean; | ||
|
||
displayName: string; | ||
|
||
hasSessions(): boolean; | ||
|
||
signOut(accountName: string): Promise<void>; | ||
|
||
getSessions(): Promise<ReadonlyArray<AuthenticationSession>>; | ||
|
||
updateSessionItems(event: AuthenticationSessionsChangeEvent): Promise<void>; | ||
|
||
login(scopes: string[]): Promise<AuthenticationSession>; | ||
|
||
logout(sessionId: string): Promise<void>; | ||
} | ||
export const AuthenticationService = Symbol('AuthenticationService'); | ||
|
||
export interface AuthenticationService { | ||
isAuthenticationProviderRegistered(id: string): boolean; | ||
getProviderIds(): string[]; | ||
registerAuthenticationProvider(id: string, provider: AuthenticationProvider): void; | ||
unregisterAuthenticationProvider(id: string): void; | ||
sessionsUpdate(providerId: string, event: AuthenticationSessionsChangeEvent): void; | ||
|
||
readonly onDidRegisterAuthenticationProvider: Event<string>; | ||
readonly onDidUnregisterAuthenticationProvider: Event<string>; | ||
|
||
readonly onDidChangeSessions: Event<{ providerId: string, event: AuthenticationSessionsChangeEvent }>; | ||
getSessions(providerId: string): Promise<ReadonlyArray<AuthenticationSession>>; | ||
getDisplayName(providerId: string): string; | ||
supportsMultipleAccounts(providerId: string): boolean; | ||
login(providerId: string, scopes: string[]): Promise<AuthenticationSession>; | ||
logout(providerId: string, sessionId: string): Promise<void>; | ||
|
||
signOutOfAccount(providerId: string, accountName: string): Promise<void>; | ||
} | ||
|
||
@injectable() | ||
export class AuthenticationServiceImpl implements AuthenticationService { | ||
private authenticationProviders: Map<string, AuthenticationProvider> = new Map<string, AuthenticationProvider>(); | ||
|
||
private onDidRegisterAuthenticationProviderEmitter: Emitter<string> = new Emitter<string>(); | ||
readonly onDidRegisterAuthenticationProvider: Event<string> = this.onDidRegisterAuthenticationProviderEmitter.event; | ||
|
||
private onDidUnregisterAuthenticationProviderEmitter: Emitter<string> = new Emitter<string>(); | ||
readonly onDidUnregisterAuthenticationProvider: Event<string> = this.onDidUnregisterAuthenticationProviderEmitter.event; | ||
|
||
private onDidChangeSessionsEmitter: Emitter<{ providerId: string, event: AuthenticationSessionsChangeEvent }> = | ||
new Emitter<{ providerId: string, event: AuthenticationSessionsChangeEvent }>(); | ||
readonly onDidChangeSessions: Event<{ providerId: string, event: AuthenticationSessionsChangeEvent }> = this.onDidChangeSessionsEmitter.event; | ||
|
||
getProviderIds(): string[] { | ||
const providerIds: string[] = []; | ||
this.authenticationProviders.forEach(provider => { | ||
providerIds.push(provider.id); | ||
}); | ||
return providerIds; | ||
} | ||
|
||
isAuthenticationProviderRegistered(id: string): boolean { | ||
return this.authenticationProviders.has(id); | ||
} | ||
|
||
registerAuthenticationProvider(id: string, authenticationProvider: AuthenticationProvider): void { | ||
this.authenticationProviders.set(id, authenticationProvider); | ||
this.onDidRegisterAuthenticationProviderEmitter.fire(id); | ||
} | ||
|
||
unregisterAuthenticationProvider(id: string): void { | ||
const provider = this.authenticationProviders.get(id); | ||
if (provider) { | ||
this.authenticationProviders.delete(id); | ||
this.onDidUnregisterAuthenticationProviderEmitter.fire(id); | ||
} | ||
} | ||
|
||
async sessionsUpdate(id: string, event: AuthenticationSessionsChangeEvent): Promise<void> { | ||
this.onDidChangeSessionsEmitter.fire({ providerId: id, event: event }); | ||
const provider = this.authenticationProviders.get(id); | ||
if (provider) { | ||
await provider.updateSessionItems(event); | ||
} | ||
} | ||
|
||
getDisplayName(id: string): string { | ||
const authProvider = this.authenticationProviders.get(id); | ||
if (authProvider) { | ||
return authProvider.displayName; | ||
} else { | ||
throw new Error(`No authentication provider '${id}' is currently registered.`); | ||
} | ||
} | ||
|
||
supportsMultipleAccounts(id: string): boolean { | ||
const authProvider = this.authenticationProviders.get(id); | ||
if (authProvider) { | ||
return authProvider.supportsMultipleAccounts; | ||
} else { | ||
throw new Error(`No authentication provider '${id}' is currently registered.`); | ||
} | ||
} | ||
|
||
async getSessions(id: string): Promise<ReadonlyArray<AuthenticationSession>> { | ||
const authProvider = this.authenticationProviders.get(id); | ||
if (authProvider) { | ||
return await authProvider.getSessions(); | ||
} else { | ||
throw new Error(`No authentication provider '${id}' is currently registered.`); | ||
} | ||
} | ||
|
||
async login(id: string, scopes: string[]): Promise<AuthenticationSession> { | ||
const authProvider = this.authenticationProviders.get(id); | ||
if (authProvider) { | ||
return authProvider.login(scopes); | ||
} else { | ||
throw new Error(`No authentication provider '${id}' is currently registered.`); | ||
} | ||
} | ||
|
||
async logout(id: string, sessionId: string): Promise<void> { | ||
const authProvider = this.authenticationProviders.get(id); | ||
if (authProvider) { | ||
return authProvider.logout(sessionId); | ||
} else { | ||
throw new Error(`No authentication provider '${id}' is currently registered.`); | ||
} | ||
} | ||
|
||
async signOutOfAccount(id: string, accountName: string): Promise<void> { | ||
const authProvider = this.authenticationProviders.get(id); | ||
if (authProvider) { | ||
return authProvider.signOut(accountName); | ||
} else { | ||
throw new Error(`No authentication provider '${id}' is currently registered.`); | ||
} | ||
} | ||
} | ||
|
||
export interface AllowedExtension { | ||
id: string; | ||
name: string; | ||
} | ||
|
||
export async function readAllowedExtensions(storageService: StorageService, providerId: string, accountName: string): Promise<AllowedExtension[]> { | ||
let trustedExtensions: AllowedExtension[] = []; | ||
try { | ||
const trustedExtensionSrc: string | undefined = await storageService.getData(`${providerId}-${accountName}`); | ||
if (trustedExtensionSrc) { | ||
trustedExtensions = JSON.parse(trustedExtensionSrc); | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
|
||
return trustedExtensions; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.