Skip to content

Commit

Permalink
Change getUserTrustDeviceChoiceForDecryption / setUserTrustDeviceChoi…
Browse files Browse the repository at this point in the history
…ceForDecryption to getShouldTrustDevice / setShouldTrustDevice (#5795)
  • Loading branch information
andrebispo5 authored and coroiu committed Jul 12, 2023
1 parent a6083aa commit b92e24b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,7 @@ export class BaseLoginDecryptionOptionsComponent implements OnInit, OnDestroy {
}

async approveWithMasterPassword() {
await this.deviceTrustCryptoService.setUserTrustDeviceChoiceForDecryption(
this.rememberDevice.value
);
await this.deviceTrustCryptoService.setShouldTrustDevice(this.rememberDevice.value);
this.router.navigate(["/lock"]);
}

Expand Down
4 changes: 2 additions & 2 deletions libs/angular/src/auth/components/lock.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ export class LockComponent implements OnInit, OnDestroy {

// Now that we have a decrypted user key in memory, we can check if we
// need to establish trust on the current device
if (this.deviceTrustCryptoService.getUserTrustDeviceChoiceForDecryption()) {
if (this.deviceTrustCryptoService.getShouldTrustDevice()) {
await this.deviceTrustCryptoService.trustDevice();
// reset the trust choice
await this.deviceTrustCryptoService.setUserTrustDeviceChoiceForDecryption(false);
await this.deviceTrustCryptoService.setShouldTrustDevice(false);
}

await this.doContinue(evaluatePasswordAfterUnlock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export abstract class DeviceTrustCryptoServiceAbstraction {
* @description Retrieves the users choice to trust the device which can only happen after decryption
* Note: this value should only be used once and then reset
*/
getUserTrustDeviceChoiceForDecryption: () => Promise<boolean>;
setUserTrustDeviceChoiceForDecryption: (value: boolean) => Promise<void>;
getShouldTrustDevice: () => Promise<boolean>;
setShouldTrustDevice: (value: boolean) => Promise<void>;

trustDevice: () => Promise<DeviceResponse>;
getDeviceKey: () => Promise<DeviceKey>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export class DeviceTrustCryptoService implements DeviceTrustCryptoServiceAbstrac
* @description Retrieves the users choice to trust the device which can only happen after decryption
* Note: this value should only be used once and then reset
*/
async getUserTrustDeviceChoiceForDecryption(): Promise<boolean> {
return await this.stateService.getUserTrustDeviceChoiceForDecryption();
async getShouldTrustDevice(): Promise<boolean> {
return await this.stateService.getShouldTrustDevice();
}

async setUserTrustDeviceChoiceForDecryption(value: boolean): Promise<void> {
await this.stateService.setUserTrustDeviceChoiceForDecryption(value);
async setShouldTrustDevice(value: boolean): Promise<void> {
await this.stateService.setShouldTrustDevice(value);
}

async trustDevice(): Promise<DeviceResponse> {
Expand Down
26 changes: 10 additions & 16 deletions libs/common/src/auth/services/device-trust-crypto.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,28 @@ describe("deviceTrustCryptoService", () => {
});

describe("User Trust Device Choice For Decryption", () => {
describe("getUserTrustDeviceChoiceForDecryption", () => {
describe("getShouldTrustDevice", () => {
it("gets the user trust device choice for decryption from the state service", async () => {
const stateSvcGetUserTrustDeviceChoiceForDecryptionSpy = jest.spyOn(
stateService,
"getUserTrustDeviceChoiceForDecryption"
);
const stateSvcGetShouldTrustDeviceSpy = jest.spyOn(stateService, "getShouldTrustDevice");

const expectedValue = true;
stateSvcGetUserTrustDeviceChoiceForDecryptionSpy.mockResolvedValue(expectedValue);
const result = await deviceTrustCryptoService.getUserTrustDeviceChoiceForDecryption();
stateSvcGetShouldTrustDeviceSpy.mockResolvedValue(expectedValue);
const result = await deviceTrustCryptoService.getShouldTrustDevice();

expect(stateSvcGetUserTrustDeviceChoiceForDecryptionSpy).toHaveBeenCalledTimes(1);
expect(stateSvcGetShouldTrustDeviceSpy).toHaveBeenCalledTimes(1);
expect(result).toEqual(expectedValue);
});
});

describe("setUserTrustDeviceChoiceForDecryption", () => {
describe("setShouldTrustDevice", () => {
it("sets the user trust device choice for decryption in the state service", async () => {
const stateSvcSetUserTrustDeviceChoiceForDecryptionSpy = jest.spyOn(
stateService,
"setUserTrustDeviceChoiceForDecryption"
);
const stateSvcSetShouldTrustDeviceSpy = jest.spyOn(stateService, "setShouldTrustDevice");

const newValue = true;
await deviceTrustCryptoService.setUserTrustDeviceChoiceForDecryption(newValue);
await deviceTrustCryptoService.setShouldTrustDevice(newValue);

expect(stateSvcSetUserTrustDeviceChoiceForDecryptionSpy).toHaveBeenCalledTimes(1);
expect(stateSvcSetUserTrustDeviceChoiceForDecryptionSpy).toHaveBeenCalledWith(newValue);
expect(stateSvcSetShouldTrustDeviceSpy).toHaveBeenCalledTimes(1);
expect(stateSvcSetShouldTrustDeviceSpy).toHaveBeenCalledWith(newValue);
});
});
});
Expand Down
7 changes: 2 additions & 5 deletions libs/common/src/platform/abstractions/state.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,8 @@ export abstract class StateService<T extends Account = Account> {
setDuckDuckGoSharedKey: (value: string, options?: StorageOptions) => Promise<void>;
getDeviceKey: (options?: StorageOptions) => Promise<DeviceKey | null>;
setDeviceKey: (value: DeviceKey, options?: StorageOptions) => Promise<void>;
getUserTrustDeviceChoiceForDecryption: (options?: StorageOptions) => Promise<boolean | null>;
setUserTrustDeviceChoiceForDecryption: (
value: boolean,
options?: StorageOptions
) => Promise<void>;
getShouldTrustDevice: (options?: StorageOptions) => Promise<boolean | null>;
setShouldTrustDevice: (value: boolean, options?: StorageOptions) => Promise<void>;
getAccountDecryptionOptions: (
options?: StorageOptions
) => Promise<AccountDecryptionOptions | null>;
Expand Down
7 changes: 2 additions & 5 deletions libs/common/src/platform/services/state.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ export class StateService<
await this.saveAccount(account, options);
}

async getUserTrustDeviceChoiceForDecryption(options?: StorageOptions): Promise<boolean> {
async getShouldTrustDevice(options?: StorageOptions): Promise<boolean> {
options = this.reconcileOptions(options, await this.defaultOnDiskLocalOptions());

if (options?.userId == null) {
Expand All @@ -1338,10 +1338,7 @@ export class StateService<
return account?.settings?.trustDeviceChoiceForDecryption ?? false;
}

async setUserTrustDeviceChoiceForDecryption(
value: boolean,
options?: StorageOptions
): Promise<void> {
async setShouldTrustDevice(value: boolean, options?: StorageOptions): Promise<void> {
options = this.reconcileOptions(options, await this.defaultOnDiskLocalOptions());
if (options?.userId == null) {
return;
Expand Down

0 comments on commit b92e24b

Please sign in to comment.