1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 15:53:27 +00:00

[PM-23621] Require userId for initAccount on the key-service (#15684)

* require userID for initAccount on key service

* add unit test coverage

* update consumer
This commit is contained in:
Thomas Avery
2025-07-22 10:30:50 -05:00
committed by GitHub
parent da6fb82fd8
commit e99abb49ec
4 changed files with 112 additions and 12 deletions

View File

@@ -661,19 +661,17 @@ export class DefaultKeyService implements KeyServiceAbstraction {
* Initialize all necessary crypto keys needed for a new account.
* Warning! This completely replaces any existing keys!
*/
async initAccount(): Promise<{
async initAccount(userId: UserId): Promise<{
userKey: UserKey;
publicKey: string;
privateKey: EncString;
}> {
const activeUserId = await firstValueFrom(this.stateProvider.activeUserId$);
if (activeUserId == null) {
throw new Error("Cannot initilize an account if one is not active.");
if (userId == null) {
throw new Error("UserId is required.");
}
// Verify user key doesn't exist
const existingUserKey = await this.getUserKey(activeUserId);
const existingUserKey = await this.getUserKey(userId);
if (existingUserKey != null) {
this.logService.error("Tried to initialize account with existing user key.");
@@ -686,9 +684,9 @@ export class DefaultKeyService implements KeyServiceAbstraction {
throw new Error("Failed to create valid private key.");
}
await this.setUserKey(userKey, activeUserId);
await this.setUserKey(userKey, userId);
await this.stateProvider
.getUser(activeUserId, USER_ENCRYPTED_PRIVATE_KEY)
.getUser(userId, USER_ENCRYPTED_PRIVATE_KEY)
.update(() => privateKey.encryptedString!);
return {