credentialsService.ts 3.4 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.
 *--------------------------------------------------------------------------------------------*/

S
Sandeep Somavarapu 已提交
6
import { ICredentialsProvider, ICredentialsService } from 'vs/platform/credentials/common/credentials';
7 8 9 10 11
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';

export class BrowserCredentialsService implements ICredentialsService {

12
	declare readonly _serviceBrand: undefined;
13 14 15 16 17 18 19

	private credentialsProvider: ICredentialsProvider;

	constructor(@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService) {
		if (environmentService.options && environmentService.options.credentialsProvider) {
			this.credentialsProvider = environmentService.options.credentialsProvider;
		} else {
20
			this.credentialsProvider = new InMemoryCredentialsProvider();
21 22 23
		}
	}

24
	getPassword(service: string, account: string): Promise<string | null> {
25 26 27
		return this.credentialsProvider.getPassword(service, account);
	}

28
	setPassword(service: string, account: string, password: string): Promise<void> {
29 30 31
		return this.credentialsProvider.setPassword(service, account, password);
	}

32
	deletePassword(service: string, account: string): Promise<boolean> {
33 34 35
		return this.credentialsProvider.deletePassword(service, account);
	}

36
	findPassword(service: string): Promise<string | null> {
37 38
		return this.credentialsProvider.findPassword(service);
	}
39

40
	findCredentials(service: string): Promise<Array<{ account: string, password: string; }>> {
41 42
		return this.credentialsProvider.findCredentials(service);
	}
43 44 45 46 47 48 49 50
}

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

51
class InMemoryCredentialsProvider implements ICredentialsProvider {
52

53
	private credentials: ICredential[] = [];
54 55

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

58
		return credential ? credential.password : null;
59 60 61 62 63 64 65 66
	}

	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> {
67 68 69 70
		const credential = this.doFindPassword(service, account);
		if (credential) {
			this.credentials = this.credentials.splice(this.credentials.indexOf(credential), 1);
		}
71

72 73
		return !!credential;
	}
74

75 76
	async findPassword(service: string): Promise<string | null> {
		const credential = this.doFindPassword(service);
77

78 79
		return credential ? credential.password : null;
	}
80

81
	private doFindPassword(service: string, account?: string): ICredential | undefined {
82
		return this.credentials.find(credential =>
83
			credential.service === service && (typeof account !== 'string' || credential.account === account));
84
	}
85

86
	async findCredentials(service: string): Promise<Array<{ account: string, password: string; }>> {
87 88 89 90
		return this.credentials
			.filter(credential => credential.service === service)
			.map(({ account, password }) => ({ account, password }));
	}
91 92 93
}

registerSingleton(ICredentialsService, BrowserCredentialsService, true);