1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 08:43:33 +00:00

extend functionality for set password flow (#150)

This commit is contained in:
Kyle Spearrin
2020-08-17 10:34:52 -04:00
committed by GitHub
parent ed6978baff
commit 7bf00b4fb3
2 changed files with 52 additions and 30 deletions

View File

@@ -1,10 +1,6 @@
import { import { OnInit } from '@angular/core';
OnInit,
} from '@angular/core';
import { import { Router } from '@angular/router';
Router,
} from '@angular/router';
import { ApiService } from '../../abstractions/api.service'; import { ApiService } from '../../abstractions/api.service';
import { CipherService } from '../../abstractions/cipher.service'; import { CipherService } from '../../abstractions/cipher.service';
@@ -22,13 +18,18 @@ import { CipherString } from '../../models/domain/cipherString';
import { MasterPasswordPolicyOptions } from '../../models/domain/masterPasswordPolicyOptions'; import { MasterPasswordPolicyOptions } from '../../models/domain/masterPasswordPolicyOptions';
import { SymmetricCryptoKey } from '../../models/domain/symmetricCryptoKey'; import { SymmetricCryptoKey } from '../../models/domain/symmetricCryptoKey';
import { KdfType } from '../../enums/kdfType';
export class ChangePasswordComponent implements OnInit { export class ChangePasswordComponent implements OnInit {
newMasterPassword: string; masterPassword: string;
confirmNewMasterPassword: string; masterPasswordRetype: string;
formPromise: Promise<any>; formPromise: Promise<any>;
masterPasswordScore: number; masterPasswordScore: number;
enforcedPolicyOptions: MasterPasswordPolicyOptions; enforcedPolicyOptions: MasterPasswordPolicyOptions;
protected kdf: KdfType;
protected kdfIterations: number;
private masterPasswordStrengthTimeout: any; private masterPasswordStrengthTimeout: any;
private email: string; private email: string;
@@ -65,35 +66,29 @@ export class ChangePasswordComponent implements OnInit {
} }
async submit() { async submit() {
const hasEncKey = await this.cryptoService.hasEncKey(); if (this.masterPassword == null || this.masterPassword === '') {
if (!hasEncKey) {
this.platformUtilsService.showToast('error', null, this.i18nService.t('updateKey'));
return;
}
if (this.newMasterPassword == null || this.newMasterPassword === '') {
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'), this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('masterPassRequired')); this.i18nService.t('masterPassRequired'));
return; return;
} }
if (this.newMasterPassword.length < 8) { if (this.masterPassword.length < 8) {
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'), this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('masterPassLength')); this.i18nService.t('masterPassLength'));
return; return;
} }
if (this.newMasterPassword !== this.confirmNewMasterPassword) { if (this.masterPassword !== this.masterPasswordRetype) {
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'), this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('masterPassDoesntMatch')); this.i18nService.t('masterPassDoesntMatch'));
return; return;
} }
const strengthResult = this.passwordGenerationService.passwordStrength(this.newMasterPassword, const strengthResult = this.passwordGenerationService.passwordStrength(this.masterPassword,
this.getPasswordStrengthUserInput()); this.getPasswordStrengthUserInput());
if (this.enforcedPolicyOptions != null && if (this.enforcedPolicyOptions != null &&
!this.policyService.evaluateMasterPassword( !this.policyService.evaluateMasterPassword(
strengthResult.score, strengthResult.score,
this.newMasterPassword, this.masterPassword,
this.enforcedPolicyOptions)) { this.enforcedPolicyOptions)) {
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'), this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('masterPasswordPolicyRequirementsNotMet')); this.i18nService.t('masterPasswordPolicyRequirementsNotMet'));
@@ -114,14 +109,25 @@ export class ChangePasswordComponent implements OnInit {
} }
const email = await this.userService.getEmail(); const email = await this.userService.getEmail();
const kdf = await this.userService.getKdf(); if (this.kdf == null) {
const kdfIterations = await this.userService.getKdfIterations(); this.kdf = await this.userService.getKdf();
const newKey = await this.cryptoService.makeKey(this.newMasterPassword, email.trim().toLowerCase(), }
kdf, kdfIterations); if (this.kdfIterations == null) {
const newMasterPasswordHash = await this.cryptoService.hashPassword(this.newMasterPassword, newKey); this.kdfIterations = await this.userService.getKdfIterations();
const newEncKey = await this.cryptoService.remakeEncKey(newKey); }
const key = await this.cryptoService.makeKey(this.masterPassword, email.trim().toLowerCase(),
this.kdf, this.kdfIterations);
const masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, key);
await this.performSubmitActions(newMasterPasswordHash, newKey, newEncKey); 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);
} }
async setupSubmitActions(): Promise<boolean> { async setupSubmitActions(): Promise<boolean> {
@@ -130,8 +136,8 @@ export class ChangePasswordComponent implements OnInit {
return true; return true;
} }
async performSubmitActions(newMasterPasswordHash: string, newKey: SymmetricCryptoKey, async performSubmitActions(masterPasswordHash: string, key: SymmetricCryptoKey,
newEncKey: [SymmetricCryptoKey, CipherString]) { encKey: [SymmetricCryptoKey, CipherString]) {
// Override in sub-class // Override in sub-class
} }
@@ -140,12 +146,20 @@ export class ChangePasswordComponent implements OnInit {
clearTimeout(this.masterPasswordStrengthTimeout); clearTimeout(this.masterPasswordStrengthTimeout);
} }
this.masterPasswordStrengthTimeout = setTimeout(() => { this.masterPasswordStrengthTimeout = setTimeout(() => {
const strengthResult = this.passwordGenerationService.passwordStrength(this.newMasterPassword, const strengthResult = this.passwordGenerationService.passwordStrength(this.masterPassword,
this.getPasswordStrengthUserInput()); this.getPasswordStrengthUserInput());
this.masterPasswordScore = strengthResult == null ? null : strengthResult.score; this.masterPasswordScore = strengthResult == null ? null : strengthResult.score;
}, 300); }, 300);
} }
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');
}
}
private getPasswordStrengthUserInput() { private getPasswordStrengthUserInput() {
let userInput: string[] = []; let userInput: string[] = [];
const atPosition = this.email.indexOf('@'); const atPosition = this.email.indexOf('@');

View File

@@ -1,4 +1,12 @@
import { KeysRequest } from './keysRequest';
import { KdfType } from '../../enums/kdfType';
export class SetPasswordRequest { export class SetPasswordRequest {
newMasterPasswordHash: string; masterPasswordHash: string;
key: string; key: string;
masterPasswordHint: string;
keys: KeysRequest;
kdf: KdfType;
kdfIterations: number;
} }