bitwarden-estensione-browser/apps/web/src/app/settings/change-email.component.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
3.6 KiB
TypeScript
Raw Normal View History

import { Component, OnInit } from "@angular/core";
2022-06-14 17:10:53 +02:00
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/abstractions/log.service";
import { MessagingService } from "@bitwarden/common/abstractions/messaging.service";
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
import { StateService } from "@bitwarden/common/abstractions/state.service";
import { TwoFactorProviderType } from "@bitwarden/common/enums/twoFactorProviderType";
import { EmailRequest } from "@bitwarden/common/models/request/emailRequest";
import { EmailTokenRequest } from "@bitwarden/common/models/request/emailTokenRequest";
@Component({
selector: "app-change-email",
templateUrl: "change-email.component.html",
})
export class ChangeEmailComponent implements OnInit {
2018-06-21 20:28:00 +02:00
masterPassword: string;
newEmail: string;
token: string;
tokenSent = false;
showTwoFactorEmailWarning = false;
2018-06-21 20:28:00 +02:00
formPromise: Promise<any>;
[Account Switching] [Refactor] Implement new account centric services (#1220) * [chore] updated services.module to use account services * [refactor] sorted services provided by services.module * [chore] removed references to deleted jslib services * [chore] used activeAccount over storageService for account level storage items * [chore] resolved linter warnings * Refactor activeAccountService to stateService * [bug] Remove uneeded calls to state service on logout This was causing console erros on logout. Clearing of data is handled fully in dedicated services, clearing them in state afterwards is essentially a redundant call. * [bug] Add back null locked callback to VaultTimeoutService * Move call to get showUpdateKey * [bug] Ensure HtmlStorageService does not override StateService options and locations * [bug] Adjust theme logic to pull from the new storage locations * [bug] Correct theme not sticking on refresh * [bug] Add enableFullWidth to the account model * [bug] fix theme option empty when light is selected * [bug] init state on application start * [bug] Reinit state when coming back from a lock * [style] Fix lint complaints * [bug] Clean state on logout * [chore] Resolved merge issues * [bug] Correct default for enableGravitars * Bump angular to 12. * Remove angular.json * Bump rxjs * Fix build errors, remove file-loader with asset/resource * Use contenthash * Bump jslib * Bump ngx-toastr * [chore] resolve issues from merge * [chore] resolve issues from merge * [bug] Add missing bracket * Use newer import syntax * [bug] Correct service orge * [style] Fix lint complaints * [chore] update jslib * [review] Address code review * [review] Address code review * [review] Rename providerService to webProviderService Co-authored-by: Robyn MacCallum <robyntmaccallum@gmail.com> Co-authored-by: Hinton <oscar@oscarhinton.com>
2021-12-14 17:10:26 +01:00
constructor(
private apiService: ApiService,
private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService,
private cryptoService: CryptoService,
private messagingService: MessagingService,
private logService: LogService,
private stateService: StateService
) {}
async ngOnInit() {
const twoFactorProviders = await this.apiService.getTwoFactorProviders();
this.showTwoFactorEmailWarning = twoFactorProviders.data.some(
(p) => p.type === TwoFactorProviderType.Email && p.enabled
2021-12-07 20:41:45 +01:00
);
2018-06-21 20:28:00 +02:00
}
async submit() {
2018-08-14 21:14:04 +02:00
const hasEncKey = await this.cryptoService.hasEncKey();
if (!hasEncKey) {
2021-12-07 20:41:45 +01:00
this.platformUtilsService.showToast("error", null, this.i18nService.t("updateKey"));
2018-06-21 20:28:00 +02:00
return;
}
2018-06-21 20:28:00 +02:00
this.newEmail = this.newEmail.trim().toLowerCase();
if (!this.tokenSent) {
const request = new EmailTokenRequest();
request.newEmail = this.newEmail;
request.masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, null);
2021-12-17 15:57:11 +01:00
try {
2018-06-21 20:28:00 +02:00
this.formPromise = this.apiService.postEmailToken(request);
await this.formPromise;
this.tokenSent = true;
} catch (e) {
this.logService.error(e);
2021-12-17 15:57:11 +01:00
}
} else {
2018-06-21 20:28:00 +02:00
const request = new EmailRequest();
request.token = this.token;
request.newEmail = this.newEmail;
request.masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, null);
const kdf = await this.stateService.getKdfType();
[Account Switching] [Refactor] Implement new account centric services (#1220) * [chore] updated services.module to use account services * [refactor] sorted services provided by services.module * [chore] removed references to deleted jslib services * [chore] used activeAccount over storageService for account level storage items * [chore] resolved linter warnings * Refactor activeAccountService to stateService * [bug] Remove uneeded calls to state service on logout This was causing console erros on logout. Clearing of data is handled fully in dedicated services, clearing them in state afterwards is essentially a redundant call. * [bug] Add back null locked callback to VaultTimeoutService * Move call to get showUpdateKey * [bug] Ensure HtmlStorageService does not override StateService options and locations * [bug] Adjust theme logic to pull from the new storage locations * [bug] Correct theme not sticking on refresh * [bug] Add enableFullWidth to the account model * [bug] fix theme option empty when light is selected * [bug] init state on application start * [bug] Reinit state when coming back from a lock * [style] Fix lint complaints * [bug] Clean state on logout * [chore] Resolved merge issues * [bug] Correct default for enableGravitars * Bump angular to 12. * Remove angular.json * Bump rxjs * Fix build errors, remove file-loader with asset/resource * Use contenthash * Bump jslib * Bump ngx-toastr * [chore] resolve issues from merge * [chore] resolve issues from merge * [bug] Add missing bracket * Use newer import syntax * [bug] Correct service orge * [style] Fix lint complaints * [chore] update jslib * [review] Address code review * [review] Address code review * [review] Rename providerService to webProviderService Co-authored-by: Robyn MacCallum <robyntmaccallum@gmail.com> Co-authored-by: Hinton <oscar@oscarhinton.com>
2021-12-14 17:10:26 +01:00
const kdfIterations = await this.stateService.getKdfIterations();
const newKey = await this.cryptoService.makeKey(
2018-06-21 20:28:00 +02:00
this.masterPassword,
2018-08-14 21:14:04 +02:00
this.newEmail,
2021-12-17 15:57:11 +01:00
kdf,
[Account Switching] [Refactor] Implement new account centric services (#1220) * [chore] updated services.module to use account services * [refactor] sorted services provided by services.module * [chore] removed references to deleted jslib services * [chore] used activeAccount over storageService for account level storage items * [chore] resolved linter warnings * Refactor activeAccountService to stateService * [bug] Remove uneeded calls to state service on logout This was causing console erros on logout. Clearing of data is handled fully in dedicated services, clearing them in state afterwards is essentially a redundant call. * [bug] Add back null locked callback to VaultTimeoutService * Move call to get showUpdateKey * [bug] Ensure HtmlStorageService does not override StateService options and locations * [bug] Adjust theme logic to pull from the new storage locations * [bug] Correct theme not sticking on refresh * [bug] Add enableFullWidth to the account model * [bug] fix theme option empty when light is selected * [bug] init state on application start * [bug] Reinit state when coming back from a lock * [style] Fix lint complaints * [bug] Clean state on logout * [chore] Resolved merge issues * [bug] Correct default for enableGravitars * Bump angular to 12. * Remove angular.json * Bump rxjs * Fix build errors, remove file-loader with asset/resource * Use contenthash * Bump jslib * Bump ngx-toastr * [chore] resolve issues from merge * [chore] resolve issues from merge * [bug] Add missing bracket * Use newer import syntax * [bug] Correct service orge * [style] Fix lint complaints * [chore] update jslib * [review] Address code review * [review] Address code review * [review] Rename providerService to webProviderService Co-authored-by: Robyn MacCallum <robyntmaccallum@gmail.com> Co-authored-by: Hinton <oscar@oscarhinton.com>
2021-12-14 17:10:26 +01:00
kdfIterations
2021-12-17 15:57:11 +01:00
);
2018-06-21 20:28:00 +02:00
request.newMasterPasswordHash = await this.cryptoService.hashPassword(
this.masterPassword,
2021-12-17 15:57:11 +01:00
newKey
);
2018-06-21 20:28:00 +02:00
const newEncKey = await this.cryptoService.remakeEncKey(newKey);
request.key = newEncKey[1].encryptedString;
2021-12-17 15:57:11 +01:00
try {
2018-06-21 20:28:00 +02:00
this.formPromise = this.apiService.postEmail(request);
await this.formPromise;
this.reset();
this.platformUtilsService.showToast(
2021-12-17 15:57:11 +01:00
"success",
2018-06-21 20:28:00 +02:00
this.i18nService.t("emailChanged"),
this.i18nService.t("logBackIn")
2021-12-17 15:57:11 +01:00
);
2018-06-21 20:28:00 +02:00
this.messagingService.send("logout");
} catch (e) {
this.logService.error(e);
2021-12-17 15:57:11 +01:00
}
}
2021-12-17 15:57:11 +01:00
}
2018-06-21 20:28:00 +02:00
reset() {
this.token = this.newEmail = this.masterPassword = null;
this.tokenSent = false;
2021-12-17 15:57:11 +01:00
}
}