1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-13 06:54:07 +00:00

Refactor setUserDecryptionOptions to accept UserId

This commit is contained in:
Anders Åberg
2025-10-28 12:21:04 +01:00
parent 8eef78960d
commit e2447099ee
7 changed files with 26 additions and 9 deletions

View File

@@ -195,10 +195,10 @@ export class DefaultSetInitialPasswordService implements SetInitialPasswordServi
userId: UserId,
) {
const userDecryptionOpts = await firstValueFrom(
this.userDecryptionOptionsService.userDecryptionOptions$,
this.userDecryptionOptionsService.userDecryptionOptionsById$(userId),
);
userDecryptionOpts.hasMasterPassword = true;
await this.userDecryptionOptionsService.setUserDecryptionOptions(userDecryptionOpts);
await this.userDecryptionOptionsService.setUserDecryptionOptions(userDecryptionOpts, userId);
await this.kdfConfigService.setKdfConfig(userId, kdfConfig);
await this.masterPasswordService.setMasterKey(masterKey, userId);
await this.keyService.setUserKey(masterKeyEncryptedUserKey[0], userId);

View File

@@ -149,7 +149,9 @@ describe("DefaultSetInitialPasswordService", () => {
userDecryptionOptions = new UserDecryptionOptions({ hasMasterPassword: true });
userDecryptionOptionsSubject = new BehaviorSubject(userDecryptionOptions);
userDecryptionOptionsService.userDecryptionOptions$ = userDecryptionOptionsSubject;
userDecryptionOptionsService.userDecryptionOptionsById$ = jest
.fn()
.mockReturnValue(userDecryptionOptionsSubject);
setPasswordRequest = new SetPasswordRequest(
credentials.newServerMasterKeyHash,
@@ -364,6 +366,7 @@ describe("DefaultSetInitialPasswordService", () => {
expect(masterPasswordApiService.setPassword).toHaveBeenCalledWith(setPasswordRequest);
expect(userDecryptionOptionsService.setUserDecryptionOptions).toHaveBeenCalledWith(
userDecryptionOptions,
userId,
);
expect(kdfConfigService.setKdfConfig).toHaveBeenCalledWith(userId, credentials.kdfConfig);
expect(masterPasswordService.setMasterKey).toHaveBeenCalledWith(
@@ -562,6 +565,7 @@ describe("DefaultSetInitialPasswordService", () => {
expect(masterPasswordApiService.setPassword).toHaveBeenCalledWith(setPasswordRequest);
expect(userDecryptionOptionsService.setUserDecryptionOptions).toHaveBeenCalledWith(
userDecryptionOptions,
userId,
);
expect(kdfConfigService.setKdfConfig).toHaveBeenCalledWith(userId, credentials.kdfConfig);
expect(masterPasswordService.setMasterKey).toHaveBeenCalledWith(

View File

@@ -1,5 +1,7 @@
import { Observable } from "rxjs";
import { UserId } from "@bitwarden/common/types/guid";
import { UserDecryptionOptions } from "../models";
export abstract class UserDecryptionOptionsServiceAbstraction {
@@ -29,6 +31,10 @@ export abstract class InternalUserDecryptionOptionsServiceAbstraction extends Us
* @remark Intended to be used when user decryption options are received from server, does
* not update the server. Consider syncing instead of updating locally.
* @param userDecryptionOptions Current user decryption options received from server.
* @param userId The user id to set the decryption options for.
*/
abstract setUserDecryptionOptions(userDecryptionOptions: UserDecryptionOptions): Promise<void>;
abstract setUserDecryptionOptions(
userDecryptionOptions: UserDecryptionOptions,
userId: UserId,
): Promise<void>;
}

View File

@@ -259,6 +259,7 @@ describe("LoginStrategy", () => {
expect(userDecryptionOptionsService.setUserDecryptionOptions).toHaveBeenCalledWith(
UserDecryptionOptions.fromResponse(idTokenResponse),
userId,
);
expect(masterPasswordService.mock.setMasterPasswordUnlockData).toHaveBeenCalledWith(
new MasterPasswordUnlockData(

View File

@@ -197,6 +197,7 @@ export abstract class LoginStrategy {
// as the user decryption options help determine the available timeout actions.
await this.userDecryptionOptionsService.setUserDecryptionOptions(
UserDecryptionOptions.fromResponse(tokenResponse),
userId,
);
if (tokenResponse.userDecryptionOptions?.masterPasswordUnlock != null) {

View File

@@ -83,11 +83,11 @@ describe("UserDecryptionOptionsService", () => {
});
describe("setUserDecryptionOptions", () => {
it("should set the active user's decryption options", async () => {
await sut.setUserDecryptionOptions(userDecryptionOptions);
it("should set the specified user's decryption options", async () => {
await sut.setUserDecryptionOptions(userDecryptionOptions, fakeUserId);
const result = await firstValueFrom(
fakeStateProvider.getActive(USER_DECRYPTION_OPTIONS).state$,
fakeStateProvider.getUser(fakeUserId, USER_DECRYPTION_OPTIONS).state$,
);
expect(result).toEqual(userDecryptionOptions);

View File

@@ -45,7 +45,12 @@ export class UserDecryptionOptionsService
return this.stateProvider.getUser(userId, USER_DECRYPTION_OPTIONS).state$;
}
async setUserDecryptionOptions(userDecryptionOptions: UserDecryptionOptions): Promise<void> {
await this.userDecryptionOptionsState.update((_) => userDecryptionOptions);
async setUserDecryptionOptions(
userDecryptionOptions: UserDecryptionOptions,
userId: UserId,
): Promise<void> {
await this.stateProvider
.getUser(userId, USER_DECRYPTION_OPTIONS)
.update((_) => userDecryptionOptions);
}
}