import { UserId } from "../../types/guid"; import { CryptoService } from "../abstractions/crypto.service"; import { KeySuffixOptions } from "../enums"; // TODO: this is a half measure improvement which allows us to reduce some side effects today (cryptoService.getUserKey setting user key in memory if auto key exists) // but ideally, in the future, we would be able to put this logic into the cryptoService // after the vault timeout settings service is transitioned to state provider so that // the getUserKey logic can simply go to the correct location based on the vault timeout settings // similar to the TokenService (it would either go to secure storage for the auto user key or memory for the user key) export class UserAutoUnlockKeyService { constructor(private cryptoService: CryptoService) {} /** * The presence of the user key in memory dictates whether the user's vault is locked or unlocked. * However, for users that have the auto unlock user key set, we need to set the user key in memory * on application bootstrap and on active account changes so that the user's vault loads unlocked. * @param userId - The user id to check for an auto user key. * @returns True if the auto user key is set successfully, false otherwise. */ async setUserKeyInMemoryIfAutoUserKeySet(userId: UserId): Promise { if (userId == null) { return false; } const autoUserKey = await this.cryptoService.getUserKeyFromStorage( KeySuffixOptions.Auto, userId, ); if (autoUserKey == null) { return false; } await this.cryptoService.setUserKey(autoUserKey, userId); return true; } }