2021-12-17 15:57:11 +01:00
|
|
|
import { Component, OnInit } from "@angular/core";
|
2018-08-28 04:40:03 +02:00
|
|
|
|
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 { DEFAULT_KDF_ITERATIONS, KdfType } from "@bitwarden/common/enums/kdfType";
|
|
|
|
import { KdfRequest } from "@bitwarden/common/models/request/kdfRequest";
|
2018-08-28 04:40:03 +02:00
|
|
|
|
|
|
|
@Component({
|
2021-12-17 15:57:11 +01:00
|
|
|
selector: "app-change-kdf",
|
|
|
|
templateUrl: "change-kdf.component.html",
|
2018-08-28 04:40:03 +02:00
|
|
|
})
|
|
|
|
export class ChangeKdfComponent implements OnInit {
|
2021-12-17 15:57:11 +01:00
|
|
|
masterPassword: string;
|
|
|
|
kdfIterations: number;
|
|
|
|
kdf = KdfType.PBKDF2_SHA256;
|
|
|
|
kdfOptions: any[] = [];
|
|
|
|
formPromise: Promise<any>;
|
2022-03-24 11:15:48 +01:00
|
|
|
recommendedKdfIterations = DEFAULT_KDF_ITERATIONS;
|
2018-08-28 04:40:03 +02:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
constructor(
|
|
|
|
private apiService: ApiService,
|
|
|
|
private i18nService: I18nService,
|
|
|
|
private platformUtilsService: PlatformUtilsService,
|
|
|
|
private cryptoService: CryptoService,
|
|
|
|
private messagingService: MessagingService,
|
|
|
|
private logService: LogService,
|
|
|
|
private stateService: StateService
|
|
|
|
) {
|
|
|
|
this.kdfOptions = [{ name: "PBKDF2 SHA-256", value: KdfType.PBKDF2_SHA256 }];
|
|
|
|
}
|
2018-08-28 04:40:03 +02:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
async ngOnInit() {
|
|
|
|
this.kdf = await this.stateService.getKdfType();
|
|
|
|
this.kdfIterations = await this.stateService.getKdfIterations();
|
|
|
|
}
|
2018-08-28 04:40:03 +02:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
async submit() {
|
|
|
|
const hasEncKey = await this.cryptoService.hasEncKey();
|
|
|
|
if (!hasEncKey) {
|
|
|
|
this.platformUtilsService.showToast("error", null, this.i18nService.t("updateKey"));
|
|
|
|
return;
|
|
|
|
}
|
2018-08-28 04:40:03 +02:00
|
|
|
|
2021-12-17 15:57:11 +01:00
|
|
|
const request = new KdfRequest();
|
|
|
|
request.kdf = this.kdf;
|
|
|
|
request.kdfIterations = this.kdfIterations;
|
|
|
|
request.masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, null);
|
|
|
|
const email = await this.stateService.getEmail();
|
|
|
|
const newKey = await this.cryptoService.makeKey(
|
|
|
|
this.masterPassword,
|
|
|
|
email,
|
|
|
|
this.kdf,
|
|
|
|
this.kdfIterations
|
|
|
|
);
|
|
|
|
request.newMasterPasswordHash = await this.cryptoService.hashPassword(
|
|
|
|
this.masterPassword,
|
|
|
|
newKey
|
|
|
|
);
|
|
|
|
const newEncKey = await this.cryptoService.remakeEncKey(newKey);
|
|
|
|
request.key = newEncKey[1].encryptedString;
|
|
|
|
try {
|
|
|
|
this.formPromise = this.apiService.postAccountKdf(request);
|
|
|
|
await this.formPromise;
|
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"success",
|
|
|
|
this.i18nService.t("encKeySettingsChanged"),
|
|
|
|
this.i18nService.t("logBackIn")
|
|
|
|
);
|
|
|
|
this.messagingService.send("logout");
|
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2018-08-28 04:40:03 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-08-28 04:40:03 +02:00
|
|
|
}
|