2022-06-16 22:20:10 +02:00
|
|
|
import {
|
|
|
|
Directive,
|
|
|
|
EventEmitter,
|
|
|
|
OnInit,
|
|
|
|
Output,
|
|
|
|
ViewChild,
|
|
|
|
ViewContainerRef,
|
|
|
|
} from "@angular/core";
|
2021-11-09 17:01:22 +01:00
|
|
|
import { FormBuilder } from "@angular/forms";
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2022-06-20 18:50:56 +02:00
|
|
|
import { ModalConfig, ModalService } from "@bitwarden/angular/services/modal.service";
|
2022-06-16 22:20:10 +02:00
|
|
|
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";
|
2022-06-16 22:20:10 +02:00
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { UserVerificationService } from "@bitwarden/common/abstractions/userVerification.service";
|
2022-06-21 22:17:57 +02:00
|
|
|
import { UserVerificationPromptService } from "@bitwarden/common/abstractions/userVerificationPrompt.service";
|
|
|
|
import { EncryptedExportType } from "@bitwarden/common/enums/EncryptedExportType";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { EventType } from "@bitwarden/common/enums/eventType";
|
|
|
|
import { PolicyType } from "@bitwarden/common/enums/policyType";
|
2018-04-10 01:05:13 +02:00
|
|
|
|
2020-08-17 18:14:40 +02:00
|
|
|
@Directive()
|
2021-09-14 16:32:06 +02:00
|
|
|
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;
|
2022-06-16 22:20:10 +02:00
|
|
|
|
|
|
|
@ViewChild("viewUserApiKeyTemplate", { read: ViewContainerRef, static: true })
|
|
|
|
viewUserApiKeyModalRef: ViewContainerRef;
|
|
|
|
|
|
|
|
encryptionPassword: string;
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-12-31 16:05:23 +01:00
|
|
|
exportForm = this.formBuilder.group({
|
2021-11-09 17:01:22 +01:00
|
|
|
format: ["json"],
|
|
|
|
secret: [""],
|
2022-06-16 22:20:10 +02:00
|
|
|
password: [""],
|
|
|
|
confirmPassword: [""],
|
|
|
|
fileEncryptionType: [""],
|
2021-11-09 17:01:22 +01:00
|
|
|
});
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-11-09 17:01:22 +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
|
|
|
|
2018-10-03 06:03:49 +02:00
|
|
|
constructor(
|
|
|
|
protected cryptoService: CryptoService,
|
|
|
|
protected i18nService: I18nService,
|
2018-08-14 21:12:10 +02:00
|
|
|
protected platformUtilsService: PlatformUtilsService,
|
|
|
|
protected exportService: ExportService,
|
2021-10-19 10:32:14 +02:00
|
|
|
protected eventService: EventService,
|
|
|
|
private policyService: PolicyService,
|
|
|
|
protected win: Window,
|
2021-11-09 17:01:22 +01:00
|
|
|
private logService: LogService,
|
|
|
|
private userVerificationService: UserVerificationService,
|
2022-06-21 23:58:20 +02:00
|
|
|
protected formBuilder: FormBuilder,
|
2022-06-16 22:20:10 +02:00
|
|
|
protected modalService: ModalService,
|
|
|
|
protected apiService: ApiService,
|
2022-06-20 18:50:56 +02:00
|
|
|
protected stateService: StateService,
|
2022-06-21 22:17:57 +02:00
|
|
|
protected userVerificationPromptService: UserVerificationPromptService,
|
2022-06-20 18:50:56 +02:00
|
|
|
protected modalConfig: ModalConfig
|
2021-11-09 17:01:22 +01:00
|
|
|
) {}
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2021-09-14 16:32:06 +02:00
|
|
|
async ngOnInit() {
|
|
|
|
await this.checkExportDisabled();
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2021-09-14 16:32:06 +02:00
|
|
|
async checkExportDisabled() {
|
|
|
|
this.disabledByPolicy = await this.policyService.policyAppliesToUser(
|
|
|
|
PolicyType.DisablePersonalVaultExport
|
2021-12-16 13:36:21 +01:00
|
|
|
);
|
2021-11-09 17:01:22 +01:00
|
|
|
if (this.disabledByPolicy) {
|
|
|
|
this.exportForm.disable();
|
2021-09-14 16:32:06 +02:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2020-12-03 21:20:38 +01:00
|
|
|
get encryptedFormat() {
|
2021-09-14 16:32:06 +02:00
|
|
|
return this.format === "encrypted_json";
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 22:20:10 +02: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);
|
|
|
|
}
|
2022-06-16 22:20:10 +02:00
|
|
|
}
|
|
|
|
|
2021-09-14 16:32:06 +02:00
|
|
|
async submit() {
|
|
|
|
if (this.disabledByPolicy) {
|
|
|
|
this.platformUtilsService.showToast(
|
2021-12-16 13:36:21 +01:00
|
|
|
"error",
|
|
|
|
null,
|
2021-09-14 16:32:06 +02:00
|
|
|
this.i18nService.t("personalVaultExportPolicyInEffect")
|
2021-11-09 17:01:22 +01:00
|
|
|
);
|
2020-12-03 21:20:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-21 22:17:57 +02:00
|
|
|
const acceptedWarning = await this.warningDialog();
|
|
|
|
if (!acceptedWarning) {
|
|
|
|
return;
|
2021-03-04 23:56:20 +01:00
|
|
|
}
|
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;
|
2021-11-09 17:01:22 +01:00
|
|
|
this.downloadFile(data);
|
2018-04-10 01:05:13 +02:00
|
|
|
this.saved();
|
2021-11-09 17:01:22 +01:00
|
|
|
await this.collectEvent();
|
2018-04-10 01:05:13 +02:00
|
|
|
this.exportForm.get("secret").setValue("");
|
2021-11-09 17:01:22 +01:00
|
|
|
} 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>" +
|
2021-03-04 23:56:20 +01:00
|
|
|
this.i18nService.t("encExportAccountWarningDesc"),
|
2018-12-17 16:54:03 +01:00
|
|
|
this.i18nService.t("confirmVaultExport"),
|
2021-03-04 23:56:20 +01:00
|
|
|
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(
|
2021-03-04 23:56:20 +01:00
|
|
|
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() {
|
2022-06-16 22:20:10 +02:00
|
|
|
return this.exportService.getExport(this.format, null, this.encryptionPassword);
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2018-07-05 20:39:58 +02:00
|
|
|
protected getFileName(prefix?: string) {
|
2020-12-03 21:20:38 +01:00
|
|
|
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
|
|
|
}
|
2020-12-03 21:20:38 +01: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);
|
|
|
|
}
|
|
|
|
|
2022-06-16 22:20:10 +02:00
|
|
|
protected clearPasswordField() {
|
|
|
|
this.encryptionPassword = "";
|
|
|
|
}
|
|
|
|
|
2021-11-09 17:01:22 +01:00
|
|
|
get format() {
|
|
|
|
return this.exportForm.get("format").value;
|
|
|
|
}
|
|
|
|
|
2022-06-16 22:20:10 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|