diff --git a/src/app/settings/change-email.component.html b/src/app/settings/change-email.component.html index 05829f6b7e..b94521b1db 100644 --- a/src/app/settings/change-email.component.html +++ b/src/app/settings/change-email.component.html @@ -31,7 +31,7 @@ {{'continue' | i18n}} - {{'submit' | i18n}} + {{'changeEmail' | i18n}} {{'cancel' | i18n}} diff --git a/src/app/settings/change-password.component.html b/src/app/settings/change-password.component.html index 6f9b9ed834..e1dd77c093 100644 --- a/src/app/settings/change-password.component.html +++ b/src/app/settings/change-password.component.html @@ -1 +1,29 @@ -change your password + + {{'warning' | i18n}} + {{'loggedOutWarning' | i18n}} + + + + + + {{'currentMasterPass' | i18n}} + + + + {{'newMasterPass' | i18n}} + + + + {{'confirmNewMasterPass' | i18n}} + + + + + + + {{'changeMasterPassword' | i18n}} + + diff --git a/src/app/settings/change-password.component.ts b/src/app/settings/change-password.component.ts index ccaef71b8d..61c108cda2 100644 --- a/src/app/settings/change-password.component.ts +++ b/src/app/settings/change-password.component.ts @@ -6,19 +6,66 @@ import { ToasterService } from 'angular2-toaster'; import { Angulartics2 } from 'angulartics2'; import { ApiService } from 'jslib/abstractions/api.service'; +import { CryptoService } from 'jslib/abstractions/crypto.service'; import { I18nService } from 'jslib/abstractions/i18n.service'; +import { MessagingService } from 'jslib/abstractions/messaging.service'; +import { UserService } from 'jslib/abstractions/user.service'; +import { PasswordRequest } from 'jslib/models/request/passwordRequest'; @Component({ selector: 'app-change-password', templateUrl: 'change-password.component.html', }) export class ChangePasswordComponent { + currentMasterPassword: string; + newMasterPassword: string; + confirmNewMasterPassword: string; formPromise: Promise; constructor(private apiService: ApiService, private i18nService: I18nService, - private analytics: Angulartics2, private toasterService: ToasterService) { } + private analytics: Angulartics2, private toasterService: ToasterService, + private cryptoService: CryptoService, private messagingService: MessagingService, + private userService: UserService) { } async submit() { + const hasKey = await this.cryptoService.hasKey(); + if (!hasKey) { + this.toasterService.popAsync('error', null, this.i18nService.t('updateKey')); + return; + } + if (this.currentMasterPassword == null || this.currentMasterPassword === '' || + this.newMasterPassword == null || this.newMasterPassword === '') { + this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'), + this.i18nService.t('masterPassRequired')); + return; + } + if (this.newMasterPassword.length < 8) { + this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'), + this.i18nService.t('masterPassLength')); + return; + } + if (this.newMasterPassword !== this.confirmNewMasterPassword) { + this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'), + this.i18nService.t('masterPassDoesntMatch')); + return; + } + + const request = new PasswordRequest(); + request.masterPasswordHash = await this.cryptoService.hashPassword(this.currentMasterPassword, null); + const email = await this.userService.getEmail(); + const newKey = await this.cryptoService.makeKey(this.newMasterPassword, email); + request.newMasterPasswordHash = await this.cryptoService.hashPassword(this.newMasterPassword, newKey); + const encKey = await this.cryptoService.getEncKey(); + const newEncKey = await this.cryptoService.encrypt(encKey.key, newKey); + request.key = newEncKey.encryptedString; + try { + this.formPromise = this.apiService.postPassword(request); + await this.formPromise; + this.analytics.eventTrack.next({ action: 'Changed Password' }); + this.toasterService.popAsync('success', this.i18nService.t('masterPasswordChanged'), + this.i18nService.t('logBackIn')); + this.messagingService.send('logout'); + } catch { } } } diff --git a/src/locales/en/messages.json b/src/locales/en/messages.json index 5f929d3577..8f3ce2c1bb 100644 --- a/src/locales/en/messages.json +++ b/src/locales/en/messages.json @@ -767,6 +767,9 @@ "accountUpdated": { "message": "Account Updated" }, + "changeEmail": { + "message": "Change Email" + }, "newEmail": { "message": "New Email" }, @@ -790,5 +793,20 @@ }, "logBackIn": { "message": "Please log back in." + }, + "changeMasterPassword": { + "message": "Change Master Password" + }, + "masterPasswordChanged": { + "message": "Master Password Changed" + }, + "currentMasterPass": { + "message": "Current Master Password" + }, + "newMasterPass": { + "message": "New Master Password" + }, + "confirmNewMasterPass": { + "message": "Confirm New Master Password" } }
{{'loggedOutWarning' | i18n}}