mirror of
https://github.com/bitwarden/jslib
synced 2025-12-23 11:43:51 +00:00
[review] [refactor] Extract some timeout logic to dedicated functions
This commit is contained in:
@@ -55,37 +55,12 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
|
|||||||
|
|
||||||
async checkVaultTimeout(): Promise<void> {
|
async checkVaultTimeout(): Promise<void> {
|
||||||
if (await this.platformUtilsService.isViewOpen()) {
|
if (await this.platformUtilsService.isViewOpen()) {
|
||||||
// Do not lock
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const userId in this.stateService.accounts.getValue()) {
|
for (const userId in this.stateService.accounts.getValue()) {
|
||||||
if (userId != null) {
|
if (userId != null || await this.shouldLock(userId)) {
|
||||||
if (await this.isLoggedOut(userId)) {
|
this.executeTimeoutAction(userId);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (await this.isLocked(userId)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const vaultTimeout = await this.getVaultTimeout(userId);
|
|
||||||
if (vaultTimeout == null || vaultTimeout < 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const lastActive = await this.stateService.getLastActive({ userId: userId });
|
|
||||||
if (lastActive == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const vaultTimeoutSeconds = vaultTimeout * 60;
|
|
||||||
const diffSeconds = ((new Date()).getTime() - lastActive) / 1000;
|
|
||||||
if (diffSeconds >= vaultTimeoutSeconds) {
|
|
||||||
// Pivot based on the saved vault timeout action
|
|
||||||
const timeoutAction = await this.stateService.getVaultTimeoutAction({ userId: userId });
|
|
||||||
timeoutAction === 'logOut' ? await this.logOut() : await this.lock(true, userId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -180,4 +155,33 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
|
|||||||
private async isLoggedOut(userId?: string): Promise<boolean> {
|
private async isLoggedOut(userId?: string): Promise<boolean> {
|
||||||
return !(await this.stateService.getIsAuthenticated({ userId: userId }));
|
return !(await this.stateService.getIsAuthenticated({ userId: userId }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async shouldLock(userId: string): Promise<boolean> {
|
||||||
|
if (await this.isLoggedOut(userId)) {
|
||||||
|
return false;;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await this.isLocked(userId)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const vaultTimeout = await this.getVaultTimeout(userId);
|
||||||
|
if (vaultTimeout == null || vaultTimeout < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastActive = await this.stateService.getLastActive({ userId: userId });
|
||||||
|
if (lastActive == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const vaultTimeoutSeconds = vaultTimeout * 60;
|
||||||
|
const diffSeconds = ((new Date()).getTime() - lastActive) / 1000;
|
||||||
|
return diffSeconds >= vaultTimeoutSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async executeTimeoutAction(userId: string): Promise<void> {
|
||||||
|
const timeoutAction = await this.stateService.getVaultTimeoutAction({ userId: userId });
|
||||||
|
timeoutAction === 'logOut' ? await this.logOut() : await this.lock(true, userId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user