From f383ee502787d519ff041e75b14f1dd4038477a3 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 9 Jan 2018 23:18:51 -0500 Subject: [PATCH] move lock service ti jslib --- src/abstractions/index.ts | 1 + src/abstractions/lock.service.ts | 4 ++ src/services/index.ts | 1 + src/services/lock.service.ts | 65 ++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 src/abstractions/lock.service.ts create mode 100644 src/services/lock.service.ts diff --git a/src/abstractions/index.ts b/src/abstractions/index.ts index 893a189120..1bd679e41c 100644 --- a/src/abstractions/index.ts +++ b/src/abstractions/index.ts @@ -5,6 +5,7 @@ export { CollectionService } from './collection.service'; export { CryptoService } from './crypto.service'; export { EnvironmentService } from './environment.service'; export { FolderService } from './folder.service'; +export { LockService } from './lock.service'; export { MessagingService } from './messaging.service'; export { PasswordGenerationService } from './passwordGeneration.service'; export { PlatformUtilsService } from './platformUtils.service'; diff --git a/src/abstractions/lock.service.ts b/src/abstractions/lock.service.ts new file mode 100644 index 0000000000..2606f617ac --- /dev/null +++ b/src/abstractions/lock.service.ts @@ -0,0 +1,4 @@ +export interface LockService { + checkLock(): Promise; + lock(): Promise; +} diff --git a/src/services/index.ts b/src/services/index.ts index 0412506aef..a01df662ae 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -7,6 +7,7 @@ export { ContainerService } from './container.service'; export { CryptoService } from './crypto.service'; export { EnvironmentService } from './environment.service'; export { FolderService } from './folder.service'; +export { LockService } from './lock.service'; export { PasswordGenerationService } from './passwordGeneration.service'; export { SettingsService } from './settings.service'; export { TokenService } from './token.service'; diff --git a/src/services/lock.service.ts b/src/services/lock.service.ts new file mode 100644 index 0000000000..e3b841a9a3 --- /dev/null +++ b/src/services/lock.service.ts @@ -0,0 +1,65 @@ +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 LockServiceInterface } from '../abstractions/lock.service'; +import { PlatformUtilsService } from '../abstractions/platformUtils.service'; +import { StorageService } from '../abstractions/storage.service'; + +export class LockService implements LockServiceInterface { + constructor(private cipherService: CipherService, private folderService: FolderService, + private collectionService: CollectionService, private cryptoService: CryptoService, + private platformUtilsService: PlatformUtilsService, + private storageService: StorageService, + private setIcon: Function, private refreshBadgeAndMenu: Function) { + this.checkLock(); + setInterval(() => this.checkLock(), 10 * 1000); // check every 10 seconds + } + + async checkLock(): Promise { + if (this.platformUtilsService.isViewOpen()) { + // Do not lock + return; + } + + const key = await this.cryptoService.getKey(); + if (key == null) { + // no key so no need to lock + return; + } + + const lockOption = await this.storageService.get(ConstantsService.lockOptionKey); + if (lockOption == null || lockOption < 0) { + return; + } + + const lastActive = await this.storageService.get(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 { + await Promise.all([ + this.cryptoService.clearKey(), + this.cryptoService.clearOrgKeys(true), + this.cryptoService.clearPrivateKey(true), + this.cryptoService.clearEncKey(true), + this.setIcon(), + this.refreshBadgeAndMenu(), + ]); + + this.folderService.clearCache(); + this.cipherService.clearCache(); + this.collectionService.clearCache(); + } +}