2020-12-22 16:57:44 +01:00
|
|
|
import { Component, OnInit, ViewChild, ViewContainerRef } from "@angular/core";
|
|
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { ApiService } from "jslib-common/abstractions/api.service";
|
|
|
|
import { CipherService } from "jslib-common/abstractions/cipher.service";
|
|
|
|
import { CryptoService } from "jslib-common/abstractions/crypto.service";
|
2020-12-22 16:57:44 +01:00
|
|
|
|
2021-08-27 14:50:58 +02:00
|
|
|
import { ModalService } from "jslib-angular/services/modal.service";
|
|
|
|
|
2021-09-17 15:44:34 +02:00
|
|
|
import { CipherData } from "jslib-common/models/data/cipherData";
|
|
|
|
import { Cipher } from "jslib-common/models/domain/cipher";
|
|
|
|
import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey";
|
2021-06-07 20:13:58 +02:00
|
|
|
import { EmergencyAccessViewResponse } from "jslib-common/models/response/emergencyAccessResponse";
|
|
|
|
import { CipherView } from "jslib-common/models/view/cipherView";
|
2020-12-22 16:57:44 +01:00
|
|
|
|
2021-02-01 17:37:32 +01:00
|
|
|
import { EmergencyAccessAttachmentsComponent } from "./emergency-access-attachments.component";
|
2020-12-22 16:57:44 +01:00
|
|
|
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;
|
2021-02-01 17:37:32 +01:00
|
|
|
@ViewChild("attachments", { read: ViewContainerRef, static: true })
|
|
|
|
attachmentsModalRef: ViewContainerRef;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-12-22 16:57:44 +01:00
|
|
|
id: string;
|
|
|
|
ciphers: CipherView[] = [];
|
2021-10-15 01:40:35 +02:00
|
|
|
loaded = false;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-12-22 16:57:44 +01:00
|
|
|
constructor(
|
|
|
|
private cipherService: CipherService,
|
|
|
|
private cryptoService: CryptoService,
|
2021-08-27 14:50:58 +02:00
|
|
|
private modalService: ModalService,
|
|
|
|
private router: Router,
|
2020-12-22 16:57:44 +01:00
|
|
|
private route: ActivatedRoute,
|
|
|
|
private apiService: ApiService
|
|
|
|
) {}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-12-22 16:57:44 +01:00
|
|
|
ngOnInit() {
|
2021-02-03 18:41:33 +01:00
|
|
|
this.route.params.subscribe((qParams) => {
|
2020-12-22 16:57:44 +01:00
|
|
|
if (qParams.id == null) {
|
|
|
|
return this.router.navigate(["settings/emergency-access"]);
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-12-22 16:57:44 +01:00
|
|
|
this.id = qParams.id;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-12-22 16:57:44 +01:00
|
|
|
this.load();
|
|
|
|
});
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-08-27 14:50:58 +02: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;
|
2020-12-22 16:57:44 +01:00
|
|
|
}
|
|
|
|
);
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-12-22 16:57:44 +01:00
|
|
|
return childComponent;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-12-22 16:57:44 +01:00
|
|
|
async load() {
|
|
|
|
const response = await this.apiService.postEmergencyAccessView(this.id);
|
|
|
|
this.ciphers = await this.getAllCiphers(response);
|
2021-10-15 01:40:35 +02:00
|
|
|
this.loaded = true;
|
2020-12-22 16:57:44 +01:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2021-02-01 17:37:32 +01:00
|
|
|
async viewAttachments(cipher: CipherView) {
|
2021-08-27 14:50:58 +02:00
|
|
|
await this.modalService.openViewRef(
|
|
|
|
EmergencyAccessAttachmentsComponent,
|
|
|
|
this.attachmentsModalRef,
|
|
|
|
(comp) => {
|
|
|
|
comp.cipher = cipher;
|
|
|
|
comp.emergencyAccessId = this.id;
|
2021-02-01 17:37:32 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-12-22 16:57:44 +01:00
|
|
|
protected async getAllCiphers(response: EmergencyAccessViewResponse): Promise<CipherView[]> {
|
|
|
|
const ciphers = response.ciphers;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-12-22 16:57:44 +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
|
|
|
|
2020-12-22 16:57:44 +01:00
|
|
|
const promises: any[] = [];
|
2021-02-03 18:41:33 +01:00
|
|
|
ciphers.forEach((cipherResponse) => {
|
2020-12-22 16:57:44 +01:00
|
|
|
const cipherData = new CipherData(cipherResponse);
|
|
|
|
const cipher = new Cipher(cipherData);
|
2021-02-03 18:41:33 +01:00
|
|
|
promises.push(cipher.decrypt(oldEncKey).then((c) => decCiphers.push(c)));
|
2020-12-22 16:57:44 +01:00
|
|
|
});
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-12-22 16:57:44 +01:00
|
|
|
await Promise.all(promises);
|
|
|
|
decCiphers.sort(this.cipherService.getLocaleSortingFunction());
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2020-12-22 16:57:44 +01:00
|
|
|
return decCiphers;
|
|
|
|
}
|
|
|
|
}
|