Timeout Vault of Inactive Users on System Lock (#7194)

This commit is contained in:
Justin Baur 2023-12-13 09:11:42 -05:00 committed by GitHub
parent fd85d13b18
commit 180d3a99e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 14 deletions

View File

@ -1,5 +1,8 @@
import { firstValueFrom } from "rxjs";
import { NotificationsService } from "@bitwarden/common/abstractions/notifications.service"; import { NotificationsService } from "@bitwarden/common/abstractions/notifications.service";
import { VaultTimeoutService } from "@bitwarden/common/abstractions/vault-timeout/vault-timeout.service"; import { VaultTimeoutService } from "@bitwarden/common/abstractions/vault-timeout/vault-timeout.service";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { VaultTimeoutAction } from "@bitwarden/common/enums/vault-timeout-action.enum"; import { VaultTimeoutAction } from "@bitwarden/common/enums/vault-timeout-action.enum";
import { BrowserStateService } from "../platform/services/abstractions/browser-state.service"; import { BrowserStateService } from "../platform/services/abstractions/browser-state.service";
@ -7,7 +10,7 @@ import { BrowserStateService } from "../platform/services/abstractions/browser-s
const IdleInterval = 60 * 5; // 5 minutes const IdleInterval = 60 * 5; // 5 minutes
export default class IdleBackground { export default class IdleBackground {
private idle: any; private idle: typeof chrome.idle | typeof browser.idle | null;
private idleTimer: number = null; private idleTimer: number = null;
private idleState = "active"; private idleState = "active";
@ -15,6 +18,7 @@ export default class IdleBackground {
private vaultTimeoutService: VaultTimeoutService, private vaultTimeoutService: VaultTimeoutService,
private stateService: BrowserStateService, private stateService: BrowserStateService,
private notificationsService: NotificationsService, private notificationsService: NotificationsService,
private accountService: AccountService,
) { ) {
this.idle = chrome.idle || (browser != null ? browser.idle : null); this.idle = chrome.idle || (browser != null ? browser.idle : null);
} }
@ -39,21 +43,27 @@ export default class IdleBackground {
} }
if (this.idle.onStateChanged) { if (this.idle.onStateChanged) {
this.idle.onStateChanged.addListener(async (newState: string) => { this.idle.onStateChanged.addListener(
if (newState === "locked") { async (newState: chrome.idle.IdleState | browser.idle.IdleState) => {
// If the screen is locked or the screensaver activates if (newState === "locked") {
const timeout = await this.stateService.getVaultTimeout(); // Need to check if any of the current users have their timeout set to `onLocked`
if (timeout === -2) { const allUsers = await firstValueFrom(this.accountService.accounts$);
// On System Lock vault timeout option for (const userId in allUsers) {
const action = await this.stateService.getVaultTimeoutAction(); // If the screen is locked or the screensaver activates
if (action === VaultTimeoutAction.LogOut) { const timeout = await this.stateService.getVaultTimeout({ userId: userId });
await this.vaultTimeoutService.logOut(); if (timeout === -2) {
} else { // On System Lock vault timeout option
await this.vaultTimeoutService.lock(); const action = await this.stateService.getVaultTimeoutAction({ userId: userId });
if (action === VaultTimeoutAction.LogOut) {
await this.vaultTimeoutService.logOut(userId);
} else {
await this.vaultTimeoutService.lock(userId);
}
}
} }
} }
} },
}); );
} }
} }

View File

@ -729,6 +729,7 @@ export default class MainBackground {
this.vaultTimeoutService, this.vaultTimeoutService,
this.stateService, this.stateService,
this.notificationsService, this.notificationsService,
this.accountService,
); );
this.webRequestBackground = new WebRequestBackground( this.webRequestBackground = new WebRequestBackground(
this.platformUtilsService, this.platformUtilsService,