2018-01-10 05:18:51 +01:00
|
|
|
import { ConstantsService } from './constants.service';
|
|
|
|
|
2018-02-19 19:07:19 +01:00
|
|
|
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';
|
2018-02-19 19:07:19 +01:00
|
|
|
import { StorageService } from '../abstractions/storage.service';
|
2020-04-06 18:06:32 +02:00
|
|
|
import { TokenService } from '../abstractions/token.service';
|
2019-03-19 20:44:48 +01:00
|
|
|
import { UserService } from '../abstractions/user.service';
|
2020-03-27 15:03:27 +01:00
|
|
|
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';
|
|
|
|
|
2020-03-27 15:03:27 +01:00
|
|
|
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,
|
2020-04-06 18:06:32 +02:00
|
|
|
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) {
|
2020-03-27 15:03:27 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-29 17:38:16 +02: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;
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:03:27 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:03:27 +01:00
|
|
|
// "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;
|
|
|
|
}
|
|
|
|
|
2020-03-29 17:38:16 +02:00
|
|
|
// 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
|
|
|
}
|
2020-03-29 17:38:16 +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;
|
|
|
|
}
|
|
|
|
|
2020-03-29 17:38:16 +02:00
|
|
|
const vaultTimeoutSeconds = vaultTimeout * 60;
|
2018-01-10 05:18:51 +01:00
|
|
|
const diffSeconds = ((new Date()).getTime() - lastActive) / 1000;
|
2020-03-29 17:38:16 +02:00
|
|
|
if (diffSeconds >= vaultTimeoutSeconds) {
|
|
|
|
// Pivot based on the saved vault timeout action
|
|
|
|
const timeoutAction = await this.storageService.get<string>(ConstantsService.vaultTimeoutActionKey);
|
2020-05-28 20:09:55 +02:00
|
|
|
timeoutAction === 'logOut' ? await this.logOut() : await this.lock(true);
|
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
|
|
|
|
2020-03-29 17:38:16 +02:00
|
|
|
async logOut(): Promise<void> {
|
|
|
|
if (this.loggedOutCallback != null) {
|
|
|
|
await this.loggedOutCallback();
|
|
|
|
}
|
2020-03-27 15:03:27 +01:00
|
|
|
}
|
|
|
|
|
2020-03-29 17:38:16 +02:00
|
|
|
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();
|
2020-04-06 18:06:32 +02:00
|
|
|
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
|
|
|
}
|