mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 00:03:56 +00:00
* Move user key memory state to state providers
Note: state service observable change is because these updates are no longer internal to the class, but reporter directly to account service through crypto service on update of a user key
* remove decrypted user key state
Note, we're going to move the encrypted cryptoSymmetric key (and associated master key encrypted user keys) as part of the master key service creation. Crypto service will no longer be responsible for the encrypted forms of user key.
* Deprecate notices belong on abstraction
* Allow for single-direction status updates
This is necessary since we don't want to have to guarantee that the update to logged out occurs after the update to locked.
* Remove deprecated subject
It turns out the set for cryptoMasterKey was also unused 🎉
64 lines
2.2 KiB
TypeScript
64 lines
2.2 KiB
TypeScript
import { UserPrivateKey, UserPublicKey, UserKey } from "../../../types/key";
|
|
import { CryptoFunctionService } from "../../abstractions/crypto-function.service";
|
|
import { EncryptService } from "../../abstractions/encrypt.service";
|
|
import { EncString, EncryptedString } from "../../models/domain/enc-string";
|
|
import { SymmetricCryptoKey } from "../../models/domain/symmetric-crypto-key";
|
|
import { KeyDefinition, CRYPTO_DISK, DeriveDefinition, CRYPTO_MEMORY } from "../../state";
|
|
import { CryptoService } from "../crypto.service";
|
|
|
|
export const USER_EVER_HAD_USER_KEY = new KeyDefinition<boolean>(CRYPTO_DISK, "everHadUserKey", {
|
|
deserializer: (obj) => obj,
|
|
});
|
|
|
|
export const USER_ENCRYPTED_PRIVATE_KEY = new KeyDefinition<EncryptedString>(
|
|
CRYPTO_DISK,
|
|
"privateKey",
|
|
{
|
|
deserializer: (obj) => obj,
|
|
},
|
|
);
|
|
|
|
export const USER_PRIVATE_KEY = DeriveDefinition.fromWithUserId<
|
|
EncryptedString,
|
|
UserPrivateKey,
|
|
// TODO: update cryptoService to user key directly
|
|
{ encryptService: EncryptService; cryptoService: CryptoService }
|
|
>(USER_ENCRYPTED_PRIVATE_KEY, {
|
|
deserializer: (obj) => new Uint8Array(Object.values(obj)) as UserPrivateKey,
|
|
derive: async ([userId, encPrivateKeyString], { encryptService, cryptoService }) => {
|
|
if (encPrivateKeyString == null) {
|
|
return null;
|
|
}
|
|
|
|
const userKey = await cryptoService.getUserKey(userId);
|
|
if (userKey == null) {
|
|
return null;
|
|
}
|
|
|
|
const encPrivateKey = new EncString(encPrivateKeyString);
|
|
const privateKey = (await encryptService.decryptToBytes(
|
|
encPrivateKey,
|
|
userKey,
|
|
)) as UserPrivateKey;
|
|
return privateKey;
|
|
},
|
|
});
|
|
|
|
export const USER_PUBLIC_KEY = DeriveDefinition.from<
|
|
UserPrivateKey,
|
|
UserPublicKey,
|
|
{ cryptoFunctionService: CryptoFunctionService }
|
|
>([USER_PRIVATE_KEY, "publicKey"], {
|
|
deserializer: (obj) => new Uint8Array(Object.values(obj)) as UserPublicKey,
|
|
derive: async (privateKey, { cryptoFunctionService }) => {
|
|
if (privateKey == null) {
|
|
return null;
|
|
}
|
|
|
|
return (await cryptoFunctionService.rsaExtractPublicKey(privateKey)) as UserPublicKey;
|
|
},
|
|
});
|
|
export const USER_KEY = new KeyDefinition<UserKey>(CRYPTO_MEMORY, "userKey", {
|
|
deserializer: (obj) => SymmetricCryptoKey.fromJSON(obj) as UserKey,
|
|
});
|