bitwarden-estensione-browser/libs/angular/src/components/export.component.ts

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

223 lines
6.4 KiB
TypeScript
Raw Normal View History

import {
Directive,
EventEmitter,
OnInit,
Output,
ViewChild,
ViewContainerRef,
} from "@angular/core";
import { FormBuilder } from "@angular/forms";
2021-12-16 13:36:21 +01:00
import { ModalConfig, ModalService } from "@bitwarden/angular/services/modal.service";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
2022-06-14 17:10:53 +02:00
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { EventService } from "@bitwarden/common/abstractions/event.service";
import { ExportService } from "@bitwarden/common/abstractions/export.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
import { PolicyService } from "@bitwarden/common/abstractions/policy.service";
import { StateService } from "@bitwarden/common/abstractions/state.service";
2022-06-14 17:10:53 +02:00
import { UserVerificationService } from "@bitwarden/common/abstractions/userVerification.service";
import { EventType } from "@bitwarden/common/enums/eventType";
import { PolicyType } from "@bitwarden/common/enums/policyType";
2018-04-10 01:05:13 +02:00
@Directive()
export class ExportComponent implements OnInit {
2018-04-10 01:05:13 +02:00
@Output() onSaved = new EventEmitter();
2021-12-16 13:36:21 +01:00
2018-07-05 20:39:58 +02:00
formPromise: Promise<string>;
2022-02-22 15:39:11 +01:00
disabledByPolicy = false;
@ViewChild("viewUserApiKeyTemplate", { read: ViewContainerRef, static: true })
viewUserApiKeyModalRef: ViewContainerRef;
encryptionPassword: string;
2021-12-16 13:36:21 +01:00
exportForm = this.formBuilder.group({
format: ["json"],
secret: [""],
password: [""],
confirmPassword: [""],
fileEncryptionType: [""],
});
2021-12-16 13:36:21 +01:00
formatOptions = [
{ name: ".json", value: "json" },
{ name: ".csv", value: "csv" },
{ name: ".json (Encrypted)", value: "encrypted_json" },
];
2021-12-16 13:36:21 +01:00
constructor(
protected cryptoService: CryptoService,
protected i18nService: I18nService,
2018-08-14 21:12:10 +02:00
protected platformUtilsService: PlatformUtilsService,
protected exportService: ExportService,
protected eventService: EventService,
private policyService: PolicyService,
protected win: Window,
private logService: LogService,
private userVerificationService: UserVerificationService,
2022-06-21 23:58:20 +02:00
protected formBuilder: FormBuilder,
protected modalService: ModalService,
protected apiService: ApiService,
protected stateService: StateService,
protected modalConfig: ModalConfig
) {}
2021-12-16 13:36:21 +01:00
async ngOnInit() {
await this.checkExportDisabled();
2021-12-16 13:36:21 +01:00
}
async checkExportDisabled() {
this.disabledByPolicy = await this.policyService.policyAppliesToUser(
PolicyType.DisablePersonalVaultExport
2021-12-16 13:36:21 +01:00
);
if (this.disabledByPolicy) {
this.exportForm.disable();
}
2021-12-16 13:36:21 +01:00
}
get encryptedFormat() {
return this.format === "encrypted_json";
2021-12-16 13:36:21 +01:00
}
async submitWithSecretAlreadyVerified() {
2022-06-21 22:17:57 +02:00
if (this.disabledByPolicy) {
this.platformUtilsService.showToast(
"error",
null,
this.i18nService.t("personalVaultExportPolicyInEffect")
);
return;
}
try {
this.formPromise = this.getExportData();
const data = await this.formPromise;
this.downloadFile(data);
this.saved();
await this.collectEvent();
this.exportForm.get("secret").setValue("");
2022-06-21 23:58:20 +02:00
this.exportForm.get("password").setValue("");
this.exportForm.get("confirmPassword").setValue("");
2022-06-21 22:17:57 +02:00
} catch (e) {
this.logService.error(e);
}
}
async submit() {
if (this.disabledByPolicy) {
this.platformUtilsService.showToast(
2021-12-16 13:36:21 +01:00
"error",
null,
this.i18nService.t("personalVaultExportPolicyInEffect")
);
return;
}
2022-06-21 22:17:57 +02:00
const acceptedWarning = await this.warningDialog();
if (!acceptedWarning) {
return;
}
2022-06-21 22:17:57 +02:00
const secret = this.exportForm.get("secret").value;
const successfulVerification = await this.userVerificationService.verifyUser(secret);
if (!successfulVerification) {
this.platformUtilsService.showToast(
"error",
this.i18nService.t("error"),
this.i18nService.t("invalidMasterPassword")
);
return;
}
2018-04-10 01:05:13 +02:00
try {
this.formPromise = this.getExportData();
const data = await this.formPromise;
this.downloadFile(data);
2018-04-10 01:05:13 +02:00
this.saved();
await this.collectEvent();
2018-04-10 01:05:13 +02:00
this.exportForm.get("secret").setValue("");
} catch (e) {
this.logService.error(e);
2018-04-10 01:05:13 +02:00
}
2021-12-16 13:36:21 +01:00
}
2018-07-05 20:39:58 +02:00
async warningDialog() {
if (this.encryptedFormat) {
return await this.platformUtilsService.showDialog(
2021-12-16 13:36:21 +01:00
"<p>" +
2018-12-17 16:54:03 +01:00
this.i18nService.t("encExportKeyWarningDesc") +
2021-12-16 13:36:21 +01:00
"<p>" +
this.i18nService.t("encExportAccountWarningDesc"),
2018-12-17 16:54:03 +01:00
this.i18nService.t("confirmVaultExport"),
this.i18nService.t("exportVault"),
this.i18nService.t("cancel"),
2021-12-16 13:36:21 +01:00
"warning",
true
2018-12-17 16:54:03 +01:00
);
2021-12-16 13:36:21 +01:00
} else {
2018-12-17 16:54:03 +01:00
return await this.platformUtilsService.showDialog(
this.i18nService.t("exportWarningDesc"),
this.i18nService.t("confirmVaultExport"),
this.i18nService.t("exportVault"),
this.i18nService.t("cancel"),
2021-12-16 13:36:21 +01:00
"warning"
);
2018-07-05 20:39:58 +02:00
}
2021-12-16 13:36:21 +01:00
}
2019-07-12 23:11:36 +02:00
protected saved() {
this.onSaved.emit();
2021-12-16 13:36:21 +01:00
}
2018-07-05 20:39:58 +02:00
protected getExportData() {
return (this.fileEncryptionType != 1 && this.password == undefined) || this.password == ""
? this.exportService.getExport(this.format, null)
: this.exportService.getPasswordProtectedExport(this.password);
2021-12-16 13:36:21 +01:00
}
2018-07-05 20:39:58 +02:00
protected getFileName(prefix?: string) {
let extension = this.format;
if (this.format === "encrypted_json") {
if (prefix == null) {
prefix = "encrypted";
} else {
prefix = "encrypted_" + prefix;
}
extension = "json";
2018-07-05 20:39:58 +02:00
}
return this.exportService.getFileName(prefix, extension);
2021-12-16 13:36:21 +01:00
}
2018-07-05 20:39:58 +02:00
2019-07-12 23:11:36 +02:00
protected async collectEvent(): Promise<any> {
await this.eventService.collect(EventType.User_ClientExportedVault);
}
protected clearPasswordField() {
this.encryptionPassword = "";
}
get format() {
return this.exportForm.get("format").value;
}
get password() {
return this.exportForm.get("password").value;
}
get confirmPassword() {
return this.exportForm.get("confirmPassword").value;
}
get fileEncryptionType() {
return this.exportForm.get("fileEncryptionType").value;
}
2018-04-10 01:05:13 +02:00
private downloadFile(csv: string): void {
2018-07-05 20:39:58 +02:00
const fileName = this.getFileName();
2018-04-10 01:05:13 +02:00
this.platformUtilsService.saveFile(this.win, csv, { type: "text/plain" }, fileName);
}
}