mirror of
https://github.com/bitwarden/browser.git
synced 2025-01-02 18:17:46 +01:00
Fix and test hasUserKey implementation (#8336)
This commit is contained in:
parent
5506842623
commit
e9a34ac5b8
@ -119,7 +119,7 @@ export class FakeActiveUserStateProvider implements ActiveUserStateProvider {
|
|||||||
states: Map<string, FakeActiveUserState<unknown>> = new Map();
|
states: Map<string, FakeActiveUserState<unknown>> = new Map();
|
||||||
|
|
||||||
constructor(public accountService: FakeAccountService) {
|
constructor(public accountService: FakeAccountService) {
|
||||||
this.activeUserId$ = accountService.activeAccountSubject.asObservable().pipe(map((a) => a.id));
|
this.activeUserId$ = accountService.activeAccountSubject.asObservable().pipe(map((a) => a?.id));
|
||||||
}
|
}
|
||||||
|
|
||||||
get<T>(keyDefinition: KeyDefinition<T> | UserKeyDefinition<T>): ActiveUserState<T> {
|
get<T>(keyDefinition: KeyDefinition<T> | UserKeyDefinition<T>): ActiveUserState<T> {
|
||||||
|
@ -4,7 +4,7 @@ import { ProfileOrganizationResponse } from "../../admin-console/models/response
|
|||||||
import { ProfileProviderOrganizationResponse } from "../../admin-console/models/response/profile-provider-organization.response";
|
import { ProfileProviderOrganizationResponse } from "../../admin-console/models/response/profile-provider-organization.response";
|
||||||
import { ProfileProviderResponse } from "../../admin-console/models/response/profile-provider.response";
|
import { ProfileProviderResponse } from "../../admin-console/models/response/profile-provider.response";
|
||||||
import { KdfConfig } from "../../auth/models/domain/kdf-config";
|
import { KdfConfig } from "../../auth/models/domain/kdf-config";
|
||||||
import { OrganizationId, ProviderId } from "../../types/guid";
|
import { OrganizationId, ProviderId, UserId } from "../../types/guid";
|
||||||
import { UserKey, MasterKey, OrgKey, ProviderKey, PinKey, CipherKey } from "../../types/key";
|
import { UserKey, MasterKey, OrgKey, ProviderKey, PinKey, CipherKey } from "../../types/key";
|
||||||
import { KeySuffixOptions, KdfType, HashPurpose } from "../enums";
|
import { KeySuffixOptions, KdfType, HashPurpose } from "../enums";
|
||||||
import { EncArrayBuffer } from "../models/domain/enc-array-buffer";
|
import { EncArrayBuffer } from "../models/domain/enc-array-buffer";
|
||||||
@ -62,12 +62,15 @@ export abstract class CryptoService {
|
|||||||
getUserKeyFromStorage: (keySuffix: KeySuffixOptions, userId?: string) => Promise<UserKey>;
|
getUserKeyFromStorage: (keySuffix: KeySuffixOptions, userId?: string) => Promise<UserKey>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Determines whether the user key is available for the given user.
|
||||||
|
* @param userId The desired user. If not provided, the active user will be used. If no active user exists, the method will return false.
|
||||||
* @returns True if the user key is available
|
* @returns True if the user key is available
|
||||||
*/
|
*/
|
||||||
hasUserKey: () => Promise<boolean>;
|
hasUserKey: (userId?: UserId) => Promise<boolean>;
|
||||||
/**
|
/**
|
||||||
* @param userId The desired user
|
* Determines whether the user key is available for the given user in memory.
|
||||||
* @returns True if the user key is set in memory
|
* @param userId The desired user. If not provided, the active user will be used. If no active user exists, the method will return false.
|
||||||
|
* @returns True if the user key is available
|
||||||
*/
|
*/
|
||||||
hasUserKeyInMemory: (userId?: string) => Promise<boolean>;
|
hasUserKeyInMemory: (userId?: string) => Promise<boolean>;
|
||||||
/**
|
/**
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { mock } from "jest-mock-extended";
|
import { mock } from "jest-mock-extended";
|
||||||
import { firstValueFrom } from "rxjs";
|
import { firstValueFrom, of } from "rxjs";
|
||||||
|
|
||||||
import { FakeAccountService, mockAccountServiceWith } from "../../../spec/fake-account-service";
|
import { FakeAccountService, mockAccountServiceWith } from "../../../spec/fake-account-service";
|
||||||
import { FakeActiveUserState, FakeSingleUserState } from "../../../spec/fake-state";
|
import { FakeActiveUserState, FakeSingleUserState } from "../../../spec/fake-state";
|
||||||
@ -108,6 +108,52 @@ describe("cryptoService", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe.each(["hasUserKey", "hasUserKeyInMemory"])(
|
||||||
|
`%s`,
|
||||||
|
(method: "hasUserKey" | "hasUserKeyInMemory") => {
|
||||||
|
let mockUserKey: UserKey;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
const mockRandomBytes = new Uint8Array(64) as CsprngArray;
|
||||||
|
mockUserKey = new SymmetricCryptoKey(mockRandomBytes) as UserKey;
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([true, false])("returns %s if the user key is set", async (hasKey) => {
|
||||||
|
stateProvider.singleUser
|
||||||
|
.getFake(mockUserId, USER_KEY)
|
||||||
|
.nextState(hasKey ? mockUserKey : null);
|
||||||
|
expect(await cryptoService[method](mockUserId)).toBe(hasKey);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns false when no active userId is set", async () => {
|
||||||
|
accountService.activeAccountSubject.next(null);
|
||||||
|
expect(await cryptoService[method]()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([true, false])(
|
||||||
|
"resolves %s for active user id when none is provided",
|
||||||
|
async (hasKey) => {
|
||||||
|
stateProvider.activeUserId$ = of(mockUserId);
|
||||||
|
stateProvider.singleUser
|
||||||
|
.getFake(mockUserId, USER_KEY)
|
||||||
|
.nextState(hasKey ? mockUserKey : null);
|
||||||
|
expect(await cryptoService[method]()).toBe(hasKey);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
describe("hasUserKey", () => {
|
||||||
|
it.each([true, false])(
|
||||||
|
"returns %s when the user key is not in memory, but the auto key is set",
|
||||||
|
async (hasKey) => {
|
||||||
|
stateProvider.singleUser.getFake(mockUserId, USER_KEY).nextState(null);
|
||||||
|
cryptoService.hasUserKeyStored = jest.fn().mockResolvedValue(hasKey);
|
||||||
|
expect(await cryptoService.hasUserKey(mockUserId)).toBe(hasKey);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
describe("getUserKeyWithLegacySupport", () => {
|
describe("getUserKeyWithLegacySupport", () => {
|
||||||
let mockUserKey: UserKey;
|
let mockUserKey: UserKey;
|
||||||
let mockMasterKey: MasterKey;
|
let mockMasterKey: MasterKey;
|
||||||
|
@ -202,13 +202,23 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async hasUserKey(): Promise<boolean> {
|
async hasUserKey(userId?: UserId): Promise<boolean> {
|
||||||
|
userId ??= await firstValueFrom(this.stateProvider.activeUserId$);
|
||||||
|
if (userId == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
(await this.hasUserKeyInMemory()) || (await this.hasUserKeyStored(KeySuffixOptions.Auto))
|
(await this.hasUserKeyInMemory(userId)) ||
|
||||||
|
(await this.hasUserKeyStored(KeySuffixOptions.Auto, userId))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async hasUserKeyInMemory(userId?: UserId): Promise<boolean> {
|
async hasUserKeyInMemory(userId?: UserId): Promise<boolean> {
|
||||||
|
userId ??= await firstValueFrom(this.stateProvider.activeUserId$);
|
||||||
|
if (userId == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return (await firstValueFrom(this.stateProvider.getUserState$(USER_KEY, userId))) != null;
|
return (await firstValueFrom(this.stateProvider.getUserState$(USER_KEY, userId))) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user