bitwarden-estensione-browser/src/services/vaultTimeout.service.ts

135 lines
5.1 KiB
TypeScript
Raw Normal View History

2018-01-10 05:18:51 +01:00
import { ConstantsService } from './constants.service';
import { CipherService } from '../abstractions/cipher.service';
import { CollectionService } from '../abstractions/collection.service';
import { CryptoService } from '../abstractions/crypto.service';
import { FolderService } from '../abstractions/folder.service';
import { MessagingService } from '../abstractions/messaging.service';
import { PlatformUtilsService } from '../abstractions/platformUtils.service';
2018-08-13 20:09:10 +02:00
import { SearchService } from '../abstractions/search.service';
import { StorageService } from '../abstractions/storage.service';
import { TokenService } from '../abstractions/token.service';
2019-03-19 20:44:48 +01:00
import { UserService } from '../abstractions/user.service';
import { VaultTimeoutService as VaultTimeoutServiceAbstraction } from '../abstractions/vaultTimeout.service';
2018-01-10 05:18:51 +01:00
2019-08-29 15:40:50 +02:00
import { CipherString } from '../models/domain/cipherString';
export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
2019-08-29 15:40:50 +02:00
pinProtectedKey: CipherString = null;
2019-02-14 03:36:36 +01:00
2018-05-16 16:18:05 +02:00
private inited = false;
2018-01-10 05:18:51 +01:00
constructor(private cipherService: CipherService, private folderService: FolderService,
private collectionService: CollectionService, private cryptoService: CryptoService,
2018-02-10 16:53:07 +01:00
private platformUtilsService: PlatformUtilsService, private storageService: StorageService,
2018-08-13 20:09:10 +02:00
private messagingService: MessagingService, private searchService: SearchService,
private userService: UserService, private tokenService: TokenService,
private lockedCallback: () => Promise<void> = null, private loggedOutCallback: () => Promise<void> = null) {
2018-05-16 16:18:05 +02:00
}
init(checkOnInterval: boolean) {
if (this.inited) {
return;
}
this.inited = true;
if (checkOnInterval) {
this.checkVaultTimeout();
setInterval(() => this.checkVaultTimeout(), 10 * 1000); // check every 10 seconds
2018-05-16 16:18:05 +02:00
}
2018-01-10 05:18:51 +01:00
}
// Keys aren't stored for a device that is locked or logged out.
2019-02-14 03:36:36 +01:00
async isLocked(): Promise<boolean> {
2019-02-14 04:08:55 +01:00
const hasKey = await this.cryptoService.hasKey();
2019-02-14 03:36:36 +01:00
return !hasKey;
}
async checkVaultTimeout(): Promise<void> {
2019-08-20 19:47:15 +02:00
if (await this.platformUtilsService.isViewOpen()) {
2018-01-10 05:18:51 +01:00
// Do not lock
return;
}
// "is logged out check" - similar to isLocked, below
2019-03-19 20:44:48 +01:00
const authed = await this.userService.isAuthenticated();
if (!authed) {
return;
}
2019-02-25 22:14:54 +01:00
if (await this.isLocked()) {
2018-01-10 05:18:51 +01:00
return;
}
// This has the potential to be removed. Evaluate after all platforms complete with auto-logout
let vaultTimeout = this.platformUtilsService.lockTimeout();
if (vaultTimeout == null) {
vaultTimeout = await this.storageService.get<number>(ConstantsService.vaultTimeoutKey);
2018-06-09 20:50:18 +02:00
}
if (vaultTimeout == null || vaultTimeout < 0) {
2018-01-10 05:18:51 +01:00
return;
}
const lastActive = await this.storageService.get<number>(ConstantsService.lastActiveKey);
if (lastActive == null) {
return;
}
const vaultTimeoutSeconds = vaultTimeout * 60;
2018-01-10 05:18:51 +01:00
const diffSeconds = ((new Date()).getTime() - lastActive) / 1000;
if (diffSeconds >= vaultTimeoutSeconds) {
// Pivot based on the saved vault timeout action
const timeoutAction = await this.storageService.get<string>(ConstantsService.vaultTimeoutActionKey);
timeoutAction === 'lock' ? await this.lock(true) : await this.logOut();
2018-01-10 05:18:51 +01:00
}
}
2019-02-14 03:36:36 +01:00
async lock(allowSoftLock = false): Promise<void> {
2019-03-19 20:44:48 +01:00
const authed = await this.userService.isAuthenticated();
if (!authed) {
return;
}
2018-01-10 05:18:51 +01:00
await Promise.all([
this.cryptoService.clearKey(),
this.cryptoService.clearOrgKeys(true),
2018-07-03 17:41:55 +02:00
this.cryptoService.clearKeyPair(true),
2018-01-10 05:18:51 +01:00
this.cryptoService.clearEncKey(true),
]);
this.folderService.clearCache();
this.cipherService.clearCache();
this.collectionService.clearCache();
2018-08-13 20:09:10 +02:00
this.searchService.clearIndex();
2018-02-10 16:53:07 +01:00
this.messagingService.send('locked');
2018-05-16 16:18:05 +02:00
if (this.lockedCallback != null) {
await this.lockedCallback();
}
2018-01-10 05:18:51 +01:00
}
2018-02-11 06:38:17 +01:00
async logOut(): Promise<void> {
if (this.loggedOutCallback != null) {
await this.loggedOutCallback();
}
}
async setVaultTimeoutOptions(timeout: number, action: string): Promise<void> {
await this.storageService.save(ConstantsService.vaultTimeoutKey, timeout);
await this.storageService.save(ConstantsService.vaultTimeoutActionKey, action);
2018-02-11 06:38:17 +01:00
await this.cryptoService.toggleKey();
await this.tokenService.toggleTokens();
2018-02-11 06:38:17 +01:00
}
2019-02-13 05:52:50 +01:00
2019-02-14 03:36:36 +01:00
async isPinLockSet(): Promise<[boolean, boolean]> {
const protectedPin = await this.storageService.get<string>(ConstantsService.protectedPin);
2019-02-13 05:52:50 +01:00
const pinProtectedKey = await this.storageService.get<string>(ConstantsService.pinProtectedKey);
2019-02-14 03:36:36 +01:00
return [protectedPin != null, pinProtectedKey != null];
}
clear(): Promise<any> {
2019-08-29 15:40:50 +02:00
this.pinProtectedKey = null;
2019-02-14 03:36:36 +01:00
return this.storageService.remove(ConstantsService.protectedPin);
}
2018-01-10 05:18:51 +01:00
}