mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 15:53:27 +00:00
add export command
This commit is contained in:
@@ -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 = '';
|
||||
|
||||
Reference in New Issue
Block a user