1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-21 11:54:02 +00:00

Rough draft of Export/Import changes w/ password encryption

This commit is contained in:
CarleyDiaz-Bitwarden
2022-06-16 16:20:10 -04:00
parent 4106eead02
commit f6a7315d13
19 changed files with 647 additions and 44 deletions

View File

@@ -1,6 +1,14 @@
import { Directive, EventEmitter, OnInit, Output } from "@angular/core";
import {
Directive,
EventEmitter,
OnInit,
Output,
ViewChild,
ViewContainerRef,
} from "@angular/core";
import { FormBuilder } from "@angular/forms";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
import { EventService } from "@bitwarden/common/abstractions/event.service";
import { ExportService } from "@bitwarden/common/abstractions/export.service";
@@ -8,20 +16,32 @@ 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";
import { UserVerificationService } from "@bitwarden/common/abstractions/userVerification.service";
import { EventType } from "@bitwarden/common/enums/eventType";
import { PolicyType } from "@bitwarden/common/enums/policyType";
import { ModalService } from "../services/modal.service";
@Directive()
export class ExportComponent implements OnInit {
@Output() onSaved = new EventEmitter();
formPromise: Promise<string>;
disabledByPolicy = false;
private alreadyVerified = false;
@ViewChild("viewUserApiKeyTemplate", { read: ViewContainerRef, static: true })
viewUserApiKeyModalRef: ViewContainerRef;
encryptionPassword: string;
exportForm = this.formBuilder.group({
format: ["json"],
secret: [""],
password: [""],
confirmPassword: [""],
fileEncryptionType: [""],
});
formatOptions = [
@@ -40,7 +60,10 @@ export class ExportComponent implements OnInit {
protected win: Window,
private logService: LogService,
private userVerificationService: UserVerificationService,
private formBuilder: FormBuilder
private formBuilder: FormBuilder,
protected modalService: ModalService,
protected apiService: ApiService,
protected stateService: StateService
) {}
async ngOnInit() {
@@ -60,6 +83,15 @@ export class ExportComponent implements OnInit {
return this.format === "encrypted_json";
}
async submitWithSecretAlreadyVerified() {
if (!this.validForm) {
return;
}
this.alreadyVerified = true;
this.submit();
}
async submit() {
if (this.disabledByPolicy) {
this.platformUtilsService.showToast(
@@ -70,19 +102,24 @@ export class ExportComponent implements OnInit {
return;
}
const acceptedWarning = await this.warningDialog();
if (!acceptedWarning) {
return;
}
if (!this.alreadyVerified) {
const acceptedWarning = await this.warningDialog();
if (!acceptedWarning) {
return;
}
const secret = this.exportForm.get("secret").value;
const secret = this.exportForm.get("secret").value;
try {
await this.userVerificationService.verifyUser(secret);
} catch (e) {
this.platformUtilsService.showToast("error", this.i18nService.t("errorOccurred"), e.message);
return;
try {
await this.userVerificationService.verifyUser(secret);
} catch (e) {
this.platformUtilsService.showToast(
"error",
this.i18nService.t("errorOccurred"),
e.message
);
return;
}
}
try {
this.formPromise = this.getExportData();
const data = await this.formPromise;
@@ -124,7 +161,7 @@ export class ExportComponent implements OnInit {
}
protected getExportData() {
return this.exportService.getExport(this.format);
return this.exportService.getExport(this.format, null, this.encryptionPassword);
}
protected getFileName(prefix?: string) {
@@ -144,10 +181,52 @@ export class ExportComponent implements OnInit {
await this.eventService.collect(EventType.User_ClientExportedVault);
}
get validForm() {
//TODO check if fileEncryption type is null?
if (this.fileEncryptionType == 2 && this.format == "encrypted_json") {
//password encryption type
const password = this.password;
const confirmPassword = this.confirmPassword;
if (password.length > 0 || confirmPassword.length > 0) {
if (password != confirmPassword) {
this.platformUtilsService.showToast(
"error",
this.i18nService.t("errorOccurred"),
"File Password and Confirm File Password do not match."
);
return false;
}
this.encryptionPassword = password;
return true;
}
} else {
this.clearPasswordField();
return true;
}
}
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;
}
private downloadFile(csv: string): void {
const fileName = this.getFileName();
this.platformUtilsService.saveFile(this.win, csv, { type: "text/plain" }, fileName);