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, private userVerificationService: UserVerificationService) { }
|
2018-05-17 16:58:30 +02:00
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
async run(password: string, options: program.OptionValues): Promise<Response> {
|
2021-09-15 15:57:43 +02:00
|
|
|
if (options.organizationid == null &&
|
|
|
|
await this.policyService.policyAppliesToUser(PolicyType.DisablePersonalVaultExport)) {
|
|
|
|
return Response.badRequest(
|
|
|
|
'One or more organization policies prevents you from exporting your personal vault.'
|
|
|
|
);
|
|
|
|
}
|
2021-11-16 10:42:30 +01:00
|
|
|
|
2020-03-12 20:37:44 +01:00
|
|
|
const canInteract = process.env.BW_NOINTERACTION !== 'true';
|
2021-11-16 10:42:30 +01:00
|
|
|
if (!canInteract) {
|
|
|
|
return Response.badRequest('User verification is required. Try running this command again in interactive mode.');
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|
2021-11-16 10:42:30 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await this.keyConnectorService.getUsesKeyConnector()
|
|
|
|
? await this.verifyOTP()
|
|
|
|
: await this.verifyMasterPassword(password);
|
|
|
|
} catch (e) {
|
|
|
|
return Response.badRequest(e.message);
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
let exportContent: string = null;
|
|
|
|
try {
|
|
|
|
exportContent = options.organizationid != null ?
|
|
|
|
await this.exportService.getOrganizationExport(options.organizationid, format) :
|
|
|
|
await this.exportService.getExport(format);
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e);
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|
2021-11-16 10:42:30 +01:00
|
|
|
return await this.saveFile(exportContent, options, format);
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
async saveFile(exportContent: string, options: program.OptionValues, format: string): Promise<Response> {
|
2018-05-17 19:28:22 +02:00
|
|
|
try {
|
2021-05-27 22:47:39 +02:00
|
|
|
const fileName = this.getFileName(format, options.organizationid != null ? 'org' : null);
|
2021-02-03 18:44:33 +01:00
|
|
|
return await CliUtils.saveResultToFile(exportContent, options.output, fileName);
|
2018-05-17 19:28:22 +02:00
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e.toString());
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-27 22:47:39 +02:00
|
|
|
|
|
|
|
private getFileName(format: string, prefix?: string) {
|
|
|
|
if (format === 'encrypted_json') {
|
|
|
|
if (prefix == null) {
|
|
|
|
prefix = 'encrypted';
|
|
|
|
} else {
|
|
|
|
prefix = 'encrypted_' + prefix;
|
|
|
|
}
|
|
|
|
format = 'json';
|
|
|
|
}
|
|
|
|
return this.exportService.getFileName(prefix, format);
|
|
|
|
}
|
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 })({
|
|
|
|
type: 'password',
|
|
|
|
name: 'password',
|
|
|
|
message: 'Master password:',
|
|
|
|
});
|
|
|
|
password = answer.password;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.userVerificationService.verifyUser({
|
|
|
|
type: VerificationType.MasterPassword,
|
|
|
|
secret: password,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async verifyOTP() {
|
|
|
|
await this.userVerificationService.requestOTP();
|
|
|
|
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({
|
|
|
|
type: 'password',
|
|
|
|
name: 'otp',
|
|
|
|
message: 'A verification code has been emailed to you.\n Verification code:',
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.userVerificationService.verifyUser({
|
|
|
|
type: VerificationType.OTP,
|
|
|
|
secret: answer.otp,
|
|
|
|
});
|
|
|
|
}
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|