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

251 lines
9.4 KiB
TypeScript
Raw Normal View History

2018-05-15 16:50:06 +02:00
import * as chk from 'chalk';
2018-05-14 17:15:54 +02:00
import * as program from 'commander';
2018-05-15 05:16:59 +02:00
import { Main } from './bw';
2018-05-15 05:40:11 +02:00
import { CreateCommand } from './commands/create.command';
2018-05-14 22:25:14 +02:00
import { DeleteCommand } from './commands/delete.command';
2018-05-15 17:30:56 +02:00
import { EditCommand } from './commands/edit.command';
2018-05-15 05:40:11 +02:00
import { EncodeCommand } from './commands/encode.command';
2018-05-14 22:25:14 +02:00
import { GetCommand } from './commands/get.command';
import { ListCommand } from './commands/list.command';
2018-05-14 17:15:54 +02:00
import { LoginCommand } from './commands/login.command';
2018-05-16 05:26:36 +02:00
import { LogoutCommand } from './commands/logout.command';
2018-05-14 17:15:54 +02:00
import { SyncCommand } from './commands/sync.command';
2018-05-16 04:47:52 +02:00
import { Response } from './models/response';
2018-05-14 20:54:19 +02:00
import { ListResponse } from './models/response/listResponse';
2018-05-16 04:47:52 +02:00
import { MessageResponse } from './models/response/messageResponse';
2018-05-14 20:54:19 +02:00
import { StringResponse } from './models/response/stringResponse';
2018-05-14 22:25:14 +02:00
import { TemplateResponse } from './models/response/templateResponse';
2018-05-14 20:54:19 +02:00
2018-05-15 16:50:06 +02:00
const chalk = chk.default;
2018-05-14 17:15:54 +02:00
export class Program {
constructor(private main: Main) { }
run() {
program
2018-05-15 18:44:10 +02:00
.version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version')
2018-05-16 04:10:24 +02:00
.option('--pretty', 'Format stdout.')
2018-05-16 04:47:52 +02:00
.option('--raw', 'Raw output instead a descriptive message.')
2018-05-16 04:10:24 +02:00
.option('--session <session>', 'Session key.');
2018-05-16 04:47:52 +02:00
program.on('option:pretty', () => {
process.env.BW_PRETTY = 'true';
});
program.on('option:raw', () => {
process.env.BW_RAW = 'true';
});
2018-05-16 04:10:24 +02:00
program.on('option:session', (key) => {
process.env.BW_SESSION = key;
});
2018-05-14 17:15:54 +02:00
program
2018-05-15 23:17:47 +02:00
.command('login [email] [password]')
2018-05-14 17:15:54 +02:00
.description('Log into a Bitwarden user account.')
2018-05-15 23:17:47 +02:00
.option('-m, --method <method>', 'Two-step login method.')
.option('-c, --code <code>', 'Two-step login code.')
2018-05-14 17:15:54 +02:00
.action(async (email: string, password: string, cmd: program.Command) => {
2018-05-16 05:26:36 +02:00
await this.exitIfAuthed();
2018-05-16 03:11:58 +02:00
const command = new LoginCommand(this.main.authService, this.main.apiService,
this.main.cryptoFunctionService);
2018-05-14 20:54:19 +02:00
const response = await command.run(email, password, cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
program
.command('logout')
.description('Log out of the current Bitwarden user account.')
2018-05-16 04:10:24 +02:00
.action(async (cmd) => {
await this.exitIfNotAuthed();
2018-05-16 05:48:50 +02:00
const command = new LogoutCommand(this.main.authService, async () => await this.main.logout());
2018-05-16 05:26:36 +02:00
const response = await command.run(cmd);
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
2018-05-15 20:21:42 +02:00
program
.command('lock')
.description('Lock the vault and destroy the current session token.')
2018-05-16 04:47:52 +02:00
.action(async (cmd) => {
await this.exitIfNotAuthed();
2018-05-15 20:21:42 +02:00
// TODO
});
program
2018-05-15 23:17:47 +02:00
.command('unlock [password]')
2018-05-15 20:21:42 +02:00
.description('Unlock the vault and obtain a new session token.')
2018-05-16 04:47:52 +02:00
.action(async (cmd) => {
await this.exitIfNotAuthed();
2018-05-15 20:21:42 +02:00
// TODO
});
2018-05-14 17:15:54 +02:00
program
.command('sync')
.description('Sync user\'s vault from server.')
.option('-f, --force', 'Force a full sync.')
.action(async (cmd) => {
2018-05-16 04:10:24 +02:00
await this.exitIfLocked();
2018-05-14 17:15:54 +02:00
const command = new SyncCommand(this.main.syncService);
2018-05-14 20:54:19 +02:00
const response = await command.run(cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
program
.command('list <object>')
.description('List objects.')
2018-05-14 19:37:52 +02:00
.action(async (object, cmd) => {
2018-05-16 04:10:24 +02:00
await this.exitIfLocked();
2018-05-14 19:37:52 +02:00
const command = new ListCommand(this.main.cipherService, this.main.folderService,
this.main.collectionService);
2018-05-14 20:54:19 +02:00
const response = await command.run(object, cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
program
2018-05-16 03:22:39 +02:00
.command('get <object> [id]')
2018-05-14 17:15:54 +02:00
.description('Get an object.')
2018-05-16 06:06:58 +02:00
.option('--uppercase', 'Include uppercase characters.')
.option('--lowercase', 'Include lowercase characters.')
.option('--number', 'Include numeric characters.')
.option('--special', 'Include special characters.')
.option('--length <length>', 'Password length.')
2018-05-14 19:37:52 +02:00
.action(async (object, id, cmd) => {
2018-05-16 04:10:24 +02:00
await this.exitIfLocked();
2018-05-14 19:37:52 +02:00
const command = new GetCommand(this.main.cipherService, this.main.folderService,
2018-05-16 06:06:58 +02:00
this.main.collectionService, this.main.totpService, this.main.syncService,
this.main.passwordGenerationService);
2018-05-14 20:54:19 +02:00
const response = await command.run(object, id, cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
2018-05-15 03:19:49 +02:00
program
.command('create <object> <encodedData>')
.description('Create an object.')
.action(async (object, encodedData, cmd) => {
2018-05-16 04:10:24 +02:00
await this.exitIfLocked();
2018-05-15 03:19:49 +02:00
const command = new CreateCommand(this.main.cipherService, this.main.folderService);
const response = await command.run(object, encodedData, cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-15 03:19:49 +02:00
});
2018-05-14 17:15:54 +02:00
program
2018-05-15 17:30:56 +02:00
.command('edit <object> <id> <encodedData>')
2018-05-14 17:15:54 +02:00
.description('Edit an object.')
2018-05-15 17:30:56 +02:00
.action(async (object, id, encodedData, cmd) => {
2018-05-16 04:10:24 +02:00
await this.exitIfLocked();
2018-05-15 17:30:56 +02:00
const command = new EditCommand(this.main.cipherService, this.main.folderService);
const response = await command.run(object, id, encodedData, cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
program
.command('delete <object> <id>')
.description('Delete an object.')
2018-05-14 21:08:48 +02:00
.action(async (object, id, cmd) => {
2018-05-16 04:10:24 +02:00
await this.exitIfLocked();
2018-05-14 21:08:48 +02:00
const command = new DeleteCommand(this.main.cipherService, this.main.folderService);
const response = await command.run(object, id, cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
2018-05-14 23:13:57 +02:00
program
.command('encode')
.description('Base64 encode stdin.')
.action(async (object, id, cmd) => {
const command = new EncodeCommand();
const response = await command.run(cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 23:13:57 +02:00
});
2018-05-14 17:15:54 +02:00
program
.parse(process.argv);
}
2018-05-14 20:54:19 +02:00
2018-05-15 18:44:10 +02:00
private processResponse(response: Response, cmd: program.Command) {
2018-05-15 20:21:42 +02:00
if (!response.success) {
2018-05-15 16:50:06 +02:00
process.stdout.write(chalk.redBright(response.message));
2018-05-14 20:54:19 +02:00
process.exit(1);
2018-05-15 20:21:42 +02:00
return;
}
if (response.data != null) {
2018-05-16 14:37:05 +02:00
let out: string = null;
2018-05-15 20:21:42 +02:00
if (response.data.object === 'string') {
2018-05-16 03:22:39 +02:00
const data = (response.data as StringResponse).data;
if (data != null) {
2018-05-16 14:37:05 +02:00
out = data;
2018-05-16 03:22:39 +02:00
}
2018-05-15 20:21:42 +02:00
} else if (response.data.object === 'list') {
2018-05-16 14:37:05 +02:00
out = this.getJson((response.data as ListResponse).data, cmd);
2018-05-15 20:21:42 +02:00
} else if (response.data.object === 'template') {
2018-05-16 14:37:05 +02:00
out = this.getJson((response.data as TemplateResponse).template, cmd);
2018-05-16 04:47:52 +02:00
} else if (response.data.object === 'message') {
2018-05-16 14:37:05 +02:00
out = this.getMessage(response);
2018-05-15 20:21:42 +02:00
} else {
2018-05-16 14:37:05 +02:00
out = this.getJson(response.data, cmd);
}
if (out != null) {
process.stdout.write(out);
2018-05-15 20:21:42 +02:00
}
2018-05-14 20:54:19 +02:00
}
2018-05-15 20:21:42 +02:00
process.exit();
2018-05-14 20:54:19 +02:00
}
2018-05-15 18:44:10 +02:00
2018-05-16 14:37:05 +02:00
private getJson(obj: any, cmd: program.Command): string {
2018-05-16 04:47:52 +02:00
if (process.env.BW_PRETTY === 'true') {
2018-05-16 14:37:05 +02:00
return JSON.stringify(obj, null, ' ');
2018-05-15 18:44:10 +02:00
} else {
2018-05-16 14:37:05 +02:00
return JSON.stringify(obj);
2018-05-15 18:44:10 +02:00
}
}
2018-05-16 14:37:05 +02:00
private getMessage(response: Response) {
2018-05-16 04:47:52 +02:00
const message = (response.data as MessageResponse);
if (process.env.BW_RAW === 'true' && message.raw != null) {
2018-05-16 14:37:05 +02:00
return message.raw;
2018-05-15 18:44:10 +02:00
}
2018-05-16 14:37:05 +02:00
let out: string = '';
2018-05-16 04:47:52 +02:00
if (message.title != null) {
2018-05-16 14:37:05 +02:00
out = chalk.greenBright(message.title);
2018-05-16 04:47:52 +02:00
}
if (message.message != null) {
if (message.title != null) {
2018-05-16 14:37:05 +02:00
out += '\n';
2018-05-16 04:47:52 +02:00
}
2018-05-16 14:37:05 +02:00
out += message.message;
2018-05-15 18:44:10 +02:00
}
2018-05-16 14:37:05 +02:00
return out.trim() === '' ? null : out;
2018-05-15 18:44:10 +02:00
}
2018-05-16 04:10:24 +02:00
private async exitIfLocked() {
await this.exitIfNotAuthed();
const key = await this.main.cryptoService.getKey();
if (key == null) {
process.stdout.write(chalk.redBright('Vault is locked.'));
process.exit(1);
}
}
private async exitIfAuthed() {
const authed = await this.main.userService.isAuthenticated();
if (authed) {
const email = await this.main.userService.getEmail();
process.stdout.write(chalk.redBright('You are already logged in as ' + email + '.'));
process.exit(1);
}
}
private async exitIfNotAuthed() {
const authed = await this.main.userService.isAuthenticated();
if (!authed) {
process.stdout.write(chalk.redBright('You are not logged in.'));
process.exit(1);
}
}
2018-05-14 17:15:54 +02:00
}