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

96 lines
3.4 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 { 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';
import { StorageService } from '../abstractions/storage.service';
2018-01-10 05:18:51 +01:00
2018-02-10 16:53:07 +01:00
export class LockService implements LockServiceAbstraction {
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 lockedCallback: () => Promise<void>) {
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
}
async checkLock(): Promise<void> {
if (this.platformUtilsService.isViewOpen()) {
// Do not lock
return;
}
2018-06-13 23:14:26 +02:00
const hasKey = await this.cryptoService.hasKey();
if (!hasKey) {
2018-01-10 05:18:51 +01:00
// no key so no need to lock
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
await this.lock();
}
}
async lock(): Promise<void> {
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
async isPinLockSet(): Promise<boolean> {
const pinProtectedKey = await this.storageService.get<string>(ConstantsService.pinProtectedKey);
return pinProtectedKey != null;
}
2018-01-10 05:18:51 +01:00
}