From 60066830a0d75208cc26b88b0e6506153e480d2c Mon Sep 17 00:00:00 2001 From: rr-bw <102181210+rr-bw@users.noreply.github.com> Date: Tue, 1 Apr 2025 21:41:37 -0700 Subject: [PATCH] update InputPasswordComponent to work with currentPassword and current password related crypto properties --- .../change-existing-password.component.ts | 112 +++++++------- .../input-password.component.html | 7 +- .../input-password.component.ts | 141 ++++++++++-------- .../input-password/password-input-result.ts | 17 ++- 4 files changed, 150 insertions(+), 127 deletions(-) diff --git a/libs/auth/src/angular/change-existing-password/change-existing-password.component.ts b/libs/auth/src/angular/change-existing-password/change-existing-password.component.ts index 86064e9ea15..758fa785f3e 100644 --- a/libs/auth/src/angular/change-existing-password/change-existing-password.component.ts +++ b/libs/auth/src/angular/change-existing-password/change-existing-password.component.ts @@ -102,6 +102,62 @@ export class ChangeExistingPasswordComponent implements OnInit { } } + // todo: move this to a service + // https://bitwarden.atlassian.net/browse/PM-17108 + async updatePassword(currentPassword: string, newPassword: string, hint: string) { + const { userId, email } = await firstValueFrom( + this.accountService.activeAccount$.pipe(map((a) => ({ userId: a?.id, email: a?.email }))), + ); + const kdfConfig = await firstValueFrom(this.kdfConfigService.getKdfConfig$(userId)); + + const currentMasterKey = await this.keyService.makeMasterKey(currentPassword, email, kdfConfig); + const decryptedUserKey = await this.masterPasswordService.decryptUserKeyWithMasterKey( + currentMasterKey, + userId, + ); + if (decryptedUserKey == null) { + this.toastService.showToast({ + variant: "error", + title: null, + message: this.i18nService.t("invalidMasterPassword"), + }); + return; + } + + const newMasterKey = await this.keyService.makeMasterKey(newPassword, email, kdfConfig); + const newMasterKeyEncryptedUserKey = await this.keyService.encryptUserKeyWithMasterKey( + newMasterKey, + decryptedUserKey, + ); + + const request = new PasswordRequest(); + request.masterPasswordHash = await this.keyService.hashMasterKey( + currentPassword, + currentMasterKey, + ); + request.masterPasswordHint = hint; + request.newMasterPasswordHash = await this.keyService.hashMasterKey(newPassword, newMasterKey); + request.key = newMasterKeyEncryptedUserKey[1].encryptedString; + + try { + await this.masterPasswordApiService.postPassword(request); + + this.toastService.showToast({ + variant: "success", + title: this.i18nService.t("masterPasswordChanged"), + message: this.i18nService.t("masterPasswordChangedDesc"), + }); + + this.messagingService.send("logout"); + } catch { + this.toastService.showToast({ + variant: "error", + title: null, + message: this.i18nService.t("errorOccurred"), + }); + } + } + async submitOld(passwordInputResult: PasswordInputResult) { if (passwordInputResult.rotateUserKey) { await this.syncService.fullSync(true); @@ -171,62 +227,6 @@ export class ChangeExistingPasswordComponent implements OnInit { } } - // todo: move this to a service - // https://bitwarden.atlassian.net/browse/PM-17108 - async updatePassword(currentPassword: string, newPassword: string, hint: string) { - const { userId, email } = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => ({ userId: a?.id, email: a?.email }))), - ); - const kdfConfig = await firstValueFrom(this.kdfConfigService.getKdfConfig$(userId)); - - const currentMasterKey = await this.keyService.makeMasterKey(currentPassword, email, kdfConfig); - const decryptedUserKey = await this.masterPasswordService.decryptUserKeyWithMasterKey( - currentMasterKey, - userId, - ); - if (decryptedUserKey == null) { - this.toastService.showToast({ - variant: "error", - title: null, - message: this.i18nService.t("invalidMasterPassword"), - }); - return; - } - - const newMasterKey = await this.keyService.makeMasterKey(newPassword, email, kdfConfig); - const newMasterKeyEncryptedUserKey = await this.keyService.encryptUserKeyWithMasterKey( - newMasterKey, - decryptedUserKey, - ); - - const request = new PasswordRequest(); - request.masterPasswordHash = await this.keyService.hashMasterKey( - currentPassword, - currentMasterKey, - ); - request.masterPasswordHint = hint; - request.newMasterPasswordHash = await this.keyService.hashMasterKey(newPassword, newMasterKey); - request.key = newMasterKeyEncryptedUserKey[1].encryptedString; - - try { - await this.masterPasswordApiService.postPassword(request); - - this.toastService.showToast({ - variant: "success", - title: this.i18nService.t("masterPasswordChanged"), - message: this.i18nService.t("masterPasswordChangedDesc"), - }); - - this.messagingService.send("logout"); - } catch { - this.toastService.showToast({ - variant: "error", - title: null, - message: this.i18nService.t("errorOccurred"), - }); - } - } - private async updateKey(newPassword: string) { const user = await firstValueFrom(this.accountService.activeAccount$); await this.changePasswordService.rotateUserKeyAndEncryptedDataLegacy(newPassword, user); diff --git a/libs/auth/src/angular/input-password/input-password.component.html b/libs/auth/src/angular/input-password/input-password.component.html index d5c0578e646..c57fd994671 100644 --- a/libs/auth/src/angular/input-password/input-password.component.html +++ b/libs/auth/src/angular/input-password/input-password.component.html @@ -63,7 +63,7 @@ id="input-password-form_confirm-new-password" bitInput type="password" - formControlName="confirmNewPassword" + formControlName="newPasswordConfirm" />