2020-08-17 16:34:52 +02:00
|
|
|
import { OnInit } from '@angular/core';
|
2020-08-01 15:42:24 +02:00
|
|
|
|
|
|
|
import { CryptoService } from '../../abstractions/crypto.service';
|
|
|
|
import { I18nService } from '../../abstractions/i18n.service';
|
|
|
|
import { MessagingService } from '../../abstractions/messaging.service';
|
|
|
|
import { PasswordGenerationService } from '../../abstractions/passwordGeneration.service';
|
|
|
|
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
|
|
|
|
import { PolicyService } from '../../abstractions/policy.service';
|
|
|
|
import { UserService } from '../../abstractions/user.service';
|
|
|
|
|
|
|
|
import { CipherString } from '../../models/domain/cipherString';
|
|
|
|
import { MasterPasswordPolicyOptions } from '../../models/domain/masterPasswordPolicyOptions';
|
|
|
|
import { SymmetricCryptoKey } from '../../models/domain/symmetricCryptoKey';
|
|
|
|
|
2020-08-17 16:34:52 +02:00
|
|
|
import { KdfType } from '../../enums/kdfType';
|
|
|
|
|
2020-08-01 15:42:24 +02:00
|
|
|
export class ChangePasswordComponent implements OnInit {
|
2020-08-17 16:34:52 +02:00
|
|
|
masterPassword: string;
|
|
|
|
masterPasswordRetype: string;
|
2020-08-01 15:42:24 +02:00
|
|
|
formPromise: Promise<any>;
|
|
|
|
masterPasswordScore: number;
|
|
|
|
enforcedPolicyOptions: MasterPasswordPolicyOptions;
|
|
|
|
|
2020-08-17 16:34:52 +02:00
|
|
|
protected kdf: KdfType;
|
|
|
|
protected kdfIterations: number;
|
|
|
|
|
2020-08-01 15:42:24 +02:00
|
|
|
private masterPasswordStrengthTimeout: any;
|
|
|
|
private email: string;
|
|
|
|
|
2020-08-21 19:25:33 +02:00
|
|
|
constructor(protected i18nService: I18nService, protected cryptoService: CryptoService,
|
|
|
|
protected messagingService: MessagingService, protected userService: UserService,
|
|
|
|
protected passwordGenerationService: PasswordGenerationService,
|
|
|
|
protected platformUtilsService: PlatformUtilsService, protected policyService: PolicyService) { }
|
2020-08-01 15:42:24 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
|
|
|
this.email = await this.userService.getEmail();
|
|
|
|
this.enforcedPolicyOptions = await this.policyService.getMasterPasswordPolicyOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
getPasswordScoreAlertDisplay() {
|
|
|
|
if (this.enforcedPolicyOptions == null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
let str: string;
|
|
|
|
switch (this.enforcedPolicyOptions.minComplexity) {
|
|
|
|
case 4:
|
|
|
|
str = this.i18nService.t('strong');
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
str = this.i18nService.t('good');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
str = this.i18nService.t('weak');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return str + ' (' + this.enforcedPolicyOptions.minComplexity + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
async submit() {
|
2020-08-17 16:34:52 +02:00
|
|
|
if (this.masterPassword == null || this.masterPassword === '') {
|
2020-08-01 15:42:24 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('masterPassRequired'));
|
|
|
|
return;
|
|
|
|
}
|
2020-08-17 16:34:52 +02:00
|
|
|
if (this.masterPassword.length < 8) {
|
2020-08-01 15:42:24 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('masterPassLength'));
|
|
|
|
return;
|
|
|
|
}
|
2020-08-17 16:34:52 +02:00
|
|
|
if (this.masterPassword !== this.masterPasswordRetype) {
|
2020-08-01 15:42:24 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('masterPassDoesntMatch'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-17 16:34:52 +02:00
|
|
|
const strengthResult = this.passwordGenerationService.passwordStrength(this.masterPassword,
|
2020-08-01 15:42:24 +02:00
|
|
|
this.getPasswordStrengthUserInput());
|
|
|
|
|
|
|
|
if (this.enforcedPolicyOptions != null &&
|
|
|
|
!this.policyService.evaluateMasterPassword(
|
|
|
|
strengthResult.score,
|
2020-08-17 16:34:52 +02:00
|
|
|
this.masterPassword,
|
2020-08-01 15:42:24 +02:00
|
|
|
this.enforcedPolicyOptions)) {
|
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('masterPasswordPolicyRequirementsNotMet'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strengthResult != null && strengthResult.score < 3) {
|
|
|
|
const result = await this.platformUtilsService.showDialog(this.i18nService.t('weakMasterPasswordDesc'),
|
|
|
|
this.i18nService.t('weakMasterPassword'), this.i18nService.t('yes'), this.i18nService.t('no'),
|
|
|
|
'warning');
|
|
|
|
if (!result) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await this.setupSubmitActions()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const email = await this.userService.getEmail();
|
2020-08-17 16:34:52 +02:00
|
|
|
if (this.kdf == null) {
|
|
|
|
this.kdf = await this.userService.getKdf();
|
|
|
|
}
|
|
|
|
if (this.kdfIterations == null) {
|
|
|
|
this.kdfIterations = await this.userService.getKdfIterations();
|
|
|
|
}
|
|
|
|
const key = await this.cryptoService.makeKey(this.masterPassword, email.trim().toLowerCase(),
|
|
|
|
this.kdf, this.kdfIterations);
|
|
|
|
const masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, key);
|
|
|
|
|
|
|
|
let encKey: [SymmetricCryptoKey, CipherString] = null;
|
|
|
|
const existingEncKey = await this.cryptoService.getEncKey();
|
|
|
|
if (existingEncKey == null) {
|
|
|
|
encKey = await this.cryptoService.makeEncKey(key);
|
|
|
|
} else {
|
|
|
|
encKey = await this.cryptoService.remakeEncKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.performSubmitActions(masterPasswordHash, key, encKey);
|
2020-08-01 15:42:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async setupSubmitActions(): Promise<boolean> {
|
|
|
|
// Override in sub-class
|
|
|
|
// Can be used for additional validation and/or other processes the should occur before changing passwords
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-17 16:34:52 +02:00
|
|
|
async performSubmitActions(masterPasswordHash: string, key: SymmetricCryptoKey,
|
|
|
|
encKey: [SymmetricCryptoKey, CipherString]) {
|
2020-08-01 15:42:24 +02:00
|
|
|
// Override in sub-class
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePasswordStrength() {
|
|
|
|
if (this.masterPasswordStrengthTimeout != null) {
|
|
|
|
clearTimeout(this.masterPasswordStrengthTimeout);
|
|
|
|
}
|
|
|
|
this.masterPasswordStrengthTimeout = setTimeout(() => {
|
2020-08-17 16:34:52 +02:00
|
|
|
const strengthResult = this.passwordGenerationService.passwordStrength(this.masterPassword,
|
2020-08-01 15:42:24 +02:00
|
|
|
this.getPasswordStrengthUserInput());
|
|
|
|
this.masterPasswordScore = strengthResult == null ? null : strengthResult.score;
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
|
2020-08-17 16:34:52 +02: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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-01 15:42:24 +02:00
|
|
|
private getPasswordStrengthUserInput() {
|
|
|
|
let userInput: string[] = [];
|
|
|
|
const atPosition = this.email.indexOf('@');
|
|
|
|
if (atPosition > -1) {
|
|
|
|
userInput = userInput.concat(this.email.substr(0, atPosition).trim().toLowerCase().split(/[^A-Za-z0-9]/));
|
|
|
|
}
|
|
|
|
return userInput;
|
|
|
|
}
|
|
|
|
}
|