From a85c45a34ed90b09f59ec27bdba754d66452915e Mon Sep 17 00:00:00 2001 From: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Date: Fri, 10 Sep 2021 08:57:32 +1000 Subject: [PATCH] Use a modal to set the unlock pin (#477) * Move set pin logic to its own modal * Fix method name and default value --- angular/src/components/set-pin.component.ts | 49 +++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 angular/src/components/set-pin.component.ts diff --git a/angular/src/components/set-pin.component.ts b/angular/src/components/set-pin.component.ts new file mode 100644 index 0000000000..41a2383ce7 --- /dev/null +++ b/angular/src/components/set-pin.component.ts @@ -0,0 +1,49 @@ +import { Directive } from '@angular/core'; + +import { CryptoService } from 'jslib-common/abstractions/crypto.service'; +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() +export class SetPinComponent { + + pin = ''; + showPin = false; + masterPassOnRestart = true; + + constructor(private modalRef: ModalRef, private cryptoService: CryptoService, private userService: UserService, + private storageService: StorageService, private vaultTimeoutService: VaultTimeoutService) { } + + 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); + } +}