1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-30 08:10:34 +01:00
bitwarden-browser/src/commands/export.command.ts

61 lines
2.5 KiB
TypeScript
Raw Normal View History

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
import { CryptoService } from 'jslib/abstractions/crypto.service';
import { ExportService } from 'jslib/abstractions/export.service';
2019-03-16 03:34:59 +01:00
import { Response } from 'jslib/cli/models/response';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
2018-05-17 16:58:30 +02:00
import { CliUtils } from '../utils';
2019-10-07 17:01:30 +02:00
import { Utils } from 'jslib/misc/utils';
2018-05-17 16:58:30 +02:00
export class ExportCommand {
2018-08-14 21:13:40 +02:00
constructor(private cryptoService: CryptoService, private exportService: ExportService) { }
2018-05-17 16:58:30 +02:00
async run(password: string, cmd: program.Command): Promise<Response> {
const canInteract = process.env.BW_NOINTERACTION !== 'true';
2019-10-21 22:04:51 +02:00
if ((password == null || password === '') && canInteract) {
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.');
}
2018-08-14 21:13:40 +02:00
const keyHash = await this.cryptoService.hashPassword(password, null);
2018-05-17 16:58:30 +02:00
const storedKeyHash = await this.cryptoService.getKeyHash();
if (storedKeyHash != null && keyHash != null && storedKeyHash === keyHash) {
2018-12-17 16:36:40 +01:00
const format = cmd.format !== 'json' ? 'csv' : 'json';
2019-10-07 17:01:30 +02:00
if (cmd.organizationid != null && !Utils.isGuid(cmd.organizationid)) {
return Response.error('`' + cmd.organizationid + '` is not a GUID.');
}
let exportContent: string = null;
2019-10-07 17:01:30 +02:00
try {
exportContent = cmd.organizationid != null ?
2019-10-07 17:01:30 +02:00
await this.exportService.getOrganizationExport(cmd.organizationid, format) :
await this.exportService.getExport(format);
} catch (e) {
return Response.error(e);
}
return await this.saveFile(exportContent, cmd, format);
2018-05-17 16:58:30 +02:00
} else {
return Response.error('Invalid master password.');
}
}
async saveFile(exportContent: string, cmd: program.Command, format: string): Promise<Response> {
2018-05-17 19:28:22 +02:00
try {
const fileName = this.exportService.getFileName(cmd.organizationid != null ? 'org' : null, format);
return await CliUtils.saveResultToFile(exportContent, cmd.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
}
}
}