bitwarden-estensione-browser/apps/web/src/app/settings/update-key.component.ts

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

105 lines
3.8 KiB
TypeScript
Raw Normal View History

2018-07-17 23:22:51 +02:00
import { Component } from "@angular/core";
2022-06-14 17:10:53 +02:00
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { CipherService } from "@bitwarden/common/abstractions/cipher.service";
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { FolderService } from "@bitwarden/common/abstractions/folder.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 { SyncService } from "@bitwarden/common/abstractions/sync.service";
import { EncString } from "@bitwarden/common/models/domain/encString";
import { CipherWithIdRequest } from "@bitwarden/common/models/request/cipherWithIdRequest";
import { FolderWithIdRequest } from "@bitwarden/common/models/request/folderWithIdRequest";
import { UpdateKeyRequest } from "@bitwarden/common/models/request/updateKeyRequest";
2018-07-17 23:22:51 +02:00
@Component({
selector: "app-update-key",
templateUrl: "update-key.component.html",
})
export class UpdateKeyComponent {
masterPassword: string;
formPromise: Promise<any>;
constructor(
private apiService: ApiService,
private i18nService: I18nService,
2021-12-07 20:41:45 +01:00
private platformUtilsService: PlatformUtilsService,
private cryptoService: CryptoService,
private messagingService: MessagingService,
private syncService: SyncService,
private folderService: FolderService,
private cipherService: CipherService,
private logService: LogService
) {}
2018-07-17 23:22:51 +02:00
async submit() {
const hasEncKey = await this.cryptoService.hasEncKey();
if (hasEncKey) {
return;
}
if (this.masterPassword == null || this.masterPassword === "") {
2021-12-07 20:41:45 +01:00
this.platformUtilsService.showToast(
"error",
this.i18nService.t("errorOccurred"),
this.i18nService.t("masterPassRequired")
2018-07-17 23:22:51 +02:00
);
return;
2018-07-17 23:22:51 +02:00
}
2021-12-17 15:57:11 +01:00
try {
2018-07-17 23:22:51 +02:00
this.formPromise = this.makeRequest().then((request) => {
return this.apiService.postAccountKey(request);
2021-12-17 15:57:11 +01:00
});
2018-07-17 23:22:51 +02:00
await this.formPromise;
this.platformUtilsService.showToast(
"success",
this.i18nService.t("keyUpdated"),
2021-12-07 20:41:45 +01:00
this.i18nService.t("logBackInOthersToo"),
{ timeout: 15000 }
2021-12-17 15:57:11 +01:00
);
2018-07-17 23:22:51 +02:00
this.messagingService.send("logout");
} catch (e) {
this.logService.error(e);
2021-12-17 15:57:11 +01:00
}
}
2018-07-17 23:22:51 +02:00
private async makeRequest(): Promise<UpdateKeyRequest> {
const key = await this.cryptoService.getKey();
const encKey = await this.cryptoService.makeEncKey(key);
const privateKey = await this.cryptoService.getPrivateKey();
let encPrivateKey: EncString = null;
if (privateKey != null) {
encPrivateKey = await this.cryptoService.encrypt(privateKey, encKey[0]);
2021-12-17 15:57:11 +01:00
}
2018-07-17 23:22:51 +02:00
const request = new UpdateKeyRequest();
request.privateKey = encPrivateKey != null ? encPrivateKey.encryptedString : null;
request.key = encKey[1].encryptedString;
request.masterPasswordHash = await this.cryptoService.hashPassword(this.masterPassword, null);
await this.syncService.fullSync(true);
const folders = await this.folderService.getAllDecrypted();
for (let i = 0; i < folders.length; i++) {
if (folders[i].id == null) {
continue;
}
const folder = await this.folderService.encrypt(folders[i], encKey[0]);
request.folders.push(new FolderWithIdRequest(folder));
}
const ciphers = await this.cipherService.getAllDecrypted();
for (let i = 0; i < ciphers.length; i++) {
2018-07-19 19:31:14 +02:00
if (ciphers[i].organizationId != null) {
continue;
2021-12-17 15:57:11 +01:00
}
2018-07-17 23:22:51 +02:00
const cipher = await this.cipherService.encrypt(ciphers[i], encKey[0]);
request.ciphers.push(new CipherWithIdRequest(cipher));
}
2021-12-17 15:57:11 +01:00
2018-07-17 23:22:51 +02:00
return request;
2021-12-17 15:57:11 +01:00
}
2018-07-17 23:22:51 +02:00
}