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 { CryptoService } from 'jslib-common/abstractions/crypto.service';
|
|
|
|
import { ExportService } from 'jslib-common/abstractions/export.service';
|
2021-09-15 15:57:43 +02:00
|
|
|
import { PolicyService } from 'jslib-common/abstractions/policy.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';
|
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-09-15 15:57:43 +02:00
|
|
|
constructor(private cryptoService: CryptoService, private exportService: ExportService,
|
|
|
|
private policyService: PolicyService) { }
|
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.'
|
|
|
|
);
|
|
|
|
}
|
2020-03-12 20:37:44 +01:00
|
|
|
const canInteract = process.env.BW_NOINTERACTION !== 'true';
|
2019-10-21 22:04:51 +02:00
|
|
|
if ((password == null || password === '') && canInteract) {
|
2018-06-19 04:07:45 +02:00
|
|
|
const answer: inquirer.Answers = await inquirer.createPromptModule({ output: process.stderr })({
|
2018-05-23 17:16:23 +02:00
|
|
|
type: 'password',
|
|
|
|
name: 'password',
|
|
|
|
message: 'Master password:',
|
2018-05-17 16:58:30 +02:00
|
|
|
});
|
2018-05-23 17:16:23 +02:00
|
|
|
password = answer.password;
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|
|
|
|
if (password == null || password === '') {
|
|
|
|
return Response.badRequest('Master password is required.');
|
|
|
|
}
|
|
|
|
|
2021-06-30 20:27:47 +02:00
|
|
|
if (await this.cryptoService.compareAndUpdateKeyHash(password, null)) {
|
2021-02-03 18:44:33 +01:00
|
|
|
let format = options.format;
|
2020-12-08 18:35:37 +01:00
|
|
|
if (format !== 'encrypted_json' && format !== 'json') {
|
|
|
|
format = 'csv';
|
|
|
|
}
|
2021-02-03 18:44:33 +01:00
|
|
|
if (options.organizationid != null && !Utils.isGuid(options.organizationid)) {
|
|
|
|
return Response.error('`' + options.organizationid + '` is not a GUID.');
|
2019-10-07 17:01:30 +02:00
|
|
|
}
|
2020-08-25 20:24:43 +02:00
|
|
|
let exportContent: string = null;
|
2019-10-07 17:01:30 +02:00
|
|
|
try {
|
2021-02-03 18:44:33 +01:00
|
|
|
exportContent = options.organizationid != null ?
|
|
|
|
await this.exportService.getOrganizationExport(options.organizationid, format) :
|
2019-10-07 17:01:30 +02:00
|
|
|
await this.exportService.getExport(format);
|
|
|
|
} catch (e) {
|
|
|
|
return Response.error(e);
|
|
|
|
}
|
2021-02-03 18:44:33 +01:00
|
|
|
return await this.saveFile(exportContent, options, format);
|
2018-05-17 16:58:30 +02:00
|
|
|
} else {
|
|
|
|
return Response.error('Invalid master password.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2018-05-17 16:58:30 +02:00
|
|
|
}
|