UNPKG

4.54 kBPlain TextView Raw
1// *****************************************************************************
2// Copyright (C) 2021 Red Hat, Inc. and others.
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License v. 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0.
7//
8// This Source Code may also be made available under the following Secondary
9// Licenses when the conditions for such availability set forth in the Eclipse
10// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11// with the GNU Classpath Exception which is available at
12// https://www.gnu.org/software/classpath/license.html.
13//
14// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15// *****************************************************************************
16
17/*---------------------------------------------------------------------------------------------
18 * Copyright (c) Microsoft Corporation. All rights reserved.
19 * Licensed under the MIT License. See License.txt in the project root for license information.
20 *--------------------------------------------------------------------------------------------*/
21// code copied and modified from https://github.com/microsoft/vscode/blob/1.55.2/src/vs/workbench/services/credentials/common/credentials.ts#L12
22
23import { inject, injectable } from 'inversify';
24import { Emitter, Event } from '../common/event';
25import { KeytarService } from '../common/keytar-protocol';
26
27export 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
35export const CredentialsService = Symbol('CredentialsService');
36
37export interface CredentialsService extends CredentialsProvider {
38 readonly onDidChangePassword: Event<CredentialsChangeEvent>;
39}
40
41export interface CredentialsChangeEvent {
42 service: string
43 account: string;
44}
45
46@injectable()
47export 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
83class 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}