2018-05-17 16:58:30 +02:00
|
|
|
import * as program from "commander";
|
2018-05-23 17:16:23 +02:00
|
|
|
import * as inquirer from "inquirer";
|
2018-05-17 16:58:30 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { ExportService } from "jslib-common/abstractions/export.service";
|
2021-11-16 10:42:30 +01:00
|
|
|
import { KeyConnectorService } from "jslib-common/abstractions/keyConnector.service";
|
2021-09-15 15:57:43 +02:00
|
|
|
import { PolicyService } from "jslib-common/abstractions/policy.service";
|
2021-11-16 10:42:30 +01:00
|
|
|
import { UserVerificationService } from "jslib-common/abstractions/userVerification.service";
|
2018-05-17 16:58:30 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { Response } from "jslib-node/cli/models/response";
|
2018-05-17 16:58:30 +02:00
|
|
|
|
2021-09-15 15:57:43 +02:00
|
|
|
import { PolicyType } from "jslib-common/enums/policyType";
|
2021-11-16 10:42:30 +01:00
|
|
|
import { VerificationType } from "jslib-common/enums/verificationType";
|
2018-05-17 16:58:30 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { Utils } from "jslib-common/misc/utils";
|
2019-10-07 17:01:30 +02:00
|
|
|
|
2021-09-15 15:57:43 +02:00
|
|
|
import { CliUtils } from "../utils";
|
|
|
|
|
2018-05-17 16:58:30 +02:00
|
|
|
export class ExportCommand {
|
2021-11-16 10:42:30 +01:00
|
|
|
constructor(
|
|
|
|
private exportService: ExportService,
|
|
|
|
private policyService: PolicyService,
|
|
|
|
private keyConnectorService: KeyConnectorService,
|
2021-09-15 15:57:43 +02:00
|
|
|
private userVerificationService: UserVerificationService
|
2021-11-16 10:42:30 +01:00
|
|
|
) {}
|
2021-12-20 18:04:00 +01:00
|
|
|
|
2021-11-16 10:42:30 +01:00
|
|
|
async run(password: string, options: program.OptionValues): Promise<Response> {
|
|
|
|
if (
|
|
|
|
options.organizationid == null &&
|
|
|
|
(await this.policyService.policyAppliesToUser(PolicyType.DisablePersonalVaultExport))
|
2021-12-20 18:04:00 +01:00
|
|
|
) {
|
2021-11-16 10:42:30 +01:00
|
|
|
return Response.badRequest(
|
|
|
|
"One or more organization policies prevents you from exporting your personal vault."
|
|
|
|
);
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
const canInteract = process.env.BW_NOINTERACTION !== "true";
|
|
|
|
if (!canInteract) {
|
|
|
|
return Response.badRequest(
|
|
|
|
"User verification is required. Try running this command again in interactive mode."
|
2018-05-17 19:28:22 +02:00
|
|
|
);
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|
2021-05-27 22:47:39 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
(await this.keyConnectorService.getUsesKeyConnector())
|
2021-11-16 10:42:30 +01:00
|
|
|
? await this.verifyOTP()
|
2021-05-27 22:47:39 +02:00
|
|
|
: await this.verifyMasterPassword(password);
|
|
|
|
} catch (e) {
|
|
|
|
return Response.badRequest(e.message);
|
|
|
|
}
|
2021-11-16 10:42:30 +01:00
|
|
|
|
|
|
|
let format = options.format;
|
|
|
|
if (format !== "encrypted_json" && format !== "json") {
|
|
|
|
format = "csv";
|
|
|
|
}
|
|
|
|
if (options.organizationid != null && !Utils.isGuid(options.organizationid)) {
|
|
|
|
return Response.error("`" + options.organizationid + "` is not a GUID.");
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2021-11-16 10:42:30 +01:00
|
|
|
let exportContent: string = null;
|
2021-12-20 18:04:00 +01:00
|
|
|
try {
|
2021-11-16 10:42:30 +01:00
|
|
|
exportContent =
|
2021-05-27 22:47:39 +02:00
|
|
|
options.organizationid != null
|
2021-11-16 10:42:30 +01:00
|
|
|
? await this.exportService.getOrganizationExport(options.organizationid, format)
|
|
|
|
: await this.exportService.getExport(format);
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e);
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2021-11-16 10:42:30 +01:00
|
|
|
return await this.saveFile(exportContent, options, format);
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
async saveFile(
|
2021-11-16 10:42:30 +01:00
|
|
|
exportContent: string,
|
|
|
|
options: program.OptionValues,
|
|
|
|
format: string
|
|
|
|
): Promise<Response> {
|
2021-12-20 18:04:00 +01:00
|
|
|
try {
|
2021-11-16 10:42:30 +01:00
|
|
|
const fileName = this.getFileName(format, options.organizationid != null ? "org" : null);
|
|
|
|
return await CliUtils.saveResultToFile(exportContent, options.output, fileName);
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e.toString());
|
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
2021-05-27 22:47:39 +02:00
|
|
|
private getFileName(format: string, prefix?: string) {
|
|
|
|
if (format === "encrypted_json") {
|
|
|
|
if (prefix == null) {
|
|
|
|
prefix = "encrypted";
|
2021-12-20 18:04:00 +01:00
|
|
|
} else {
|
2021-05-27 22:47:39 +02:00
|
|
|
prefix = "encrypted_" + prefix;
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2021-05-27 22:47:39 +02:00
|
|
|
format = "json";
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2021-11-16 10:42:30 +01:00
|
|
|
return this.exportService.getFileName(prefix, format);
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 10:42:30 +01:00
|
|
|
private async verifyMasterPassword(password: string) {
|
|
|
|
if (password == null || password === "") {
|
|
|
|
const answer: inquirer.Answers = await inquirer.createPromptModule({
|
|
|
|
output: process.stderr,
|
2021-12-20 18:04:00 +01:00
|
|
|
})({
|
2021-11-16 10:42:30 +01:00
|
|
|
type: "password",
|
|
|
|
name: "password",
|
|
|
|
message: "Master password:",
|
2021-12-20 18:04:00 +01:00
|
|
|
});
|
2021-11-16 10:42:30 +01:00
|
|
|
password = answer.password;
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
2021-11-16 10:42:30 +01:00
|
|
|
await this.userVerificationService.verifyUser({
|
|
|
|
type: VerificationType.MasterPassword,
|
|
|
|
secret: password,
|
2021-12-20 18:04:00 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-16 10:42:30 +01:00
|
|
|
private async verifyOTP() {
|
|
|
|
await this.userVerificationService.requestOTP();
|
2021-12-28 21:38:51 +01:00
|
|
|
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({
|
2021-11-16 10:42:30 +01:00
|
|
|
type: "password",
|
2021-12-20 18:04:00 +01:00
|
|
|
name: "otp",
|
2021-11-16 10:42:30 +01:00
|
|
|
message: "A verification code has been emailed to you.\n Verification code:",
|
2021-12-20 18:04:00 +01:00
|
|
|
});
|
|
|
|
|
2021-11-16 10:42:30 +01:00
|
|
|
await this.userVerificationService.verifyUser({
|
|
|
|
type: VerificationType.OTP,
|
|
|
|
secret: answer.otp,
|
2021-12-20 18:04:00 +01:00
|
|
|
});
|
|
|
|
}
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|