From 5d874d07b35a23dc6d54f1f435d88d2ddd815e33 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 19 Aug 2020 10:57:35 -0400 Subject: [PATCH] abstract set password component to jslib (#153) --- .../components/set-password.component.ts | 90 +++++++++++++++++++ src/angular/components/sso.component.ts | 2 +- 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/angular/components/set-password.component.ts diff --git a/src/angular/components/set-password.component.ts b/src/angular/components/set-password.component.ts new file mode 100644 index 0000000000..d2d33817ef --- /dev/null +++ b/src/angular/components/set-password.component.ts @@ -0,0 +1,90 @@ +import { + ActivatedRoute, + Router, +} from '@angular/router'; + +import { ApiService } from '../../abstractions/api.service'; +import { CipherService } from '../../abstractions/cipher.service'; +import { CryptoService } from '../../abstractions/crypto.service'; +import { FolderService } from '../../abstractions/folder.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 { SyncService } from '../../abstractions/sync.service'; +import { UserService } from '../../abstractions/user.service'; + +import { CipherString } from '../../models/domain/cipherString'; +import { SymmetricCryptoKey } from '../../models/domain/symmetricCryptoKey'; + +import { KeysRequest } from '../../models/request/keysRequest'; +import { SetPasswordRequest } from '../../models/request/setPasswordRequest'; + +import { ChangePasswordComponent as BaseChangePasswordComponent } from './change-password.component'; + +import { KdfType } from '../../enums/kdfType'; + +export class SetPasswordComponent extends BaseChangePasswordComponent { + showPassword: boolean = false; + hint: string = ''; + + onSuccessfulChangePassword: () => Promise; + successRoute = 'vault'; + + constructor(apiService: ApiService, i18nService: I18nService, + cryptoService: CryptoService, messagingService: MessagingService, + userService: UserService, passwordGenerationService: PasswordGenerationService, + platformUtilsService: PlatformUtilsService, folderService: FolderService, + cipherService: CipherService, syncService: SyncService, + policyService: PolicyService, router: Router, private route: ActivatedRoute) { + super(apiService, i18nService, cryptoService, messagingService, userService, passwordGenerationService, + platformUtilsService, folderService, cipherService, syncService, policyService, router); + } + + async setupSubmitActions() { + this.kdf = KdfType.PBKDF2_SHA256; + const useLowerKdf = this.platformUtilsService.isEdge() || this.platformUtilsService.isIE(); + this.kdfIterations = useLowerKdf ? 10000 : 100000; + return true; + } + + async performSubmitActions(masterPasswordHash: string, key: SymmetricCryptoKey, + encKey: [SymmetricCryptoKey, CipherString]) { + const request = new SetPasswordRequest(); + request.masterPasswordHash = masterPasswordHash; + request.key = encKey[1].encryptedString; + request.masterPasswordHint = this.hint; + request.kdf = this.kdf; + request.kdfIterations = this.kdfIterations; + + const keys = await this.cryptoService.makeKeyPair(encKey[0]); + request.keys = new KeysRequest(keys[0], keys[1].encryptedString); + + try { + this.formPromise = this.apiService.setPassword(request); + await this.formPromise; + + await this.userService.setInformation(await this.userService.getUserId(), await this.userService.getEmail(), + this.kdf, this.kdfIterations); + await this.cryptoService.setKey(key); + await this.cryptoService.setKeyHash(masterPasswordHash); + await this.cryptoService.setEncKey(encKey[1].encryptedString); + await this.cryptoService.setEncPrivateKey(keys[1].encryptedString); + + if (this.onSuccessfulChangePassword != null) { + this.onSuccessfulChangePassword(); + } else { + this.router.navigate([this.successRoute]); + } + } catch { + this.platformUtilsService.showToast('error', null, this.i18nService.t('errorOccurred')); + } + } + + togglePassword(confirmField: boolean) { + this.platformUtilsService.eventTrack('Toggled Master Password on Set Password'); + this.showPassword = !this.showPassword; + document.getElementById(confirmField ? 'masterPasswordRetype' : 'masterPassword').focus(); + } +} diff --git a/src/angular/components/sso.component.ts b/src/angular/components/sso.component.ts index 9c537e8814..5b873bf4fb 100644 --- a/src/angular/components/sso.component.ts +++ b/src/angular/components/sso.component.ts @@ -30,7 +30,7 @@ export class SsoComponent { protected twoFactorRoute = '2fa'; protected successRoute = 'lock'; - protected changePasswordRoute = 'change-password'; + protected changePasswordRoute = 'set-password'; protected clientId: string; protected redirectUri: string; protected state: string;