bitwarden-estensione-browser/src/app/settings/emergency-access-view.compo...

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

104 lines
3.3 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, ViewChild, ViewContainerRef } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { ApiService } from "jslib-common/abstractions/api.service";
import { CipherService } from "jslib-common/abstractions/cipher.service";
import { CryptoService } from "jslib-common/abstractions/crypto.service";
import { ModalService } from "jslib-angular/services/modal.service";
import { CipherData } from "jslib-common/models/data/cipherData";
import { Cipher } from "jslib-common/models/domain/cipher";
import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey";
import { EmergencyAccessViewResponse } from "jslib-common/models/response/emergencyAccessResponse";
import { CipherView } from "jslib-common/models/view/cipherView";
import { EmergencyAccessAttachmentsComponent } from "./emergency-access-attachments.component";
import { EmergencyAddEditComponent } from "./emergency-add-edit.component";
@Component({
selector: "emergency-access-view",
templateUrl: "emergency-access-view.component.html",
})
export class EmergencyAccessViewComponent implements OnInit {
@ViewChild("cipherAddEdit", { read: ViewContainerRef, static: true })
cipherAddEditModalRef: ViewContainerRef;
@ViewChild("attachments", { read: ViewContainerRef, static: true })
attachmentsModalRef: ViewContainerRef;
2021-12-17 15:57:11 +01:00
id: string;
ciphers: CipherView[] = [];
loaded = false;
2021-12-17 15:57:11 +01:00
constructor(
private cipherService: CipherService,
private cryptoService: CryptoService,
private modalService: ModalService,
private router: Router,
private route: ActivatedRoute,
private apiService: ApiService
) {}
2021-12-17 15:57:11 +01:00
ngOnInit() {
this.route.params.subscribe((qParams) => {
if (qParams.id == null) {
return this.router.navigate(["settings/emergency-access"]);
}
2021-12-17 15:57:11 +01:00
this.id = qParams.id;
2021-12-17 15:57:11 +01:00
this.load();
});
}
2021-12-17 15:57:11 +01:00
async selectCipher(cipher: CipherView) {
const [_, childComponent] = await this.modalService.openViewRef(
EmergencyAddEditComponent,
this.cipherAddEditModalRef,
(comp) => {
comp.cipherId = cipher == null ? null : cipher.id;
comp.cipher = cipher;
}
);
2021-12-17 15:57:11 +01:00
return childComponent;
}
2021-12-17 15:57:11 +01:00
async load() {
const response = await this.apiService.postEmergencyAccessView(this.id);
this.ciphers = await this.getAllCiphers(response);
this.loaded = true;
}
2021-12-17 15:57:11 +01:00
async viewAttachments(cipher: CipherView) {
await this.modalService.openViewRef(
EmergencyAccessAttachmentsComponent,
this.attachmentsModalRef,
(comp) => {
comp.cipher = cipher;
comp.emergencyAccessId = this.id;
}
);
}
2021-12-17 15:57:11 +01:00
protected async getAllCiphers(response: EmergencyAccessViewResponse): Promise<CipherView[]> {
const ciphers = response.ciphers;
2021-12-17 15:57:11 +01:00
const decCiphers: CipherView[] = [];
const oldKeyBuffer = await this.cryptoService.rsaDecrypt(response.keyEncrypted);
const oldEncKey = new SymmetricCryptoKey(oldKeyBuffer);
2021-12-17 15:57:11 +01:00
const promises: any[] = [];
ciphers.forEach((cipherResponse) => {
const cipherData = new CipherData(cipherResponse);
const cipher = new Cipher(cipherData);
promises.push(cipher.decrypt(oldEncKey).then((c) => decCiphers.push(c)));
});
2021-12-17 15:57:11 +01:00
await Promise.all(promises);
decCiphers.sort(this.cipherService.getLocaleSortingFunction());
2021-12-17 15:57:11 +01:00
return decCiphers;
}
}