authTokenService.ts 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. 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';
10
import { IProductService } from 'vs/platform/product/common/productService';
S
Sandeep Somavarapu 已提交
11
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
12

S
Sandeep Somavarapu 已提交
13 14
const SERVICE_NAME = 'VS Code';
const ACCOUNT = 'MyAccount';
15 16 17 18

export class AuthTokenService extends Disposable implements IAuthTokenService {
	_serviceBrand: undefined;

19
	private _status: AuthTokenStatus = AuthTokenStatus.Disabled;
20 21 22 23 24 25
	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,
26
		@IProductService productService: IProductService,
S
Sandeep Somavarapu 已提交
27
		@IConfigurationService configurationService: IConfigurationService,
28 29
	) {
		super();
S
Sandeep Somavarapu 已提交
30
		if (productService.settingsSyncStoreUrl && configurationService.getValue('configurationSync.enableAuth')) {
31 32 33 34 35 36 37
			this._status = AuthTokenStatus.Inactive;
			this.getToken().then(token => {
				if (token) {
					this.setStatus(AuthTokenStatus.Active);
				}
			});
		}
38 39 40
	}

	getToken(): Promise<string | null> {
S
Sandeep Somavarapu 已提交
41 42 43
		if (this.status === AuthTokenStatus.Disabled) {
			throw new Error('Not enabled');
		}
44 45 46 47
		return this.credentialsService.getPassword(SERVICE_NAME, ACCOUNT);
	}

	async updateToken(token: string): Promise<void> {
S
Sandeep Somavarapu 已提交
48 49 50
		if (this.status === AuthTokenStatus.Disabled) {
			throw new Error('Not enabled');
		}
51
		await this.credentialsService.setPassword(SERVICE_NAME, ACCOUNT, token);
52
		this.setStatus(AuthTokenStatus.Active);
53 54 55
	}

	async refreshToken(): Promise<void> {
S
Sandeep Somavarapu 已提交
56 57 58
		if (this.status === AuthTokenStatus.Disabled) {
			throw new Error('Not enabled');
		}
59 60 61 62
		await this.deleteToken();
	}

	async deleteToken(): Promise<void> {
S
Sandeep Somavarapu 已提交
63 64 65
		if (this.status === AuthTokenStatus.Disabled) {
			throw new Error('Not enabled');
		}
66
		await this.credentialsService.deletePassword(SERVICE_NAME, ACCOUNT);
67
		this.setStatus(AuthTokenStatus.Inactive);
68 69 70 71 72 73 74 75 76 77 78
	}

	private setStatus(status: AuthTokenStatus): void {
		if (this._status !== status) {
			this._status = status;
			this._onDidChangeStatus.fire(status);
		}
	}

}