diff --git a/common/src/abstractions/crypto.service.ts b/common/src/abstractions/crypto.service.ts index 6ebedd3b2c..f26479fe10 100644 --- a/common/src/abstractions/crypto.service.ts +++ b/common/src/abstractions/crypto.service.ts @@ -15,6 +15,7 @@ export abstract class CryptoService { setEncPrivateKey: (encPrivateKey: string) => Promise<{}>; setOrgKeys: (orgs: ProfileOrganizationResponse[]) => Promise<{}>; getKey: (keySuffix?: KeySuffixOptions) => Promise; + getKeyFromStorage: (keySuffix: KeySuffixOptions) => Promise; getKeyHash: () => Promise; getEncKey: (key?: SymmetricCryptoKey) => Promise; getPublicKey: () => Promise; diff --git a/common/src/services/crypto.service.ts b/common/src/services/crypto.service.ts index fd3c129bb1..23330c943d 100644 --- a/common/src/services/crypto.service.ts +++ b/common/src/services/crypto.service.ts @@ -100,7 +100,18 @@ export class CryptoService implements CryptoServiceAbstraction { if (this.key != null) { return this.key; } + keySuffix ||= 'auto'; + const symmetricKey = await this.getKeyFromStorage(keySuffix); + + if (symmetricKey != null) { + this.setKey(symmetricKey); + } + + return symmetricKey; + } + + async getKeyFromStorage(keySuffix: KeySuffixOptions): Promise { const key = await this.retrieveKeyFromStorage(keySuffix); if (key != null) { @@ -112,10 +123,9 @@ export class CryptoService implements CryptoServiceAbstraction { return null; } - this.setKey(symmetricKey); + return symmetricKey; } - - return key == null ? null : this.key; + return null; } async getKeyHash(): Promise {