From e52a506d8613fc23105b4a57239112f17da92f6a Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Thu, 9 Feb 2023 15:31:45 +0100 Subject: [PATCH] Improve reloading with biometrics --- apps/desktop/src/app/app.component.ts | 6 +++++ apps/desktop/src/main.ts | 3 ++- .../main/biometric/biometrics.service.spec.ts | 26 ++++++++++++++++--- .../src/main/biometric/biometrics.service.ts | 11 ++++++-- libs/common/src/services/system.service.ts | 23 +++++----------- 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/apps/desktop/src/app/app.component.ts b/apps/desktop/src/app/app.component.ts index e2bd6a77e4..67a761a32d 100644 --- a/apps/desktop/src/app/app.component.ts +++ b/apps/desktop/src/app/app.component.ts @@ -199,6 +199,12 @@ export class AppComponent implements OnInit, OnDestroy { await this.systemService.clearPendingClipboard(); await this.systemService.startProcessReload(this.authService); break; + case "startProcessReload": + this.systemService.startProcessReload(this.authService); + break; + case "cancelProcessReload": + this.systemService.cancelProcessReload(); + break; case "reloadProcess": (window.location as any).reload(true); break; diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts index 6f1ef4a109..b573ef6975 100644 --- a/apps/desktop/src/main.ts +++ b/apps/desktop/src/main.ts @@ -121,7 +121,8 @@ export class Main { this.i18nService, this.windowMain, this.stateService, - this.logService + this.logService, + this.messagingService ); this.desktopCredentialStorageListener = new DesktopCredentialStorageListener( diff --git a/apps/desktop/src/main/biometric/biometrics.service.spec.ts b/apps/desktop/src/main/biometric/biometrics.service.spec.ts index 2e5b9a9505..2bc7ef0a51 100644 --- a/apps/desktop/src/main/biometric/biometrics.service.spec.ts +++ b/apps/desktop/src/main/biometric/biometrics.service.spec.ts @@ -2,6 +2,7 @@ import { mock } from "jest-mock-extended"; import { I18nService } from "@bitwarden/common/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/abstractions/log.service"; +import { MessagingService } from "@bitwarden/common/abstractions/messaging.service"; import { StateService } from "@bitwarden/common/abstractions/state.service"; import { WindowMain } from "../window.main"; @@ -16,9 +17,16 @@ describe("biometrics tests", function () { const windowMain = mock(); const stateService = mock(); const logService = mock(); + const messagingService = mock(); it("Should call the platformspecific methods", () => { - const sut = new BiometricsService(i18nService, windowMain, stateService, logService); + const sut = new BiometricsService( + i18nService, + windowMain, + stateService, + logService, + messagingService + ); const mockService = mock(); (sut as any).platformSpecificService = mockService; @@ -42,7 +50,13 @@ describe("biometrics tests", function () { }); }); - const sut = new BiometricsService(i18nService, windowMain, stateService, logService); + const sut = new BiometricsService( + i18nService, + windowMain, + stateService, + logService, + messagingService + ); it("Should create a biometrics service specific for Windows", () => { const internalService = (sut as any).platformSpecificService; @@ -68,7 +82,13 @@ describe("biometrics tests", function () { }); it("Should create a biometrics service specific for MacOs", () => { - const sut = new BiometricsService(i18nService, windowMain, stateService, logService); + const sut = new BiometricsService( + i18nService, + windowMain, + stateService, + logService, + messagingService + ); const internalService = (sut as any).platformSpecificService; expect(internalService).not.toBeNull(); expect(internalService).toBeInstanceOf(BiometricDarwinMain); diff --git a/apps/desktop/src/main/biometric/biometrics.service.ts b/apps/desktop/src/main/biometric/biometrics.service.ts index b117b0f41f..74b96cbed2 100644 --- a/apps/desktop/src/main/biometric/biometrics.service.ts +++ b/apps/desktop/src/main/biometric/biometrics.service.ts @@ -1,5 +1,6 @@ import { I18nService } from "@bitwarden/common/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/abstractions/log.service"; +import { MessagingService } from "@bitwarden/common/abstractions/messaging.service"; import { StateService } from "@bitwarden/common/abstractions/state.service"; import { WindowMain } from "../window.main"; @@ -13,7 +14,8 @@ export class BiometricsService implements BiometricsServiceAbstraction { private i18nService: I18nService, private windowMain: WindowMain, private stateService: StateService, - private logService: LogService + private logService: LogService, + private messagingService: MessagingService ) { this.loadPlatformSpecificService(); } @@ -52,6 +54,11 @@ export class BiometricsService implements BiometricsServiceAbstraction { } async authenticateBiometric(): Promise { - return await this.platformSpecificService.authenticateBiometric(); + this.messagingService.send("cancelProcessReload"); + const response = await this.platformSpecificService.authenticateBiometric(); + if (!response) { + this.messagingService.send("startProcessReload"); + } + return response; } } diff --git a/libs/common/src/services/system.service.ts b/libs/common/src/services/system.service.ts index dc67a7f1e2..4e1dad9af5 100644 --- a/libs/common/src/services/system.service.ts +++ b/libs/common/src/services/system.service.ts @@ -45,34 +45,23 @@ export class SystemService implements SystemServiceAbstraction { } this.cancelProcessReload(); - this.reloadInterval = setInterval(async () => await this.executeProcessReload(), 10000); - } - - private async inactiveMoreThanSeconds(seconds: number): Promise { - const lastActive = await this.stateService.getLastActive(); - if (lastActive != null) { - const diffMs = new Date().getTime() - lastActive; - return diffMs >= seconds * 1000; - } - return true; + await this.executeProcessReload(); } private async executeProcessReload() { - const accounts = await firstValueFrom(this.stateService.accounts$); - const doRefresh = - accounts == null || - Object.keys(accounts).length == 0 || - (await this.inactiveMoreThanSeconds(5)); - const biometricLockedFingerprintValidated = await this.stateService.getBiometricFingerprintValidated(); - if (doRefresh && !biometricLockedFingerprintValidated) { + if (!biometricLockedFingerprintValidated) { clearInterval(this.reloadInterval); this.reloadInterval = null; this.messagingService.send("reloadProcess"); if (this.reloadCallback != null) { await this.reloadCallback(); } + return; + } + if (this.reloadInterval == null) { + this.reloadInterval = setInterval(async () => await this.executeProcessReload(), 1000); } }