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

766 lines
35 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-17 06:07:44 +02:00
import { ConfigCommand } from './commands/config.command';
2019-10-07 16:42:02 +02:00
import { ConfirmCommand } from './commands/confirm.command';
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-17 16:58:30 +02:00
import { ExportCommand } from './commands/export.command';
import { GenerateCommand } from './commands/generate.command';
2018-05-14 22:25:14 +02:00
import { GetCommand } from './commands/get.command';
import { ImportCommand } from './commands/import.command';
2018-05-14 22:25:14 +02:00
import { ListCommand } from './commands/list.command';
2018-05-16 16:25:25 +02:00
import { LockCommand } from './commands/lock.command';
2018-05-14 17:15:54 +02:00
import { LoginCommand } from './commands/login.command';
import { RestoreCommand } from './commands/restore.command';
2018-10-24 04:56:15 +02:00
import { ShareCommand } from './commands/share.command';
import { StatusCommand } from './commands/status.command';
2018-05-14 17:15:54 +02:00
import { SyncCommand } from './commands/sync.command';
2018-05-16 16:25:25 +02:00
import { UnlockCommand } from './commands/unlock.command';
2018-05-14 17:15:54 +02:00
import { CompletionCommand } from './commands/completion.command';
2019-03-18 15:33:43 +01:00
import { LogoutCommand } from 'jslib/cli/commands/logout.command';
2019-03-16 03:34:59 +01:00
import { UpdateCommand } from 'jslib/cli/commands/update.command';
import { Response } from 'jslib/cli/models/response';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
2018-05-14 22:25:14 +02:00
import { TemplateResponse } from './models/response/templateResponse';
import { CliUtils } from './utils';
2018-05-14 20:54:19 +02:00
2019-03-16 16:27:27 +01:00
import { BaseProgram } from 'jslib/cli/baseProgram';
2018-05-15 16:50:06 +02:00
const chalk = chk.default;
const writeLn = CliUtils.writeLn;
2018-05-16 20:47:03 +02:00
2019-03-16 16:27:27 +01:00
export class Program extends BaseProgram {
constructor(private main: Main) {
super(main.userService, writeLn);
}
2018-05-14 17:15:54 +02:00
run() {
program
2018-05-18 17:24:49 +02:00
.option('--pretty', 'Format output. JSON is tabbed with two spaces.')
.option('--raw', 'Return raw output instead of a descriptive message.')
2018-05-18 17:34:14 +02:00
.option('--response', 'Return a JSON formatted version of response output.')
.option('--quiet', 'Don\'t return anything to stdout.')
2019-10-21 22:04:51 +02:00
.option('--nointeraction', 'Do not prompt for interactive user input.')
2018-05-17 22:40:43 +02:00
.option('--session <session>', 'Pass session key instead of reading from env.')
2018-05-17 05:34:58 +02:00
.version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version');
2018-05-16 04:10:24 +02:00
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 16:29:57 +02:00
program.on('option:quiet', () => {
process.env.BW_QUIET = 'true';
});
2018-05-18 17:34:14 +02:00
program.on('option:response', () => {
process.env.BW_RESPONSE = 'true';
});
2019-10-21 22:04:51 +02:00
program.on('option:nointeraction', () => {
process.env.BW_NOINTERACTION = '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
2018-05-16 20:47:03 +02:00
program.on('command:*', () => {
2019-06-05 03:03:26 +02:00
writeLn(chalk.redBright('Invalid command: ' + program.args.join(' ')), false, true);
writeLn('See --help for a list of available commands.', true, true);
2018-06-20 20:42:21 +02:00
process.exitCode = 1;
2018-05-16 20:47:03 +02:00
});
program.on('--help', () => {
writeLn('\n Examples:');
writeLn('');
2018-05-17 03:38:45 +02:00
writeLn(' bw login');
writeLn(' bw lock');
writeLn(' bw unlock myPassword321');
2018-05-17 05:32:14 +02:00
writeLn(' bw list --help');
2018-05-17 03:38:45 +02:00
writeLn(' bw list items --search google');
writeLn(' bw get item 99ee88d2-6046-4ea7-92c2-acac464b1412');
writeLn(' bw get password google.com');
writeLn(' echo \'{"name":"My Folder"}\' | bw encode');
writeLn(' bw create folder eyJuYW1lIjoiTXkgRm9sZGVyIn0K');
writeLn(' bw edit folder c7c7b60b-9c61-40f2-8ccd-36c49595ed72 eyJuYW1lIjoiTXkgRm9sZGVyMiJ9Cg==');
2018-05-17 04:21:29 +02:00
writeLn(' bw delete item 99ee88d2-6046-4ea7-92c2-acac464b1412');
2018-05-18 17:14:47 +02:00
writeLn(' bw generate -lusn --length 18');
writeLn(' bw config server https://bitwarden.example.com');
2018-05-18 17:14:47 +02:00
writeLn('', true);
2018-05-16 20:47:03 +02:00
});
2018-05-14 17:15:54 +02:00
program
2018-05-15 23:17:47 +02:00
.command('login [email] [password]')
2018-05-16 20:47:03 +02:00
.description('Log into a user account.')
.option('--method <method>', 'Two-step login method.')
.option('--code <code>', 'Two-step login code.')
.option('--sso', 'Log in with Single-Sign On.')
.option('--apiKey', 'Log in with an Api Key.')
2019-01-15 17:46:58 +01:00
.option('--check', 'Check login status.', async () => {
const authed = await this.main.userService.isAuthenticated();
if (authed) {
2019-01-15 17:55:12 +01:00
const res = new MessageResponse('You are logged in!', null);
2019-01-15 17:46:58 +01:00
this.processResponse(Response.success(res), true);
}
this.processResponse(Response.error('You are not logged in.'), true);
})
2018-05-17 05:23:12 +02:00
.on('--help', () => {
writeLn('\n Notes:');
writeLn('');
writeLn(' See docs for valid `method` enum values.');
writeLn('');
2018-05-18 17:24:49 +02:00
writeLn(' Pass `--raw` option to only return the session key.');
2018-05-17 05:23:12 +02:00
writeLn('');
writeLn(' Examples:');
writeLn('');
writeLn(' bw login');
writeLn(' bw login john@example.com myPassword321 --raw');
2018-05-18 17:14:47 +02:00
writeLn(' bw login john@example.com myPassword321 --method 1 --code 249213');
writeLn(' bw login --sso');
2018-05-18 17:14:47 +02:00
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
2018-05-14 17:15:54 +02:00
.action(async (email: string, password: string, cmd: program.Command) => {
2019-04-19 23:38:02 +02:00
if (!cmd.check) {
await this.exitIfAuthed();
const command = new LoginCommand(this.main.authService, this.main.apiService,
this.main.cryptoFunctionService, this.main.syncService, this.main.i18nService,
2020-09-23 17:59:30 +02:00
this.main.environmentService, this.main.passwordGenerationService,
this.main.platformUtilsService);
2019-04-19 23:38:02 +02:00
const response = await command.run(email, password, cmd);
this.processResponse(response);
}
2018-05-14 17:15:54 +02:00
});
program
.command('logout')
2018-05-16 20:47:03 +02:00
.description('Log out of the current user account.')
2018-05-17 05:23:12 +02:00
.on('--help', () => {
writeLn('\n Examples:');
writeLn('');
2018-05-18 17:14:47 +02:00
writeLn(' bw logout');
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
2018-05-16 04:10:24 +02:00
.action(async (cmd) => {
await this.exitIfNotAuthed();
2019-03-18 15:33:43 +01:00
const command = new LogoutCommand(this.main.authService, this.main.i18nService,
async () => await this.main.logout());
2018-05-16 05:26:36 +02:00
const response = await command.run(cmd);
2018-05-16 16:25:25 +02:00
this.processResponse(response);
2018-05-14 17:15:54 +02:00
});
2018-05-15 20:21:42 +02:00
program
.command('lock')
2018-05-18 17:24:49 +02:00
.description('Lock the vault and destroy active session keys.')
2018-05-17 05:23:12 +02:00
.on('--help', () => {
writeLn('\n Examples:');
writeLn('');
2018-05-18 17:14:47 +02:00
writeLn(' bw lock');
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
2018-05-16 04:47:52 +02:00
.action(async (cmd) => {
await this.exitIfNotAuthed();
const command = new LockCommand(this.main.vaultTimeoutService);
2018-05-16 16:25:25 +02:00
const response = await command.run(cmd);
this.processResponse(response);
2018-05-15 20:21:42 +02:00
});
program
2018-05-15 23:17:47 +02:00
.command('unlock [password]')
2018-05-18 17:24:49 +02:00
.description('Unlock the vault and return a new session key.')
2018-05-17 05:23:12 +02:00
.on('--help', () => {
writeLn('\n Notes:');
writeLn('');
2018-05-17 22:40:43 +02:00
writeLn(' After unlocking, any previous session keys will no longer be valid.');
2018-05-17 05:23:12 +02:00
writeLn('');
2018-05-18 17:24:49 +02:00
writeLn(' Pass `--raw` option to only return the session key.');
2018-05-17 05:23:12 +02:00
writeLn('');
writeLn(' Examples:');
writeLn('');
writeLn(' bw unlock');
writeLn(' bw unlock myPassword321');
2018-05-18 17:14:47 +02:00
writeLn(' bw unlock myPassword321 --raw');
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
2019-04-19 23:41:15 +02:00
.option('--check', 'Check lock status.', async () => {
const locked = await this.main.vaultTimeoutService.isLocked();
2019-04-19 23:38:02 +02:00
if (!locked) {
const res = new MessageResponse('Vault is unlocked!', null);
this.processResponse(Response.success(res), true);
}
this.processResponse(Response.error('Vault is locked.'), true);
})
2018-05-16 16:25:25 +02:00
.action(async (password, cmd) => {
2019-04-19 23:38:02 +02:00
if (!cmd.check) {
await this.exitIfNotAuthed();
const command = new UnlockCommand(this.main.cryptoService, this.main.userService,
this.main.cryptoFunctionService, this.main.apiService);
2019-04-19 23:38:02 +02:00
const response = await command.run(password, cmd);
this.processResponse(response);
}
2018-05-15 20:21:42 +02:00
});
2018-05-14 17:15:54 +02:00
program
.command('sync')
2018-05-18 17:24:49 +02:00
.description('Pull the latest vault data from server.')
2018-05-14 17:15:54 +02:00
.option('-f, --force', 'Force a full sync.')
.option('--last', 'Get the last sync date.')
2018-05-17 05:23:12 +02:00
.on('--help', () => {
writeLn('\n Examples:');
writeLn('');
writeLn(' bw sync');
writeLn(' bw sync -f');
2018-05-18 17:14:47 +02:00
writeLn(' bw sync --last');
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
2018-05-14 17:15:54 +02:00
.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-16 16:25:25 +02:00
this.processResponse(response);
2018-05-14 17:15:54 +02:00
});
program
.command('list <object>')
2018-05-18 17:24:49 +02:00
.description('List an array of objects from the vault.')
.option('--search <search>', 'Perform a search on the listed objects.')
.option('--url <url>', 'Filter items of type login with a url-match search.')
.option('--folderid <folderid>', 'Filter items by folder id.')
.option('--collectionid <collectionid>', 'Filter items by collection id.')
.option('--organizationid <organizationid>', 'Filter items or collections by organization id.')
.option('--trash', 'Filter items that are deleted and in the trash.')
2018-05-17 05:23:12 +02:00
.on('--help', () => {
writeLn('\n Objects:');
writeLn('');
writeLn(' items');
writeLn(' folders');
writeLn(' collections');
2018-05-18 21:26:59 +02:00
writeLn(' organizations');
2019-10-07 15:58:19 +02:00
writeLn(' org-collections');
writeLn(' org-members');
2018-05-17 05:23:12 +02:00
writeLn('');
writeLn(' Notes:');
writeLn('');
2018-05-18 17:24:49 +02:00
writeLn(' Combining search with a filter performs a logical AND operation.');
writeLn('');
2018-05-17 05:23:12 +02:00
writeLn(' Combining multiple filters performs a logical OR operation.');
writeLn('');
writeLn(' Examples:');
writeLn('');
writeLn(' bw list items');
writeLn(' bw list items --folderid 60556c31-e649-4b5d-8daf-fc1c391a1bf2');
writeLn(' bw list items --search google --folderid 60556c31-e649-4b5d-8daf-fc1c391a1bf2');
writeLn(' bw list items --url https://google.com');
2018-05-17 05:23:12 +02:00
writeLn(' bw list items --folderid null');
2018-05-21 15:14:58 +02:00
writeLn(' bw list items --organizationid notnull');
writeLn(' bw list items --folderid 60556c31-e649-4b5d-8daf-fc1c391a1bf2 --organizationid notnull');
writeLn(' bw list items --trash');
2018-05-18 17:14:47 +02:00
writeLn(' bw list folders --search email');
2019-10-07 15:58:19 +02:00
writeLn(' bw list org-members --organizationid 60556c31-e649-4b5d-8daf-fc1c391a1bf2');
2018-05-18 17:14:47 +02:00
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
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,
2019-10-01 17:29:25 +02:00
this.main.collectionService, this.main.userService, this.main.searchService, this.main.apiService);
2018-05-14 20:54:19 +02:00
const response = await command.run(object, cmd);
2018-05-16 16:25:25 +02:00
this.processResponse(response);
2018-05-14 17:15:54 +02:00
});
program
.command('get <object> <id>')
2018-05-18 17:24:49 +02:00
.description('Get an object from the vault.')
2018-05-17 21:55:44 +02:00
.option('--itemid <itemid>', 'Attachment\'s item id.')
2018-05-17 19:28:22 +02:00
.option('--output <output>', 'Output directory or filename for attachment.')
2019-09-25 23:11:48 +02:00
.option('--organizationid <organizationid>', 'Organization id for an organization object.')
2018-05-17 05:23:12 +02:00
.on('--help', () => {
writeLn('\n Objects:');
writeLn('');
writeLn(' item');
writeLn(' username');
writeLn(' password');
writeLn(' uri');
writeLn(' totp');
2018-05-17 17:07:53 +02:00
writeLn(' exposed');
2018-05-17 21:55:44 +02:00
writeLn(' attachment');
2018-05-17 05:23:12 +02:00
writeLn(' folder');
writeLn(' collection');
2019-10-01 17:04:15 +02:00
writeLn(' org-collection');
2018-05-18 21:26:59 +02:00
writeLn(' organization');
2018-05-17 05:23:12 +02:00
writeLn(' template');
2018-11-16 16:02:22 +01:00
writeLn(' fingerprint');
2018-05-17 05:23:12 +02:00
writeLn('');
writeLn(' Id:');
writeLn('');
2018-05-17 21:55:44 +02:00
writeLn(' Search term or object\'s globally unique `id`.');
2018-05-17 05:23:12 +02:00
writeLn('');
writeLn(' If raw output is specified and no output filename or directory is given for');
writeLn(' an attachment query, the attachment content is written to stdout.');
writeLn('');
2018-05-17 05:23:12 +02:00
writeLn(' Examples:');
writeLn('');
writeLn(' bw get item 99ee88d2-6046-4ea7-92c2-acac464b1412');
writeLn(' bw get password https://google.com');
writeLn(' bw get totp google.com');
2018-05-17 17:07:53 +02:00
writeLn(' bw get exposed yahoo.com');
2018-05-17 21:55:44 +02:00
writeLn(' bw get attachment b857igwl1dzrs2 --itemid 99ee88d2-6046-4ea7-92c2-acac464b1412 ' +
'--output ./photo.jpg');
writeLn(' bw get attachment photo.jpg --itemid 99ee88d2-6046-4ea7-92c2-acac464b1412 --raw');
2018-05-17 05:23:12 +02:00
writeLn(' bw get folder email');
2018-05-18 17:14:47 +02:00
writeLn(' bw get template folder');
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
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-17 19:28:22 +02:00
this.main.collectionService, this.main.totpService, this.main.auditService,
2018-11-16 16:02:22 +01:00
this.main.cryptoService, this.main.userService, this.main.searchService,
this.main.apiService);
2018-05-14 20:54:19 +02:00
const response = await command.run(object, id, cmd);
2018-05-16 16:25:25 +02:00
this.processResponse(response);
2018-05-14 17:15:54 +02:00
});
2018-05-15 03:19:49 +02:00
program
.command('create <object> [encodedJson]')
2018-05-17 21:33:36 +02:00
.option('--file <file>', 'Path to file for attachment.')
.option('--itemid <itemid>', 'Attachment\'s item id.')
2019-10-01 17:16:24 +02:00
.option('--organizationid <organizationid>', 'Organization id for an organization object.')
2018-05-18 17:24:49 +02:00
.description('Create an object in the vault.')
2018-05-17 05:23:12 +02:00
.on('--help', () => {
writeLn('\n Objects:');
writeLn('');
writeLn(' item');
2018-05-17 21:33:36 +02:00
writeLn(' attachment');
2018-05-17 05:23:12 +02:00
writeLn(' folder');
2019-10-01 17:04:15 +02:00
writeLn(' org-collection');
2018-05-17 05:23:12 +02:00
writeLn('');
writeLn(' Notes:');
writeLn('');
2018-05-18 17:24:49 +02:00
writeLn(' `encodedJson` can also be piped into stdin.');
2018-05-17 05:23:12 +02:00
writeLn('');
writeLn(' Examples:');
writeLn('');
writeLn(' bw create folder eyJuYW1lIjoiTXkgRm9sZGVyIn0K');
writeLn(' echo \'eyJuYW1lIjoiTXkgRm9sZGVyIn0K\' | bw create folder');
writeLn(' bw create attachment --file ./myfile.csv ' +
2018-05-18 17:14:47 +02:00
'--itemid 16b15b89-65b3-4639-ad2a-95052a6d8f66');
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
.action(async (object, encodedJson, cmd) => {
2018-05-16 04:10:24 +02:00
await this.exitIfLocked();
2018-05-18 16:58:30 +02:00
const command = new CreateCommand(this.main.cipherService, this.main.folderService,
2019-09-25 22:08:56 +02:00
this.main.userService, this.main.cryptoService, this.main.apiService);
const response = await command.run(object, encodedJson, cmd);
2018-05-16 16:25:25 +02:00
this.processResponse(response);
2018-05-15 03:19:49 +02:00
});
2018-05-14 17:15:54 +02:00
program
.command('edit <object> <id> [encodedJson]')
2019-10-01 17:16:24 +02:00
.option('--organizationid <organizationid>', 'Organization id for an organization object.')
2018-05-18 17:24:49 +02:00
.description('Edit an object from the vault.')
2018-05-17 05:23:12 +02:00
.on('--help', () => {
writeLn('\n Objects:');
writeLn('');
writeLn(' item');
2018-10-23 23:31:59 +02:00
writeLn(' item-collections');
2018-05-17 05:23:12 +02:00
writeLn(' folder');
2019-10-01 17:16:24 +02:00
writeLn(' org-collection');
2018-05-17 05:23:12 +02:00
writeLn('');
writeLn(' Id:');
writeLn('');
2018-05-17 22:21:12 +02:00
writeLn(' Object\'s globally unique `id`.');
2018-05-17 05:23:12 +02:00
writeLn('');
writeLn(' Notes:');
writeLn('');
2018-05-18 17:24:49 +02:00
writeLn(' `encodedJson` can also be piped into stdin.');
2018-05-17 05:23:12 +02:00
writeLn('');
writeLn(' Examples:');
writeLn('');
writeLn(' bw edit folder 5cdfbd80-d99f-409b-915b-f4c5d0241b02 eyJuYW1lIjoiTXkgRm9sZGVyMiJ9Cg==');
writeLn(' echo \'eyJuYW1lIjoiTXkgRm9sZGVyMiJ9Cg==\' | ' +
2018-05-18 17:14:47 +02:00
'bw edit folder 5cdfbd80-d99f-409b-915b-f4c5d0241b02');
2018-10-23 23:31:59 +02:00
writeLn(' bw edit item-collections 78307355-fd25-416b-88b8-b33fd0e88c82 ' +
'WyI5NzQwNTNkMC0zYjMzLTRiOTgtODg2ZS1mZWNmNWM4ZGJhOTYiXQ==');
2018-05-18 17:14:47 +02:00
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
.action(async (object, id, encodedJson, cmd) => {
2018-05-16 04:10:24 +02:00
await this.exitIfLocked();
2019-10-01 17:16:24 +02:00
const command = new EditCommand(this.main.cipherService, this.main.folderService,
this.main.cryptoService, this.main.apiService);
const response = await command.run(object, id, encodedJson, cmd);
2018-05-16 16:25:25 +02:00
this.processResponse(response);
2018-05-14 17:15:54 +02:00
});
program
.command('delete <object> <id>')
2018-05-17 21:55:44 +02:00
.option('--itemid <itemid>', 'Attachment\'s item id.')
2019-10-01 17:04:15 +02:00
.option('--organizationid <organizationid>', 'Organization id for an organization object.')
2020-04-14 20:32:43 +02:00
.option('-p, --permanent', 'Permanently deletes the item instead of soft-deleting it (item only).')
2018-05-18 17:24:49 +02:00
.description('Delete an object from the vault.')
2018-05-17 05:23:12 +02:00
.on('--help', () => {
writeLn('\n Objects:');
writeLn('');
writeLn(' item');
2018-05-17 21:55:44 +02:00
writeLn(' attachment');
2018-05-17 05:23:12 +02:00
writeLn(' folder');
2019-10-01 17:04:15 +02:00
writeLn(' org-collection');
2018-05-17 05:23:12 +02:00
writeLn('');
writeLn(' Id:');
writeLn('');
2018-05-17 22:21:12 +02:00
writeLn(' Object\'s globally unique `id`.');
2018-05-17 05:23:12 +02:00
writeLn('');
writeLn(' Examples:');
writeLn('');
2018-05-17 19:43:53 +02:00
writeLn(' bw delete item 7063feab-4b10-472e-b64c-785e2b870b92');
2020-04-14 20:32:43 +02:00
writeLn(' bw delete item 89c21cd2-fab0-4f69-8c6e-ab8a0168f69a --permanent');
2018-05-17 05:23:12 +02:00
writeLn(' bw delete folder 5cdfbd80-d99f-409b-915b-f4c5d0241b02');
2018-05-18 17:14:47 +02:00
writeLn(' bw delete attachment b857igwl1dzrs2 --itemid 310d5ffd-e9a2-4451-af87-ea054dce0f78');
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
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-18 16:58:30 +02:00
const command = new DeleteCommand(this.main.cipherService, this.main.folderService,
2019-10-01 17:04:15 +02:00
this.main.userService, this.main.apiService);
2018-05-14 21:08:48 +02:00
const response = await command.run(object, id, cmd);
2018-05-16 16:25:25 +02:00
this.processResponse(response);
2018-05-14 17:15:54 +02:00
});
program
.command('restore <object> <id>')
.description('Restores an object from the trash.')
.on('--help', () => {
writeLn('\n Objects:');
writeLn('');
writeLn(' item');
writeLn('');
writeLn(' Id:');
writeLn('');
writeLn(' Object\'s globally unique `id`.');
writeLn('');
writeLn(' Examples:');
writeLn('');
writeLn(' bw restore item 7063feab-4b10-472e-b64c-785e2b870b92');
writeLn('', true);
})
.action(async (object, id, cmd) => {
await this.exitIfLocked();
const command = new RestoreCommand(this.main.cipherService);
const response = await command.run(object, id, cmd);
this.processResponse(response);
});
2018-10-24 04:56:15 +02:00
program
.command('share <id> <organizationId> [encodedJson]')
.description('Share an item to an organization.')
.on('--help', () => {
writeLn('\n Id:');
writeLn('');
writeLn(' Item\'s globally unique `id`.');
writeLn('');
writeLn(' Organization Id:');
writeLn('');
writeLn(' Organization\'s globally unique `id`.');
writeLn('');
writeLn(' Notes:');
writeLn('');
writeLn(' `encodedJson` can also be piped into stdin. `encodedJson` contains ' +
'an array of collection ids.');
writeLn('');
writeLn(' Examples:');
writeLn('');
writeLn(' bw share 4af958ce-96a7-45d9-beed-1e70fabaa27a 6d82949b-b44d-468a-adae-3f3bacb0ea32 ' +
'WyI5NzQwNTNkMC0zYjMzLTRiOTgtODg2ZS1mZWNmNWM4ZGJhOTYiXQ==');
writeLn(' echo \'["974053d0-3b33-4b98-886e-fecf5c8dba96"]\' | bw encode | ' +
'bw share 4af958ce-96a7-45d9-beed-1e70fabaa27a 6d82949b-b44d-468a-adae-3f3bacb0ea32');
writeLn('', true);
})
.action(async (id, organizationId, encodedJson, cmd) => {
await this.exitIfLocked();
const command = new ShareCommand(this.main.cipherService);
const response = await command.run(id, organizationId, encodedJson, cmd);
this.processResponse(response);
});
2019-10-07 16:42:02 +02:00
program
.command('confirm <object> <id>')
.option('--organizationid <organizationid>', 'Organization id for an organization object.')
.description('Confirm an object to the organization.')
.on('--help', () => {
writeLn('\n Objects:');
writeLn('');
writeLn(' org-member');
writeLn('');
writeLn(' Id:');
writeLn('');
writeLn(' Object\'s globally unique `id`.');
writeLn('');
writeLn(' Examples:');
writeLn('');
writeLn(' bw confirm org-member 7063feab-4b10-472e-b64c-785e2b870b92 ' +
'--organizationid 310d5ffd-e9a2-4451-af87-ea054dce0f78');
writeLn('', true);
})
.action(async (object, id, cmd) => {
await this.exitIfLocked();
const command = new ConfirmCommand(this.main.apiService, this.main.cryptoService);
const response = await command.run(object, id, cmd);
this.processResponse(response);
});
2018-08-06 16:47:11 +02:00
program
2018-08-16 20:43:12 +02:00
.command('import [format] [input]')
2018-08-06 16:47:11 +02:00
.description('Import vault data from a file.')
.option('--formats', 'List formats')
.on('--help', () => {
writeLn('\n Examples:');
writeLn('');
writeLn(' bw import --formats');
writeLn(' bw import bitwardencsv ./from/source.csv');
2018-08-16 20:43:12 +02:00
writeLn(' bw import keepass2xml keepass_backup.xml');
2018-08-06 16:47:11 +02:00
})
2018-08-16 20:43:12 +02:00
.action(async (format, filepath, cmd) => {
2018-08-06 16:47:11 +02:00
await this.exitIfLocked();
2018-08-16 20:43:12 +02:00
const command = new ImportCommand(this.main.importService);
const response = await command.run(format, filepath, cmd);
2018-08-06 16:47:11 +02:00
this.processResponse(response);
});
2018-05-17 16:58:30 +02:00
program
.command('export [password]')
2018-12-17 16:36:40 +01:00
.description('Export vault data to a CSV or JSON file.')
2018-05-17 16:58:30 +02:00
.option('--output <output>', 'Output directory or filename.')
2018-12-17 16:36:40 +01:00
.option('--format <format>', 'Export file format.')
2019-10-07 17:01:30 +02:00
.option('--organizationid <organizationid>', 'Organization id for an organization.')
2018-05-17 16:58:30 +02:00
.on('--help', () => {
2018-12-17 16:36:40 +01:00
writeLn('\n Notes:');
writeLn('');
writeLn(' Valid formats are `csv` and `json`. Default format is `csv`.');
writeLn('');
writeLn(' If raw output is specified and no output filename or directory is given, the');
writeLn(' result is written to stdout.');
writeLn('');
2018-12-17 16:36:40 +01:00
writeLn(' Examples:');
2018-05-17 16:58:30 +02:00
writeLn('');
writeLn(' bw export');
writeLn(' bw export myPassword321');
2018-12-17 16:36:40 +01:00
writeLn(' bw export myPassword321 --format json');
2018-05-17 16:58:30 +02:00
writeLn(' bw export --output ./exp/bw.csv');
2018-12-17 16:36:40 +01:00
writeLn(' bw export myPassword321 --output bw.json --format json');
2019-10-07 17:01:30 +02:00
writeLn(' bw export myPassword321 --organizationid 7063feab-4b10-472e-b64c-785e2b870b92');
2018-05-18 17:14:47 +02:00
writeLn('', true);
2018-05-17 16:58:30 +02:00
})
.action(async (password, cmd) => {
await this.exitIfLocked();
2018-08-14 21:13:40 +02:00
const command = new ExportCommand(this.main.cryptoService, this.main.exportService);
2018-05-17 16:58:30 +02:00
const response = await command.run(password, cmd);
this.processResponse(response);
});
program
.command('generate')
2018-10-09 04:57:36 +02:00
.description('Generate a password/passphrase.')
.option('-u, --uppercase', 'Include uppercase characters.')
.option('-l, --lowercase', 'Include lowercase characters.')
.option('-n, --number', 'Include numeric characters.')
.option('-s, --special', 'Include special characters.')
2018-10-09 04:57:36 +02:00
.option('-p, --passphrase', 'Generate a passphrase.')
.option('--length <length>', 'Length of the password.')
2018-10-09 04:57:36 +02:00
.option('--words <words>', 'Number of words.')
.option('--separator <separator>', 'Word separator.')
2018-05-17 05:23:12 +02:00
.on('--help', () => {
writeLn('\n Notes:');
writeLn('');
writeLn(' Default options are `-uln --length 14`.');
writeLn('');
writeLn(' Minimum `length` is 5.');
writeLn('');
2018-10-09 04:57:36 +02:00
writeLn(' Minimum `words` is 3.');
writeLn('');
2018-05-17 05:23:12 +02:00
writeLn(' Examples:');
writeLn('');
writeLn(' bw generate');
writeLn(' bw generate -u -l --length 18');
writeLn(' bw generate -ulns --length 25');
2018-05-18 17:14:47 +02:00
writeLn(' bw generate -ul');
2018-10-09 04:57:36 +02:00
writeLn(' bw generate -p --separator _');
writeLn(' bw generate -p --words 5 --separator space');
2018-05-18 17:14:47 +02:00
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
.action(async (cmd) => {
const command = new GenerateCommand(this.main.passwordGenerationService);
const response = await command.run(cmd);
this.processResponse(response);
});
2018-05-14 23:13:57 +02:00
program
.command('encode')
2018-05-17 05:23:12 +02:00
.description('Base 64 encode stdin.')
.on('--help', () => {
writeLn('\n Notes:');
writeLn('');
writeLn(' Use to create `encodedJson` for `create` and `edit` commands.');
writeLn('');
writeLn(' Examples:');
writeLn('');
2018-05-18 17:14:47 +02:00
writeLn(' echo \'{"name":"My Folder"}\' | bw encode');
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
2018-05-14 23:13:57 +02:00
.action(async (object, id, cmd) => {
const command = new EncodeCommand();
const response = await command.run(cmd);
2018-05-16 16:25:25 +02:00
this.processResponse(response);
});
2018-05-17 06:07:44 +02:00
program
.command('config <setting> [value]')
2018-05-18 17:24:49 +02:00
.description('Configure CLI settings.')
.option('--web-vault <url>', 'Provides a custom web vault URL that differs from the base URL.')
.option('--api <url>', 'Provides a custom API URL that differs from the base URL.')
.option('--identity <url>', 'Provides a custom identity URL that differs from the base URL.')
.option('--icons <url>', 'Provides a custom icons service URL that differs from the base URL.')
.option('--notifications <url>', 'Provides a custom notifications URL that differs from the base URL.')
.option('--events <url>', 'Provides a custom events URL that differs from the base URL.')
2018-05-17 06:07:44 +02:00
.on('--help', () => {
writeLn('\n Settings:');
writeLn('');
2019-04-04 06:45:39 +02:00
writeLn(' server - On-premises hosted installation URL.');
2018-05-17 06:07:44 +02:00
writeLn('');
writeLn(' Examples:');
writeLn('');
2019-10-11 03:25:47 +02:00
writeLn(' bw config server');
2018-05-17 06:07:44 +02:00
writeLn(' bw config server https://bw.company.com');
2018-05-18 17:14:47 +02:00
writeLn(' bw config server bitwarden.com');
writeLn(' bw config server --api http://localhost:4000 --identity http://localhost:33656');
2018-05-18 17:14:47 +02:00
writeLn('', true);
2018-05-17 06:07:44 +02:00
})
.action(async (setting, value, cmd) => {
const command = new ConfigCommand(this.main.environmentService);
const response = await command.run(setting, value, cmd);
this.processResponse(response);
});
2018-05-16 16:25:25 +02:00
program
.command('update')
.description('Check for updates.')
2018-05-17 05:23:12 +02:00
.on('--help', () => {
writeLn('\n Notes:');
writeLn('');
writeLn(' Returns the URL to download the newest version of this CLI tool.');
writeLn('');
2018-05-22 20:49:20 +02:00
writeLn(' Use the `--raw` option to return only the download URL for the update.');
2018-05-18 17:24:49 +02:00
writeLn('');
2018-05-17 05:23:12 +02:00
writeLn(' Examples:');
writeLn('');
2018-05-18 17:14:47 +02:00
writeLn(' bw update');
2018-05-22 20:49:20 +02:00
writeLn(' bw update --raw');
2018-05-18 17:14:47 +02:00
writeLn('', true);
2018-05-17 05:23:12 +02:00
})
2018-05-22 23:36:54 +02:00
.action(async (cmd) => {
2019-03-18 15:33:43 +01:00
const command = new UpdateCommand(this.main.platformUtilsService, this.main.i18nService,
'cli', 'bw', true);
2018-05-22 20:49:20 +02:00
const response = await command.run(cmd);
this.processResponse(response);
2018-05-14 23:13:57 +02:00
});
program
.command('completion')
.description('Generate shell completions.')
.option('--shell <shell>', 'Shell to generate completions for.')
.on('--help', () => {
writeLn('\n Notes:');
writeLn('');
2020-05-26 15:34:44 +02:00
writeLn(' Valid shells are `zsh`.');
writeLn('');
writeLn(' Examples:');
writeLn('');
writeLn(' bw completion --shell zsh');
writeLn('', true);
})
.action(async (cmd: program.Command) => {
const command = new CompletionCommand();
const response = await command.run(cmd);
this.processResponse(response);
2020-05-26 15:34:44 +02:00
});
program
.command('status')
.description('Show server, last sync, user information, and vault status.')
.on('--help', () => {
writeLn('');
writeLn('');
writeLn(' Example return value:');
writeLn('');
writeLn(' {');
writeLn(' "serverUrl": "https://bitwarden.example.com",');
writeLn(' "lastSync": "2020-06-16T06:33:51.419Z",');
writeLn(' "userEmail": "user@example.com,');
writeLn(' "userId": "00000000-0000-0000-0000-000000000000",');
writeLn(' "status": "locked"');
writeLn(' }');
writeLn('');
writeLn(' Notes:');
writeLn('');
writeLn(' `status` is one of:');
writeLn(' - `unauthenticated` when you are not logged in');
writeLn(' - `locked` when you are logged in and the vault is locked');
writeLn(' - `unlocked` when you are logged in and the vault is unlocked');
writeLn('', true);
})
.action(async (cmd: program.Command) => {
const command = new StatusCommand(
this.main.environmentService,
this.main.syncService,
this.main.userService,
this.main.vaultTimeoutService);
const response = await command.run(cmd);
this.processResponse(response);
});
2018-05-14 17:15:54 +02:00
program
.parse(process.argv);
2018-05-16 20:47:03 +02:00
if (process.argv.slice(2).length === 0) {
program.outputHelp();
}
2018-05-14 17:15:54 +02:00
}
2018-05-14 20:54:19 +02:00
2019-03-16 16:27:27 +01:00
protected processResponse(response: Response, exitImmediately = false) {
super.processResponse(response, exitImmediately, () => {
if (response.data.object === 'template') {
return this.getJson((response.data as TemplateResponse).template);
2018-05-16 04:47:52 +02:00
}
2019-03-16 16:27:27 +01:00
return null;
});
2018-05-15 18:44:10 +02:00
}
2018-05-16 04:10:24 +02:00
private async exitIfLocked() {
await this.exitIfNotAuthed();
2018-06-13 23:20:09 +02:00
const hasKey = await this.main.cryptoService.hasKey();
if (!hasKey) {
const canInteract = process.env.BW_NOINTERACTION !== 'true';
2019-10-21 22:04:51 +02:00
if (canInteract) {
const command = new UnlockCommand(this.main.cryptoService, this.main.userService,
this.main.cryptoFunctionService, this.main.apiService);
2019-10-21 22:04:51 +02:00
const response = await command.run(null, null);
if (!response.success) {
this.processResponse(response, true);
}
} else {
this.processResponse(Response.error('Vault is locked.'), true);
}
2018-05-16 04:10:24 +02:00
}
}
2018-05-14 17:15:54 +02:00
}