/********************************************************************************
 * Copyright (C) 2021 Red Hat, Inc. and others.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the Eclipse
 * Public License v. 2.0 are satisfied: GNU General Public License, version 2
 * with the GNU Classpath Exception which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 ********************************************************************************/ import { inject , injectable } from 'inversify' ; import { Emitter , Event } from '../common/event' ; import { KeytarService } from '../common/keytar-protocol' ; export interface CredentialsProvider { getPassword ( service , account ) : Promise < string | undefined > ; setPassword ( service , account , password ) : Promise < void > ; deletePassword ( service , account ) : Promise < boolean > ; findPassword ( service ) : Promise < string | undefined > ; findCredentials ( service ) : Promise < Array < { account : string ; password : string ; } > > ; } export const CredentialsService = Symbol ( 'CredentialsService' ) ; export interface CredentialsService extends CredentialsProvider { readonly onDidChangePassword : Event < CredentialsChangeEvent > ; } export interface CredentialsChangeEvent { service : string ; account : string ; } @ injectable ( ) export class CredentialsServiceImpl implements CredentialsService { private onDidChangePasswordEmitter = new Emitter < CredentialsChangeEvent > ( ) ; readonly onDidChangePassword = this . onDidChangePasswordEmitter . event ; private credentialsProvider : CredentialsProvider ; constructor ( @ inject ( KeytarService ) private readonly keytarService ) { this . credentialsProvider = new KeytarCredentialsProvider ( this . keytarService ) ; } getPassword ( service , account ) { return this . credentialsProvider . getPassword ( service , account ) ; } async setPassword ( service , account , password ) { await this . credentialsProvider . setPassword ( service , account , password ) ; this . onDidChangePasswordEmitter . fire ( { service , account } ) ; } deletePassword ( service , account ) { const didDelete = this . credentialsProvider . deletePassword ( service , account ) ; this . onDidChangePasswordEmitter . fire ( { service , account } ) ; return didDelete ; } findPassword ( service ) { return this . credentialsProvider . findPassword ( service ) ; } findCredentials ( service ) { return this . credentialsProvider . findCredentials ( service ) ; } } class KeytarCredentialsProvider implements CredentialsProvider { constructor ( private readonly keytarService ) { } deletePassword ( service , account ) { return this . keytarService . deletePassword ( service , account ) ; } findCredentials ( service ) { return this . keytarService . findCredentials ( service ) ; } findPassword ( service ) { return this . keytarService . findPassword ( service ) ; } getPassword ( service , account ) { return this . keytarService . getPassword ( service , account ) ; } setPassword ( service , account , password ) { return this . keytarService . setPassword ( service , account , password ) ; } }