1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | import { inject, injectable } from 'inversify';
|
24 | import { Emitter, Event } from '../common/event';
|
25 | import { KeytarService } from '../common/keytar-protocol';
|
26 |
|
27 | export interface CredentialsProvider {
|
28 | getPassword(service: string, account: string): Promise<string | undefined>;
|
29 | setPassword(service: string, account: string, password: string): Promise<void>;
|
30 | deletePassword(service: string, account: string): Promise<boolean>;
|
31 | findPassword(service: string): Promise<string | undefined>;
|
32 | findCredentials(service: string): Promise<Array<{ account: string, password: string }>>;
|
33 | }
|
34 |
|
35 | export const CredentialsService = Symbol('CredentialsService');
|
36 |
|
37 | export interface CredentialsService extends CredentialsProvider {
|
38 | readonly onDidChangePassword: Event<CredentialsChangeEvent>;
|
39 | }
|
40 |
|
41 | export interface CredentialsChangeEvent {
|
42 | service: string
|
43 | account: string;
|
44 | }
|
45 |
|
46 | @injectable()
|
47 | export class CredentialsServiceImpl implements CredentialsService {
|
48 | private onDidChangePasswordEmitter = new Emitter<CredentialsChangeEvent>();
|
49 | readonly onDidChangePassword = this.onDidChangePasswordEmitter.event;
|
50 |
|
51 | private credentialsProvider: CredentialsProvider;
|
52 |
|
53 | constructor(@inject(KeytarService) private readonly keytarService: KeytarService) {
|
54 | this.credentialsProvider = new KeytarCredentialsProvider(this.keytarService);
|
55 | }
|
56 |
|
57 | getPassword(service: string, account: string): Promise<string | undefined> {
|
58 | return this.credentialsProvider.getPassword(service, account);
|
59 | }
|
60 |
|
61 | async setPassword(service: string, account: string, password: string): Promise<void> {
|
62 | await this.credentialsProvider.setPassword(service, account, password);
|
63 |
|
64 | this.onDidChangePasswordEmitter.fire({ service, account });
|
65 | }
|
66 |
|
67 | deletePassword(service: string, account: string): Promise<boolean> {
|
68 | const didDelete = this.credentialsProvider.deletePassword(service, account);
|
69 | this.onDidChangePasswordEmitter.fire({ service, account });
|
70 |
|
71 | return didDelete;
|
72 | }
|
73 |
|
74 | findPassword(service: string): Promise<string | undefined> {
|
75 | return this.credentialsProvider.findPassword(service);
|
76 | }
|
77 |
|
78 | findCredentials(service: string): Promise<Array<{ account: string, password: string; }>> {
|
79 | return this.credentialsProvider.findCredentials(service);
|
80 | }
|
81 | }
|
82 |
|
83 | class KeytarCredentialsProvider implements CredentialsProvider {
|
84 |
|
85 | constructor(private readonly keytarService: KeytarService) {}
|
86 |
|
87 | deletePassword(service: string, account: string): Promise<boolean> {
|
88 | return this.keytarService.deletePassword(service, account);
|
89 | }
|
90 |
|
91 | findCredentials(service: string): Promise<Array<{ account: string; password: string }>> {
|
92 | return this.keytarService.findCredentials(service);
|
93 | }
|
94 |
|
95 | findPassword(service: string): Promise<string | undefined> {
|
96 | return this.keytarService.findPassword(service);
|
97 | }
|
98 |
|
99 | getPassword(service: string, account: string): Promise<string | undefined> {
|
100 | return this.keytarService.getPassword(service, account);
|
101 | }
|
102 |
|
103 | setPassword(service: string, account: string, password: string): Promise<void> {
|
104 | return this.keytarService.setPassword(service, account, password);
|
105 | }
|
106 | }
|