mirror of
https://github.com/bitwarden/browser
synced 2025-01-20 16:31:15 +01:00
add export command
This commit is contained in:
parent
b2f8858c26
commit
36421c9144
2
jslib
2
jslib
@ -1 +1 @@
|
||||
Subproject commit 1fdb694fae15cc1b46c4fb55ed6e37be819d859c
|
||||
Subproject commit ba10d0704212f2bc8fabf0d3d6ebb552fd183401
|
11
package-lock.json
generated
11
package-lock.json
generated
@ -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",
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
80
src/commands/export.command.ts
Normal file
80
src/commands/export.command.ts
Normal file
@ -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<Response> {
|
||||
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<Response> {
|
||||
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<Response>((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));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
@ -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>', '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.')
|
||||
|
@ -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<any>;
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
15
src/utils.ts
15
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<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let input: string = '';
|
||||
|
Loading…
Reference in New Issue
Block a user