Improve reloading with biometrics

This commit is contained in:
Daniel James Smith 2023-02-09 15:31:45 +01:00
parent 3f98c18f76
commit e52a506d86
No known key found for this signature in database
GPG Key ID: 03E4BD365FF06726
5 changed files with 46 additions and 23 deletions

View File

@ -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;

View File

@ -121,7 +121,8 @@ export class Main {
this.i18nService,
this.windowMain,
this.stateService,
this.logService
this.logService,
this.messagingService
);
this.desktopCredentialStorageListener = new DesktopCredentialStorageListener(

View File

@ -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<WindowMain>();
const stateService = mock<StateService>();
const logService = mock<LogService>();
const messagingService = mock<MessagingService>();
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<BiometricsServiceAbstraction>();
(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);

View File

@ -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<boolean> {
return await this.platformSpecificService.authenticateBiometric();
this.messagingService.send("cancelProcessReload");
const response = await this.platformSpecificService.authenticateBiometric();
if (!response) {
this.messagingService.send("startProcessReload");
}
return response;
}
}

View File

@ -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<boolean> {
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);
}
}