2021-11-18 12:20:55 +01:00
|
|
|
import {
|
|
|
|
Directive,
|
|
|
|
OnInit
|
|
|
|
} from '@angular/core';
|
2021-09-10 00:57:32 +02:00
|
|
|
|
|
|
|
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
|
2021-11-18 12:20:55 +01:00
|
|
|
import { KeyConnectorService } from 'jslib-common/abstractions/keyConnector.service';
|
2021-09-10 00:57:32 +02:00
|
|
|
import { StorageService } from 'jslib-common/abstractions/storage.service';
|
|
|
|
import { UserService } from 'jslib-common/abstractions/user.service';
|
|
|
|
import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service';
|
|
|
|
|
|
|
|
import { ConstantsService } from 'jslib-common/services/constants.service';
|
|
|
|
|
|
|
|
import { Utils } from 'jslib-common/misc/utils';
|
|
|
|
|
|
|
|
import { ModalRef } from './modal/modal.ref';
|
|
|
|
|
|
|
|
@Directive()
|
2021-11-18 12:20:55 +01:00
|
|
|
export class SetPinComponent implements OnInit {
|
2021-09-10 00:57:32 +02:00
|
|
|
|
|
|
|
pin = '';
|
|
|
|
showPin = false;
|
|
|
|
masterPassOnRestart = true;
|
2021-11-18 12:20:55 +01:00
|
|
|
showMasterPassOnRestart = true;
|
2021-09-10 00:57:32 +02:00
|
|
|
|
|
|
|
constructor(private modalRef: ModalRef, private cryptoService: CryptoService, private userService: UserService,
|
2021-11-18 12:20:55 +01:00
|
|
|
private storageService: StorageService, private vaultTimeoutService: VaultTimeoutService,
|
|
|
|
private keyConnectorService: KeyConnectorService) { }
|
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
this.showMasterPassOnRestart = this.masterPassOnRestart = !await this.keyConnectorService.getUsesKeyConnector();
|
|
|
|
}
|
2021-09-10 00:57:32 +02:00
|
|
|
|
|
|
|
toggleVisibility() {
|
|
|
|
this.showPin = !this.showPin;
|
|
|
|
}
|
|
|
|
|
|
|
|
async submit() {
|
|
|
|
if (Utils.isNullOrWhitespace(this.pin)) {
|
|
|
|
this.modalRef.close(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
const kdf = await this.userService.getKdf();
|
|
|
|
const kdfIterations = await this.userService.getKdfIterations();
|
|
|
|
const email = await this.userService.getEmail();
|
|
|
|
const pinKey = await this.cryptoService.makePinKey(this.pin, email, kdf, kdfIterations);
|
|
|
|
const key = await this.cryptoService.getKey();
|
|
|
|
const pinProtectedKey = await this.cryptoService.encrypt(key.key, pinKey);
|
|
|
|
if (this.masterPassOnRestart) {
|
|
|
|
const encPin = await this.cryptoService.encrypt(this.pin);
|
|
|
|
await this.storageService.save(ConstantsService.protectedPin, encPin.encryptedString);
|
|
|
|
this.vaultTimeoutService.pinProtectedKey = pinProtectedKey;
|
|
|
|
} else {
|
|
|
|
await this.storageService.save(ConstantsService.pinProtectedKey, pinProtectedKey.encryptedString);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.modalRef.close(true);
|
|
|
|
}
|
|
|
|
}
|