mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-08 01:28:26 -05:00
Merge from vscode f2d41726ba5a0e8abfe61b2c743022b1b6372010 (#7415)
* Merge from vscode f2d41726ba5a0e8abfe61b2c743022b1b6372010 * add missing files
This commit is contained in:
29
src/vs/platform/auth/common/auth.ts
Normal file
29
src/vs/platform/auth/common/auth.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { Event } from 'vs/base/common/event';
|
||||
|
||||
export const enum AuthTokenStatus {
|
||||
Disabled = 'Disabled',
|
||||
Inactive = 'Inactive',
|
||||
Active = 'Active'
|
||||
}
|
||||
|
||||
export const IAuthTokenService = createDecorator<IAuthTokenService>('IAuthTokenService');
|
||||
|
||||
export interface IAuthTokenService {
|
||||
_serviceBrand: undefined;
|
||||
|
||||
readonly status: AuthTokenStatus;
|
||||
readonly onDidChangeStatus: Event<AuthTokenStatus>;
|
||||
|
||||
getToken(): Promise<string | null>;
|
||||
updateToken(token: string): Promise<void>;
|
||||
refreshToken(): Promise<void>;
|
||||
deleteToken(): Promise<void>;
|
||||
|
||||
}
|
||||
|
||||
31
src/vs/platform/auth/common/authTokenIpc.ts
Normal file
31
src/vs/platform/auth/common/authTokenIpc.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IServerChannel } from 'vs/base/parts/ipc/common/ipc';
|
||||
import { Event } from 'vs/base/common/event';
|
||||
import { IAuthTokenService } from 'vs/platform/auth/common/auth';
|
||||
|
||||
export class AuthTokenChannel implements IServerChannel {
|
||||
|
||||
constructor(private readonly service: IAuthTokenService) { }
|
||||
|
||||
listen(_: unknown, event: string): Event<any> {
|
||||
switch (event) {
|
||||
case 'onDidChangeStatus': return this.service.onDidChangeStatus;
|
||||
}
|
||||
throw new Error(`Event not found: ${event}`);
|
||||
}
|
||||
|
||||
call(context: any, command: string, args?: any): Promise<any> {
|
||||
switch (command) {
|
||||
case '_getInitialStatus': return Promise.resolve(this.service.status);
|
||||
case 'getToken': return this.service.getToken();
|
||||
case 'updateToken': return this.service.updateToken(args[0]);
|
||||
case 'refreshToken': return this.service.refreshToken();
|
||||
case 'deleteToken': return this.service.deleteToken();
|
||||
}
|
||||
throw new Error('Invalid call');
|
||||
}
|
||||
}
|
||||
76
src/vs/platform/auth/common/authTokenService.ts
Normal file
76
src/vs/platform/auth/common/authTokenService.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { Event, Emitter } from 'vs/base/common/event';
|
||||
import { IAuthTokenService, AuthTokenStatus } from 'vs/platform/auth/common/auth';
|
||||
import { ICredentialsService } from 'vs/platform/credentials/common/credentials';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { IProductService } from 'vs/platform/product/common/productService';
|
||||
|
||||
const SERVICE_NAME = 'VS Code';
|
||||
const ACCOUNT = 'MyAccount';
|
||||
|
||||
export class AuthTokenService extends Disposable implements IAuthTokenService {
|
||||
_serviceBrand: undefined;
|
||||
|
||||
private _status: AuthTokenStatus = AuthTokenStatus.Disabled;
|
||||
get status(): AuthTokenStatus { return this._status; }
|
||||
private _onDidChangeStatus: Emitter<AuthTokenStatus> = this._register(new Emitter<AuthTokenStatus>());
|
||||
readonly onDidChangeStatus: Event<AuthTokenStatus> = this._onDidChangeStatus.event;
|
||||
|
||||
constructor(
|
||||
@ICredentialsService private readonly credentialsService: ICredentialsService,
|
||||
@IProductService productService: IProductService,
|
||||
) {
|
||||
super();
|
||||
if (productService.settingsSyncStoreUrl) {
|
||||
this._status = AuthTokenStatus.Inactive;
|
||||
this.getToken().then(token => {
|
||||
if (token) {
|
||||
this.setStatus(AuthTokenStatus.Active);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getToken(): Promise<string | null> {
|
||||
if (this.status === AuthTokenStatus.Disabled) {
|
||||
throw new Error('Not enabled');
|
||||
}
|
||||
return this.credentialsService.getPassword(SERVICE_NAME, ACCOUNT);
|
||||
}
|
||||
|
||||
async updateToken(token: string): Promise<void> {
|
||||
if (this.status === AuthTokenStatus.Disabled) {
|
||||
throw new Error('Not enabled');
|
||||
}
|
||||
await this.credentialsService.setPassword(SERVICE_NAME, ACCOUNT, token);
|
||||
this.setStatus(AuthTokenStatus.Active);
|
||||
}
|
||||
|
||||
async refreshToken(): Promise<void> {
|
||||
if (this.status === AuthTokenStatus.Disabled) {
|
||||
throw new Error('Not enabled');
|
||||
}
|
||||
await this.deleteToken();
|
||||
}
|
||||
|
||||
async deleteToken(): Promise<void> {
|
||||
if (this.status === AuthTokenStatus.Disabled) {
|
||||
throw new Error('Not enabled');
|
||||
}
|
||||
await this.credentialsService.deletePassword(SERVICE_NAME, ACCOUNT);
|
||||
this.setStatus(AuthTokenStatus.Inactive);
|
||||
}
|
||||
|
||||
private setStatus(status: AuthTokenStatus): void {
|
||||
if (this._status !== status) {
|
||||
this._status = status;
|
||||
this._onDidChangeStatus.fire(status);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user