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

57 lines
2.2 KiB
TypeScript
Raw Normal View History

2018-05-17 16:58:30 +02:00
import * as program from 'commander';
import * as fs from 'fs';
2018-05-23 17:16:23 +02:00
import * as inquirer from 'inquirer';
2018-05-17 16:58:30 +02:00
import * as path from 'path';
import { CryptoService } from 'jslib/abstractions/crypto.service';
import { ExportService } from 'jslib/abstractions/export.service';
import { UserService } from 'jslib/abstractions/user.service';
import { Response } from '../models/response';
import { MessageResponse } from '../models/response/messageResponse';
import { Utils } from 'jslib/misc/utils';
import { CliUtils } from '../utils';
export class ExportCommand {
constructor(private cryptoService: CryptoService, private userService: UserService,
private exportService: ExportService) { }
async run(password: string, cmd: program.Command): Promise<Response> {
if (password == null || password === '') {
2018-05-23 17:16:23 +02:00
const answer = await inquirer.prompt<any>({
type: 'password',
name: 'password',
message: 'Master password:',
2018-05-17 16:58:30 +02:00
mask: '*',
});
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.');
}
const email = await this.userService.getEmail();
const key = await this.cryptoService.makeKey(password, email);
const keyHash = await this.cryptoService.hashPassword(password, key);
const storedKeyHash = await this.cryptoService.getKeyHash();
if (storedKeyHash != null && keyHash != null && storedKeyHash === keyHash) {
const csv = await this.exportService.getCsv();
return await this.saveFile(csv, cmd);
} else {
return Response.error('Invalid master password.');
}
}
async saveFile(csv: string, cmd: program.Command): Promise<Response> {
2018-05-17 19:28:22 +02:00
try {
const filePath = await CliUtils.saveFile(csv, cmd.output, this.exportService.getFileName());
const res = new MessageResponse('Saved ' + filePath, null);
res.raw = filePath;
return Response.success(res);
} catch (e) {
return Response.error(e.toString());
2018-05-17 16:58:30 +02:00
}
}
}