mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-23 11:56:00 +01:00
[Auto-Logout] Implement logout functionality in VaultTimeoutService (#92)
* Initial commit for logic changes in VaultTimeoutService * Fixed lint error * Updated logOut spelling - as an action its two words * Hitting save to make sure all my changes are included * Made requested changes Co-authored-by: Vincent Salucci <vsalucci@bitwarden.com>
This commit is contained in:
parent
64c54cfb86
commit
28e3fff739
@ -5,7 +5,7 @@ export abstract class VaultTimeoutService {
|
|||||||
isLocked: () => Promise<boolean>;
|
isLocked: () => Promise<boolean>;
|
||||||
checkVaultTimeout: () => Promise<void>;
|
checkVaultTimeout: () => Promise<void>;
|
||||||
lock: (allowSoftLock?: boolean) => Promise<void>;
|
lock: (allowSoftLock?: boolean) => Promise<void>;
|
||||||
logout: () => Promise<void>;
|
logOut: () => Promise<void>;
|
||||||
setVaultTimeoutOptions: (vaultTimeout: number, vaultTimeoutAction: string) => Promise<void>;
|
setVaultTimeoutOptions: (vaultTimeout: number, vaultTimeoutAction: string) => Promise<void>;
|
||||||
isPinLockSet: () => Promise<[boolean, boolean]>;
|
isPinLockSet: () => Promise<[boolean, boolean]>;
|
||||||
clear: () => Promise<any>;
|
clear: () => Promise<any>;
|
||||||
|
@ -22,7 +22,8 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
|
|||||||
private collectionService: CollectionService, private cryptoService: CryptoService,
|
private collectionService: CollectionService, private cryptoService: CryptoService,
|
||||||
private platformUtilsService: PlatformUtilsService, private storageService: StorageService,
|
private platformUtilsService: PlatformUtilsService, private storageService: StorageService,
|
||||||
private messagingService: MessagingService, private searchService: SearchService,
|
private messagingService: MessagingService, private searchService: SearchService,
|
||||||
private userService: UserService, private lockedCallback: () => Promise<void> = null) {
|
private userService: UserService, private lockedCallback: () => Promise<void> = null,
|
||||||
|
private loggedOutCallback: () => Promise<void> = null) {
|
||||||
}
|
}
|
||||||
|
|
||||||
init(checkOnInterval: boolean) {
|
init(checkOnInterval: boolean) {
|
||||||
@ -37,6 +38,7 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Keys aren't stored for a device that is locked or logged out.
|
||||||
async isLocked(): Promise<boolean> {
|
async isLocked(): Promise<boolean> {
|
||||||
const hasKey = await this.cryptoService.hasKey();
|
const hasKey = await this.cryptoService.hasKey();
|
||||||
return !hasKey;
|
return !hasKey;
|
||||||
@ -58,11 +60,13 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let lockOption = this.platformUtilsService.lockTimeout();
|
// This has the potential to be removed. Evaluate after all platforms complete with auto-logout
|
||||||
if (lockOption == null) {
|
let vaultTimeout = this.platformUtilsService.lockTimeout();
|
||||||
lockOption = await this.storageService.get<number>(ConstantsService.vaultTimeoutKey);
|
if (vaultTimeout == null) {
|
||||||
|
vaultTimeout = await this.storageService.get<number>(ConstantsService.vaultTimeoutKey);
|
||||||
}
|
}
|
||||||
if (lockOption == null || lockOption < 0) {
|
|
||||||
|
if (vaultTimeout == null || vaultTimeout < 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,12 +75,12 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO update with vault timeout name and pivot based on action saved
|
const vaultTimeoutSeconds = vaultTimeout * 60;
|
||||||
const lockOptionSeconds = lockOption * 60;
|
|
||||||
const diffSeconds = ((new Date()).getTime() - lastActive) / 1000;
|
const diffSeconds = ((new Date()).getTime() - lastActive) / 1000;
|
||||||
if (diffSeconds >= lockOptionSeconds) {
|
if (diffSeconds >= vaultTimeoutSeconds) {
|
||||||
// need to lock now
|
// Pivot based on the saved vault timeout action
|
||||||
await this.lock(true);
|
const timeoutAction = await this.storageService.get<string>(ConstantsService.vaultTimeoutActionKey);
|
||||||
|
timeoutAction === 'lock' ? await this.lock(true) : await this.logOut();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,13 +107,15 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async logout(): Promise<void> {
|
async logOut(): Promise<void> {
|
||||||
// TODO Add logic for loggedOutCallback
|
if (this.loggedOutCallback != null) {
|
||||||
|
await this.loggedOutCallback();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async setVaultTimeoutOptions(vaultTimeout: number, vaultTimeoutAction: string): Promise<void> {
|
async setVaultTimeoutOptions(timeout: number, action: string): Promise<void> {
|
||||||
await this.storageService.save(ConstantsService.vaultTimeoutKey, vaultTimeout);
|
await this.storageService.save(ConstantsService.vaultTimeoutKey, timeout);
|
||||||
// TODO Add logic for vaultTimeoutAction
|
await this.storageService.save(ConstantsService.vaultTimeoutActionKey, action);
|
||||||
await this.cryptoService.toggleKey();
|
await this.cryptoService.toggleKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user