credentialsService.ts 3.7 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { ICredentialsService, ICredentialsProvider } from 'vs/workbench/services/credentials/common/credentials';
7 8
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
9
import { Emitter } from 'vs/base/common/event';
10 11 12

export class BrowserCredentialsService implements ICredentialsService {

13
	declare readonly _serviceBrand: undefined;
14

15
	private _onDidChangePassword: Emitter<void> = new Emitter();
16
	readonly onDidChangePassword = this._onDidChangePassword.event;
17

18 19 20 21 22 23
	private credentialsProvider: ICredentialsProvider;

	constructor(@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService) {
		if (environmentService.options && environmentService.options.credentialsProvider) {
			this.credentialsProvider = environmentService.options.credentialsProvider;
		} else {
24
			this.credentialsProvider = new InMemoryCredentialsProvider();
25 26 27
		}
	}

28
	getPassword(service: string, account: string): Promise<string | null> {
29 30 31
		return this.credentialsProvider.getPassword(service, account);
	}

32 33 34
	async setPassword(service: string, account: string, password: string): Promise<void> {
		await this.credentialsProvider.setPassword(service, account, password);
		this._onDidChangePassword.fire();
35 36
	}

37
	deletePassword(service: string, account: string): Promise<boolean> {
38 39 40 41 42 43
		const didDelete = this.credentialsProvider.deletePassword(service, account);
		if (didDelete) {
			this._onDidChangePassword.fire();
		}

		return didDelete;
44 45
	}

46
	findPassword(service: string): Promise<string | null> {
47 48
		return this.credentialsProvider.findPassword(service);
	}
49

50
	findCredentials(service: string): Promise<Array<{ account: string, password: string; }>> {
51 52
		return this.credentialsProvider.findCredentials(service);
	}
53 54 55 56 57 58 59 60
}

interface ICredential {
	service: string;
	account: string;
	password: string;
}

61
class InMemoryCredentialsProvider implements ICredentialsProvider {
62

63
	private credentials: ICredential[] = [];
64 65

	async getPassword(service: string, account: string): Promise<string | null> {
66
		const credential = this.doFindPassword(service, account);
67

68
		return credential ? credential.password : null;
69 70 71 72 73 74 75 76
	}

	async setPassword(service: string, account: string, password: string): Promise<void> {
		this.deletePassword(service, account);
		this.credentials.push({ service, account, password });
	}

	async deletePassword(service: string, account: string): Promise<boolean> {
77 78 79 80
		const credential = this.doFindPassword(service, account);
		if (credential) {
			this.credentials = this.credentials.splice(this.credentials.indexOf(credential), 1);
		}
81

82 83
		return !!credential;
	}
84

85 86
	async findPassword(service: string): Promise<string | null> {
		const credential = this.doFindPassword(service);
87

88 89
		return credential ? credential.password : null;
	}
90

91
	private doFindPassword(service: string, account?: string): ICredential | undefined {
92
		return this.credentials.find(credential =>
93
			credential.service === service && (typeof account !== 'string' || credential.account === account));
94
	}
95

96
	async findCredentials(service: string): Promise<Array<{ account: string, password: string; }>> {
97 98 99 100
		return this.credentials
			.filter(credential => credential.service === service)
			.map(({ account, password }) => ({ account, password }));
	}
101 102 103
}

registerSingleton(ICredentialsService, BrowserCredentialsService, true);