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 { LockService as LockServiceAbstraction } from '../abstractions/lock.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';
|
2019-03-19 20:44:48 +01:00
|
|
|
import { UserService } from '../abstractions/user.service';
|
2018-01-10 05:18:51 +01:00
|
|
|
|
2018-02-10 16:53:07 +01:00
|
|
|
export class LockService implements LockServiceAbstraction {
|
2019-02-14 03:36:36 +01:00
|
|
|
pinLocked = false;
|
|
|
|
|
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,
|
2019-03-19 20:44:48 +01:00
|
|
|
private userService: UserService, private lockedCallback: () => Promise<void> = null) {
|
2018-05-16 16:18:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
init(checkOnInterval: boolean) {
|
|
|
|
if (this.inited) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.inited = true;
|
|
|
|
if (checkOnInterval) {
|
|
|
|
this.checkLock();
|
|
|
|
setInterval(() => this.checkLock(), 10 * 1000); // check every 10 seconds
|
|
|
|
}
|
2018-01-10 05:18:51 +01:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
if (hasKey && this.pinLocked) {
|
2019-02-14 03:36:36 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return !hasKey;
|
|
|
|
}
|
|
|
|
|
2018-01-10 05:18:51 +01:00
|
|
|
async checkLock(): 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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-06-09 20:50:18 +02:00
|
|
|
let lockOption = this.platformUtilsService.lockTimeout();
|
|
|
|
if (lockOption == null) {
|
|
|
|
lockOption = await this.storageService.get<number>(ConstantsService.lockOptionKey);
|
|
|
|
}
|
2018-01-10 05:18:51 +01:00
|
|
|
if (lockOption == null || lockOption < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const lastActive = await this.storageService.get<number>(ConstantsService.lastActiveKey);
|
|
|
|
if (lastActive == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const lockOptionSeconds = lockOption * 60;
|
|
|
|
const diffSeconds = ((new Date()).getTime() - lastActive) / 1000;
|
|
|
|
if (diffSeconds >= lockOptionSeconds) {
|
|
|
|
// need to lock now
|
2019-02-14 03:36:36 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-14 03:36:36 +01:00
|
|
|
if (allowSoftLock) {
|
|
|
|
const pinSet = await this.isPinLockSet();
|
|
|
|
if (pinSet[0]) {
|
2019-02-14 04:08:55 +01:00
|
|
|
this.pinLocked = true;
|
|
|
|
this.messagingService.send('locked');
|
|
|
|
if (this.lockedCallback != null) {
|
|
|
|
await this.lockedCallback();
|
|
|
|
}
|
2019-02-14 03:36:36 +01:00
|
|
|
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 setLockOption(lockOption: number): Promise<void> {
|
|
|
|
await this.storageService.save(ConstantsService.lockOptionKey, lockOption);
|
|
|
|
await this.cryptoService.toggleKey();
|
|
|
|
}
|
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> {
|
|
|
|
return this.storageService.remove(ConstantsService.protectedPin);
|
|
|
|
}
|
2018-01-10 05:18:51 +01:00
|
|
|
}
|