move lock service ti jslib

This commit is contained in:
Kyle Spearrin 2018-01-09 23:18:51 -05:00
parent 8e64181a5d
commit f383ee5027
4 changed files with 71 additions and 0 deletions

View File

@ -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';

View File

@ -0,0 +1,4 @@
export interface LockService {
checkLock(): Promise<void>;
lock(): Promise<void>;
}

View File

@ -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';

View File

@ -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<void> {
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<number>(ConstantsService.lockOptionKey);
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),
this.cryptoService.clearPrivateKey(true),
this.cryptoService.clearEncKey(true),
this.setIcon(),
this.refreshBadgeAndMenu(),
]);
this.folderService.clearCache();
this.cipherService.clearCache();
this.collectionService.clearCache();
}
}