From d2fd834a38c7273d509a7b3ef0e8c25e728bcfb3 Mon Sep 17 00:00:00 2001 From: rr-bw <102181210+rr-bw@users.noreply.github.com> Date: Sat, 29 Mar 2025 20:37:04 -0700 Subject: [PATCH] create a ChangePasswordService --- .../web-change-password.service.ts | 26 +++++++++++++++++++ .../change-existing-password.component.ts | 18 +++++++------ .../change-password.service.abstraction.ts | 10 +++++++ libs/auth/src/common/abstractions/index.ts | 1 + .../default-change-password.service.ts | 14 ++++++++++ libs/auth/src/common/services/index.ts | 1 + 6 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 apps/web/src/app/auth/core/services/change-password/web-change-password.service.ts create mode 100644 libs/auth/src/common/abstractions/change-password.service.abstraction.ts create mode 100644 libs/auth/src/common/services/change-password/default-change-password.service.ts diff --git a/apps/web/src/app/auth/core/services/change-password/web-change-password.service.ts b/apps/web/src/app/auth/core/services/change-password/web-change-password.service.ts new file mode 100644 index 00000000000..15d8f132af8 --- /dev/null +++ b/apps/web/src/app/auth/core/services/change-password/web-change-password.service.ts @@ -0,0 +1,26 @@ +import { inject } from "@angular/core"; + +import { ChangePasswordService, DefaultChangePasswordService } from "@bitwarden/auth/common"; +import { Account } from "@bitwarden/common/auth/abstractions/account.service"; +import { UserKeyRotationService } from "@bitwarden/web-vault/app/key-management/key-rotation/user-key-rotation.service"; + +export class WebChangePasswordComponent + extends DefaultChangePasswordService + implements ChangePasswordService +{ + userKeyRotationService = inject(UserKeyRotationService); + + async rotateUserKeyMasterPasswordAndEncryptedData( + currentPassword: string, + newPassword: string, + user: Account, + hint: string, + ): Promise { + await this.userKeyRotationService.rotateUserKeyMasterPasswordAndEncryptedData( + currentPassword, + newPassword, + user, + hint, + ); + } +} 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 3d80288b682..f5ec816a9d8 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 @@ -1,6 +1,7 @@ import { Component, Input, OnInit } from "@angular/core"; import { firstValueFrom, map } from "rxjs"; +import { ChangePasswordService } from "@bitwarden/auth/common"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { MasterPasswordPolicyOptions } from "@bitwarden/common/admin-console/models/domain/master-password-policy-options"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; @@ -36,6 +37,7 @@ export class ChangeExistingPasswordComponent implements OnInit { constructor( private accountService: AccountService, + private changePasswordService: ChangePasswordService, private configService: ConfigService, private i18nService: I18nService, private kdfConfigService: KdfConfigService, @@ -74,14 +76,14 @@ export class ChangeExistingPasswordComponent implements OnInit { try { if (passwordInputResult.rotateUserKey) { await this.syncService.fullSync(true); - // const user = await firstValueFrom(this.accountService.activeAccount$); - // // TODO-rr-bw: make a ChangeExistingPasswordService with Default & Web implementations - // // await this.changeExistingPasswordService.rotateUserKeyMasterPasswordAndEncryptedData( - // // passwordInputResult.currentPassword, - // // passwordInputResult.newPassword, - // // user, - // // passwordInputResult.hint, - // // ); + const user = await firstValueFrom(this.accountService.activeAccount$); + + await this.changePasswordService.rotateUserKeyMasterPasswordAndEncryptedData( + passwordInputResult.currentPassword, + passwordInputResult.newPassword, + user, + passwordInputResult.hint, + ); } else { await this.updatePassword( passwordInputResult.currentPassword, diff --git a/libs/auth/src/common/abstractions/change-password.service.abstraction.ts b/libs/auth/src/common/abstractions/change-password.service.abstraction.ts new file mode 100644 index 00000000000..8f81edab830 --- /dev/null +++ b/libs/auth/src/common/abstractions/change-password.service.abstraction.ts @@ -0,0 +1,10 @@ +import { Account } from "@bitwarden/common/auth/abstractions/account.service"; + +export abstract class ChangePasswordService { + abstract rotateUserKeyMasterPasswordAndEncryptedData( + currentPassword: string, + newPassword: string, + user: Account, + hint: string, + ): Promise; +} diff --git a/libs/auth/src/common/abstractions/index.ts b/libs/auth/src/common/abstractions/index.ts index c0dc500ddb9..796db3baf9d 100644 --- a/libs/auth/src/common/abstractions/index.ts +++ b/libs/auth/src/common/abstractions/index.ts @@ -1,4 +1,5 @@ export * from "./auth-request-api.service"; +export * from "./change-password.service.abstraction"; export * from "./pin.service.abstraction"; export * from "./login-email.service"; export * from "./login-strategy.service"; 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 new file mode 100644 index 00000000000..29bc17b6de5 --- /dev/null +++ b/libs/auth/src/common/services/change-password/default-change-password.service.ts @@ -0,0 +1,14 @@ +import { Account } from "@bitwarden/common/auth/abstractions/account.service"; + +import { ChangePasswordService } from "../../abstractions"; + +export class DefaultChangePasswordService implements ChangePasswordService { + async rotateUserKeyMasterPasswordAndEncryptedData( + currentPassword: string, + newPassword: string, + user: Account, + hint: string, + ): Promise { + return null; + } +} diff --git a/libs/auth/src/common/services/index.ts b/libs/auth/src/common/services/index.ts index 73d31799b7e..38bc3519721 100644 --- a/libs/auth/src/common/services/index.ts +++ b/libs/auth/src/common/services/index.ts @@ -1,3 +1,4 @@ +export * from "./change-password/default-change-password.service"; export * from "./pin/pin.service.implementation"; export * from "./login-email/login-email.service"; export * from "./login-strategies/login-strategy.service";