import { CryptoFunctionService } from "jslib-common/abstractions/cryptoFunction.service" ; import { LogService } from "jslib-common/abstractions/log.service" ; import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service" ; import { StateService } from "jslib-common/abstractions/state.service" ; import { CryptoService } from "jslib-common/services/crypto.service" ; import { KeySuffixOptions } from "jslib-common/enums/keySuffixOptions" ; import { StorageLocation } from "jslib-common/enums/storageLocation" ; import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey" ; export class ElectronCryptoService extends CryptoService { constructor ( cryptoFunctionService , platformUtilService , logService , stateService ) { super ( cryptoFunctionService , platformUtilService , logService , stateService ) ; } async hasKeyStored ( keySuffix ) { await this . upgradeSecurelyStoredKey ( ) ; return super . hasKeyStored ( keySuffix ) ; } protected async storeKey ( key , userId ? ) { if ( await this . shouldStoreKey ( KeySuffixOptions . Auto , userId ) ) { await this . stateService . setCryptoMasterKeyAuto ( key . keyB64 , { userId : userId } ) ; } else { this . clearStoredKey ( KeySuffixOptions . Auto ) ; } if ( await this . shouldStoreKey ( KeySuffixOptions . Biometric , userId ) ) { await this . stateService . setCryptoMasterKeyBiometric ( key . keyB64 , { userId : userId } ) ; } else { this . clearStoredKey ( KeySuffixOptions . Biometric ) ; } } protected async retrieveKeyFromStorage ( keySuffix ) { await this . upgradeSecurelyStoredKey ( ) ; return super . retrieveKeyFromStorage ( keySuffix ) ; } deprecated private async upgradeSecurelyStoredKey ( ) { const key = await this . stateService . getCryptoMasterKeyB64 ( ) ; if ( key == null ) { return ; } try { if ( await this . shouldStoreKey ( KeySuffixOptions . Auto ) ) { await this . stateService . setCryptoMasterKeyAuto ( key ) ; } if ( await this . shouldStoreKey ( KeySuffixOptions . Biometric ) ) { await this . stateService . setCryptoMasterKeyBiometric ( key ) ; } } catch ( e ) { this . logService . error ( `Encountered error while upgrading obsolete Bitwarden secure storage item:` ) ; this . logService . error ( e ) ; } await this . stateService . setCryptoMasterKeyB64 ( null ) ; } }