From 2a976a0db2c900ef75c864592652c9081cb51dbf Mon Sep 17 00:00:00 2001 From: rr-bw <102181210+rr-bw@users.noreply.github.com> Date: Thu, 3 Apr 2025 15:49:12 -0700 Subject: [PATCH] update typing --- .../change-password.component.ts | 35 +++++++++++++++---- .../input-password.component.ts | 10 +++--- .../default-change-password.service.ts | 6 ++-- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/libs/auth/src/angular/change-password/change-password.component.ts b/libs/auth/src/angular/change-password/change-password.component.ts index bd9f5a67bcf..92a38c96645 100644 --- a/libs/auth/src/angular/change-password/change-password.component.ts +++ b/libs/auth/src/angular/change-password/change-password.component.ts @@ -38,7 +38,7 @@ export class ChangePasswordComponent implements OnInit { email?: string; masterPasswordPolicyOptions?: MasterPasswordPolicyOptions; userkeyRotationV2 = false; - formPromise: Promise; + formPromise?: Promise; constructor( private accountService: AccountService, @@ -77,11 +77,23 @@ export class ChangePasswordComponent implements OnInit { } async submitNew(passwordInputResult: PasswordInputResult) { + if ( + passwordInputResult.currentPassword == null || + passwordInputResult.currentMasterKey == null || + passwordInputResult.currentServerMasterKeyHash == null + ) { + throw new Error("Invalid current password credentials"); + } + try { if (passwordInputResult.rotateUserKey) { await this.syncService.fullSync(true); const user = await firstValueFrom(this.accountService.activeAccount$); + if (user == null) { + throw new Error("User not found"); + } + await this.changePasswordService.rotateUserKeyMasterPasswordAndEncryptedData( passwordInputResult.currentPassword, passwordInputResult.newPassword, @@ -105,23 +117,27 @@ export class ChangePasswordComponent implements OnInit { this.messagingService.send("logout"); } - } catch (e) { + } catch { this.toastService.showToast({ variant: "error", - title: this.i18nService.t("errorOccurred"), - message: e.message, + title: "", + message: this.i18nService.t("errorOccurred"), }); } } async submitOld(passwordInputResult: PasswordInputResult) { + if (passwordInputResult.currentServerMasterKeyHash == null) { + throw new Error("Invalid current password credentials"); + } + if (passwordInputResult.rotateUserKey) { await this.syncService.fullSync(true); } const userId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); - let newMasterKeyEncryptedUserKey: [UserKey, EncString] = null; + let newMasterKeyEncryptedUserKey: [UserKey, EncString] | null = null; const userKey = await this.keyService.getUserKey(); if (userKey == null) { @@ -138,7 +154,7 @@ export class ChangePasswordComponent implements OnInit { request.masterPasswordHash = passwordInputResult.currentServerMasterKeyHash; request.masterPasswordHint = passwordInputResult.newPasswordHint; request.newMasterPasswordHash = passwordInputResult.newServerMasterKeyHash; - request.key = newMasterKeyEncryptedUserKey[1].encryptedString; + request.key = newMasterKeyEncryptedUserKey[1].encryptedString as string; try { if (passwordInputResult.rotateUserKey) { @@ -169,7 +185,7 @@ export class ChangePasswordComponent implements OnInit { } catch { this.toastService.showToast({ variant: "error", - title: null, + title: "", message: this.i18nService.t("errorOccurred"), }); } @@ -177,6 +193,11 @@ export class ChangePasswordComponent implements OnInit { private async updateKey(newPassword: string) { const user = await firstValueFrom(this.accountService.activeAccount$); + + if (user == null) { + throw new Error("User not found"); + } + await this.changePasswordService.rotateUserKeyAndEncryptedDataLegacy(newPassword, user); } } diff --git a/libs/auth/src/angular/input-password/input-password.component.ts b/libs/auth/src/angular/input-password/input-password.component.ts index 03cd03ef9b5..2d878383d95 100644 --- a/libs/auth/src/angular/input-password/input-password.component.ts +++ b/libs/auth/src/angular/input-password/input-password.component.ts @@ -108,7 +108,7 @@ export class InputPasswordComponent implements OnInit { protected secondaryButtonTextStr: string = ""; protected InputPasswordFlow = InputPasswordFlow; - private kdfConfig?: KdfConfig; + private kdfConfig: KdfConfig = DEFAULT_KDF_CONFIG; private minHintLength = 0; protected maxHintLength = 50; protected minPasswordLength = Utils.minimumPasswordLength; @@ -224,8 +224,10 @@ export class InputPasswordComponent implements OnInit { this.kdfConfig = (await this.kdfConfigService.getKdfConfig()) || DEFAULT_KDF_CONFIG; - const currentPassword = this.formGroup.get("currentPassword")?.value; - const { newPassword, newPasswordHint, checkForBreaches } = this.formGroup.value; + const currentPassword = this.formGroup.get("currentPassword")?.value || ""; + const newPassword = this.formGroup.controls.newPassword.value; + const newPasswordHint = this.formGroup.controls.newPasswordHint.value; + const checkForBreaches = this.formGroup.controls.checkForBreaches.value; // 1. Verify current password is correct (if necessary) if ( @@ -336,7 +338,7 @@ export class InputPasswordComponent implements OnInit { if (decryptedUserKey == null) { this.toastService.showToast({ variant: "error", - title: null, + title: "", message: this.i18nService.t("invalidMasterPassword"), }); diff --git a/libs/auth/src/common/services/change-password/default-change-password.service.ts b/libs/auth/src/common/services/change-password/default-change-password.service.ts index 85aa40db7a7..5377cdb0ccf 100644 --- a/libs/auth/src/common/services/change-password/default-change-password.service.ts +++ b/libs/auth/src/common/services/change-password/default-change-password.service.ts @@ -61,12 +61,12 @@ export class DefaultChangePasswordService implements ChangePasswordService { request.masterPasswordHash = currentServerMasterKeyHash; request.masterPasswordHint = newPasswordHint; request.newMasterPasswordHash = newServerMasterKeyHash; - request.key = newMasterKeyEncryptedUserKey[1].encryptedString; + request.key = newMasterKeyEncryptedUserKey[1].encryptedString as string; try { await this.masterPasswordApiService.postPassword(request); - } catch (e) { - throw new Error(e); + } catch { + throw new Error("Could not change password"); } } }