Files
clients/src/angular/components/lock.component.ts

141 lines
5.9 KiB
TypeScript
Raw Normal View History

2019-01-07 23:30:04 -05:00
import { OnInit } from '@angular/core';
2018-04-04 22:59:14 -04:00
import { Router } from '@angular/router';
import { CryptoService } from '../../abstractions/crypto.service';
import { EnvironmentService } from '../../abstractions/environment.service';
2018-04-04 22:59:14 -04:00
import { I18nService } from '../../abstractions/i18n.service';
2019-02-12 23:52:50 -05:00
import { LockService } from '../../abstractions/lock.service';
2018-04-04 22:59:14 -04:00
import { MessagingService } from '../../abstractions/messaging.service';
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
import { StateService } from '../../abstractions/state.service';
2019-02-12 23:52:50 -05:00
import { StorageService } from '../../abstractions/storage.service';
2018-04-04 22:59:14 -04:00
import { UserService } from '../../abstractions/user.service';
2019-02-12 23:52:50 -05:00
import { ConstantsService } from '../../services/constants.service';
import { CipherString } from '../../models/domain/cipherString';
import { SymmetricCryptoKey } from '../../models/domain/symmetricCryptoKey';
2019-06-03 09:53:18 -04:00
import { Utils } from '../../misc/utils';
2019-01-07 23:30:04 -05:00
export class LockComponent implements OnInit {
2018-04-04 22:59:14 -04:00
masterPassword: string = '';
2019-02-12 23:52:50 -05:00
pin: string = '';
2018-04-04 22:59:14 -04:00
showPassword: boolean = false;
2019-01-07 23:30:04 -05:00
email: string;
2019-02-12 23:52:50 -05:00
pinLock: boolean = false;
webVaultHostname: string = '';
2018-04-04 22:59:14 -04:00
protected successRoute: string = 'vault';
2018-10-15 13:02:31 -04:00
protected onSuccessfulSubmit: () => void;
2018-04-04 22:59:14 -04:00
2019-02-12 23:52:50 -05:00
private invalidPinAttempts = 0;
2019-02-13 21:36:36 -05:00
private pinSet: [boolean, boolean];
2019-02-12 23:52:50 -05:00
constructor(protected router: Router, protected i18nService: I18nService,
2018-04-04 22:59:14 -04:00
protected platformUtilsService: PlatformUtilsService, protected messagingService: MessagingService,
2019-02-12 23:52:50 -05:00
protected userService: UserService, protected cryptoService: CryptoService,
protected storageService: StorageService, protected lockService: LockService,
protected environmentService: EnvironmentService, protected stateService: StateService) { }
2018-04-04 22:59:14 -04:00
2019-01-07 23:30:04 -05:00
async ngOnInit() {
2019-02-13 21:36:36 -05:00
this.pinSet = await this.lockService.isPinLockSet();
const hasKey = await this.cryptoService.hasKey();
this.pinLock = (this.pinSet[0] && hasKey) || this.pinSet[1];
2019-01-07 23:30:04 -05:00
this.email = await this.userService.getEmail();
2019-06-03 09:53:18 -04:00
let vaultUrl = this.environmentService.getWebVaultUrl();
if (vaultUrl == null) {
vaultUrl = 'https://bitwarden.com';
}
this.webVaultHostname = Utils.getHostname(vaultUrl);
2019-01-07 23:30:04 -05:00
}
2018-04-04 22:59:14 -04:00
async submit() {
if (this.pinLock && (this.pin == null || this.pin === '')) {
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('pinRequired'));
return;
}
if (!this.pinLock && (this.masterPassword == null || this.masterPassword === '')) {
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('masterPassRequired'));
return;
}
2019-02-12 23:52:50 -05:00
const kdf = await this.userService.getKdf();
const kdfIterations = await this.userService.getKdfIterations();
if (this.pinLock) {
2019-02-13 21:36:36 -05:00
let failed = true;
2019-02-12 23:52:50 -05:00
try {
2019-02-13 21:36:36 -05:00
if (this.pinSet[0]) {
const protectedPin = await this.storageService.get<string>(ConstantsService.protectedPin);
const decPin = await this.cryptoService.decryptToUtf8(new CipherString(protectedPin));
failed = decPin !== this.pin;
2019-03-06 15:46:40 -05:00
this.lockService.pinLocked = failed;
2019-03-05 16:22:00 -05:00
if (!failed) {
this.doContinue();
}
2019-02-13 21:36:36 -05:00
} else {
2019-03-05 16:36:11 -05:00
const key = await this.cryptoService.makeKeyFromPin(this.pin, this.email, kdf, kdfIterations);
2019-02-13 21:36:36 -05:00
failed = false;
2019-03-05 16:36:11 -05:00
await this.setKeyAndContinue(key);
2019-02-13 21:36:36 -05:00
}
2019-03-05 16:22:00 -05:00
} catch {
failed = true;
}
2019-02-13 21:36:36 -05:00
if (failed) {
2019-02-12 23:52:50 -05:00
this.invalidPinAttempts++;
if (this.invalidPinAttempts >= 5) {
this.messagingService.send('logout');
return;
}
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('invalidPin'));
}
2018-04-04 22:59:14 -04:00
} else {
const key = await this.cryptoService.makeKey(this.masterPassword, this.email, kdf, kdfIterations);
const keyHash = await this.cryptoService.hashPassword(this.masterPassword, key);
const storedKeyHash = await this.cryptoService.getKeyHash();
if (storedKeyHash != null && keyHash != null && storedKeyHash === keyHash) {
this.setKeyAndContinue(key);
} else {
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('invalidMasterPassword'));
}
2018-04-04 22:59:14 -04:00
}
}
async logOut() {
const confirmed = await this.platformUtilsService.showDialog(this.i18nService.t('logOutConfirmation'),
this.i18nService.t('logOut'), this.i18nService.t('logOut'), this.i18nService.t('cancel'));
if (confirmed) {
this.messagingService.send('logout');
}
}
togglePassword() {
this.platformUtilsService.eventTrack('Toggled Master Password on Unlock');
2018-04-04 22:59:14 -04:00
this.showPassword = !this.showPassword;
2019-02-13 10:05:58 -05:00
document.getElementById(this.pinLock ? 'pin' : 'masterPassword').focus();
2018-04-04 22:59:14 -04:00
}
2019-02-12 23:52:50 -05:00
private async setKeyAndContinue(key: SymmetricCryptoKey) {
await this.cryptoService.setKey(key);
2019-02-13 21:36:36 -05:00
this.doContinue();
}
private async doContinue() {
const disableFavicon = await this.storageService.get<boolean>(ConstantsService.disableFaviconKey);
await this.stateService.save(ConstantsService.disableFaviconKey, !!disableFavicon);
2019-02-12 23:52:50 -05:00
this.messagingService.send('unlocked');
if (this.onSuccessfulSubmit != null) {
this.onSuccessfulSubmit();
} else if (this.router != null) {
this.router.navigate([this.successRoute]);
}
}
2018-04-04 22:59:14 -04:00
}