From 36421c9144e44db5119f0895724cfcd617bc3da5 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 17 May 2018 10:58:30 -0400 Subject: [PATCH] add export command --- jslib | 2 +- package-lock.json | 11 ++++ package.json | 2 + src/bw.ts | 3 ++ src/commands/export.command.ts | 80 ++++++++++++++++++++++++++++ src/program.ts | 22 ++++++++ src/services/lowdbStorage.service.ts | 16 ++---- src/utils.ts | 15 ++++++ 8 files changed, 137 insertions(+), 14 deletions(-) create mode 100644 src/commands/export.command.ts diff --git a/jslib b/jslib index 1fdb694fae..ba10d07042 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit 1fdb694fae15cc1b46c4fb55ed6e37be819d859c +Subproject commit ba10d0704212f2bc8fabf0d3d6ebb552fd183401 diff --git a/package-lock.json b/package-lock.json index 5151b65c23..c5a8e714ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -46,6 +46,12 @@ "integrity": "sha512-zK8v6Vu+8LiXdj8FvA4/WHvhfDwzwUa4rR+JKZwUpSQzBvSFgN5UmHjSiSeg2kci19refUVnxJF7uc+d6wtXBw==", "dev": true }, + "@types/papaparse": { + "version": "4.1.31", + "resolved": "https://registry.npmjs.org/@types/papaparse/-/papaparse-4.1.31.tgz", + "integrity": "sha512-8+d1hk3GgF+NJ6mMZZ5zKimqIOc+8OTzpLw4RQ8wnS1NkJh/dMH3NEhSud4Ituq2SGXJjOG6wIczCBAKsSsBdQ==", + "dev": true + }, "@types/readline-sync": { "version": "1.4.3", "resolved": "https://registry.npmjs.org/@types/readline-sync/-/readline-sync-1.4.3.tgz", @@ -3419,6 +3425,11 @@ "integrity": "sha512-lQe48YPsMJAig+yngZ87Lus+NF+3mtu7DVOBu6b/gHO1YpKwIj5AWjZ/TOS7i46HD/UixzWb1zeWDZfGZ3iYcg==", "dev": true }, + "papaparse": { + "version": "4.3.5", + "resolved": "https://registry.npmjs.org/papaparse/-/papaparse-4.3.5.tgz", + "integrity": "sha1-ts31yub+nsYDsb5m8RSmOsZFoDY=" + }, "parallel-transform": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/parallel-transform/-/parallel-transform-1.1.0.tgz", diff --git a/package.json b/package.json index a47ac9dd34..9557a2cbe2 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "@types/lunr": "^2.1.5", "@types/node": "^10.0.8", "@types/node-forge": "^0.7.1", + "@types/papaparse": "4.1.31", "@types/readline-sync": "^1.4.3", "clean-webpack-plugin": "^0.1.17", "copy-webpack-plugin": "^4.2.0", @@ -66,6 +67,7 @@ "lowdb": "1.0.0", "node-fetch": "2.1.2", "node-forge": "0.7.1", + "papaparse": "4.3.5", "readline-sync": "1.4.9" } } diff --git a/src/bw.ts b/src/bw.ts index 4f47c727fa..886b6e6a74 100644 --- a/src/bw.ts +++ b/src/bw.ts @@ -14,6 +14,7 @@ import { ConstantsService } from 'jslib/services/constants.service'; import { ContainerService } from 'jslib/services/container.service'; import { CryptoService } from 'jslib/services/crypto.service'; import { EnvironmentService } from 'jslib/services/environment.service'; +import { ExportService } from 'jslib/services/export.service'; import { FolderService } from 'jslib/services/folder.service'; import { LockService } from 'jslib/services/lock.service'; import { NodeApiService } from 'jslib/services/nodeApi.service'; @@ -50,6 +51,7 @@ export class Main { totpService: TotpService; containerService: ContainerService; auditService: AuditService; + exportService: ExportService; cryptoFunctionService: NodeCryptoFunctionService; authService: AuthService; program: Program; @@ -84,6 +86,7 @@ export class Main { this.storageService, this.messagingService, async (expired: boolean) => await this.logout()); this.passwordGenerationService = new PasswordGenerationService(this.cryptoService, this.storageService); this.totpService = new TotpService(this.storageService, this.cryptoFunctionService); + this.exportService = new ExportService(this.folderService, this.cipherService); this.authService = new AuthService(this.cryptoService, this.apiService, this.userService, this.tokenService, this.appIdService, this.i18nService, this.platformUtilsService, this.messagingService, true); this.program = new Program(this); diff --git a/src/commands/export.command.ts b/src/commands/export.command.ts new file mode 100644 index 0000000000..7075e21e69 --- /dev/null +++ b/src/commands/export.command.ts @@ -0,0 +1,80 @@ +import * as program from 'commander'; +import * as fs from 'fs'; +import * as path from 'path'; +import * as readline from 'readline-sync'; + +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 { + if (password == null || password === '') { + password = readline.question('Master password: ', { + hideEchoBack: true, + mask: '*', + }); + } + 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 { + let p: string = null; + let mkdir = false; + if (cmd.output != null && cmd.output !== '') { + const osOutput = path.join(cmd.output); + if (osOutput.indexOf(path.sep) === -1) { + p = path.join(process.cwd(), osOutput); + } else { + mkdir = true; + if (osOutput.endsWith(path.sep)) { + p = path.join(osOutput, this.exportService.getFileName()); + } else { + p = osOutput; + } + } + } else { + p = path.join(process.cwd(), this.exportService.getFileName()); + } + + p = path.resolve(p); + if (mkdir) { + const dir = p.substring(0, p.lastIndexOf(path.sep)); + if (!fs.existsSync(dir)) { + CliUtils.mkdirpSync(dir, 755); + } + } + + return new Promise((resolve, reject) => { + fs.writeFile(p, csv, (err) => { + if (err != null) { + reject(Response.error('Cannot save file to ' + p)); + } + const res = new MessageResponse('Saved ' + p + '', null); + resolve(Response.success(res)); + }); + }); + } +} diff --git a/src/program.ts b/src/program.ts index 7f0b1bc1c8..6659c3de1b 100644 --- a/src/program.ts +++ b/src/program.ts @@ -8,6 +8,7 @@ import { CreateCommand } from './commands/create.command'; import { DeleteCommand } from './commands/delete.command'; import { EditCommand } from './commands/edit.command'; import { EncodeCommand } from './commands/encode.command'; +import { ExportCommand } from './commands/export.command'; import { GenerateCommand } from './commands/generate.command'; import { GetCommand } from './commands/get.command'; import { ListCommand } from './commands/list.command'; @@ -339,6 +340,27 @@ export class Program { this.processResponse(response); }); + program + .command('export [password]') + .description('Export vault data to a CSV.') + .option('--output ', 'Output directory or filename.') + .on('--help', () => { + writeLn('\n Examples:'); + writeLn(''); + writeLn(' bw export'); + writeLn(' bw export myPassword321'); + writeLn(' bw export --output ./exp/bw.csv'); + writeLn(' bw export myPassword321 --output bw.csv'); + writeLn(''); + }) + .action(async (password, cmd) => { + await this.exitIfLocked(); + const command = new ExportCommand(this.main.cryptoService, this.main.userService, + this.main.exportService); + const response = await command.run(password, cmd); + this.processResponse(response); + }); + program .command('generate') .description('Generate a password.') diff --git a/src/services/lowdbStorage.service.ts b/src/services/lowdbStorage.service.ts index 9b9b0e569a..a11e5c8e28 100644 --- a/src/services/lowdbStorage.service.ts +++ b/src/services/lowdbStorage.service.ts @@ -6,6 +6,8 @@ import * as path from 'path'; import { StorageService } from 'jslib/abstractions/storage.service'; import { Utils } from 'jslib/misc/utils'; +import { CliUtils } from '../utils'; + export class LowdbStorageService implements StorageService { private db: lowdb.LowdbSync; @@ -19,7 +21,7 @@ export class LowdbStorageService implements StorageService { p = path.join(process.env.HOME, '.config', appDirName); } if (!fs.existsSync(p)) { - this.mkdirpSync(p, 755); + CliUtils.mkdirpSync(p, 755); } p = path.join(p, 'data.json'); @@ -41,16 +43,4 @@ export class LowdbStorageService implements StorageService { this.db.unset(key).write(); return Promise.resolve(); } - - private mkdirpSync(targetDir: string, mode = 755, relative = false) { - const initialDir = path.isAbsolute(targetDir) ? path.sep : ''; - const baseDir = relative ? __dirname : '.'; - targetDir.split(path.sep).reduce((parentDir, childDir) => { - const dir = path.resolve(baseDir, parentDir, childDir); - if (!fs.existsSync(dir)) { - fs.mkdirSync(dir, mode); - } - return dir; - }, initialDir); - } } diff --git a/src/utils.ts b/src/utils.ts index 63cfd3aac7..224b8eb9e3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,8 +1,23 @@ +import * as fs from 'fs'; +import * as path from 'path'; + import { CipherView } from 'jslib/models/view/cipherView'; import { CollectionView } from 'jslib/models/view/collectionView'; import { FolderView } from 'jslib/models/view/folderView'; export class CliUtils { + static mkdirpSync(targetDir: string, mode = 755, relative = false, relativeDir: string = null) { + const initialDir = path.isAbsolute(targetDir) ? path.sep : ''; + const baseDir = relative ? (relativeDir != null ? relativeDir : __dirname) : '.'; + targetDir.split(path.sep).reduce((parentDir, childDir) => { + const dir = path.resolve(baseDir, parentDir, childDir); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, mode); + } + return dir; + }, initialDir); + } + static readStdin(): Promise { return new Promise((resolve, reject) => { let input: string = '';