credentialsService.ts 3.8 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 { ICredentialsService } from 'vs/platform/credentials/common/credentials';
7 8
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
9
import { find } from 'vs/base/common/arrays';
10 11 12 13

export interface ICredentialsProvider {
	getPassword(service: string, account: string): Promise<string | null>;
	setPassword(service: string, account: string, password: string): Promise<void>;
14

15
	deletePassword(service: string, account: string): Promise<boolean>;
16

17
	findPassword(service: string): Promise<string | null>;
18
	findCredentials(service: string): Promise<Array<{ account: string, password: string; }>>;
19 20 21 22
}

export class BrowserCredentialsService implements ICredentialsService {

23
	_serviceBrand: undefined;
24 25 26 27 28 29 30

	private credentialsProvider: ICredentialsProvider;

	constructor(@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService) {
		if (environmentService.options && environmentService.options.credentialsProvider) {
			this.credentialsProvider = environmentService.options.credentialsProvider;
		} else {
31
			this.credentialsProvider = new InMemoryCredentialsProvider();
32 33 34
		}
	}

35
	getPassword(service: string, account: string): Promise<string | null> {
36 37 38
		return this.credentialsProvider.getPassword(service, account);
	}

39
	setPassword(service: string, account: string, password: string): Promise<void> {
40 41 42
		return this.credentialsProvider.setPassword(service, account, password);
	}

43
	deletePassword(service: string, account: string): Promise<boolean> {
44 45 46
		return this.credentialsProvider.deletePassword(service, account);
	}

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

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

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

62
class InMemoryCredentialsProvider implements ICredentialsProvider {
63

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

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

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

	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> {
78 79 80 81
		const credential = this.doFindPassword(service, account);
		if (credential) {
			this.credentials = this.credentials.splice(this.credentials.indexOf(credential), 1);
		}
82

83 84
		return !!credential;
	}
85

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

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

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

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

registerSingleton(ICredentialsService, BrowserCredentialsService, true);