2021-02-03 18:44:33 +01:00
|
|
|
import * as program from 'commander';
|
2020-03-09 17:06:38 +01:00
|
|
|
import * as fs from 'fs';
|
2019-02-08 04:17:22 +01:00
|
|
|
import * as jsdom from 'jsdom';
|
2018-05-31 15:08:54 +02:00
|
|
|
import * as path from 'path';
|
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { LogLevelType } from 'jslib-common/enums/logLevelType';
|
2020-10-20 16:07:36 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { AuthService } from 'jslib-common/services/auth.service';
|
2018-05-12 21:12:28 +02:00
|
|
|
|
2018-05-15 05:40:11 +02:00
|
|
|
import { I18nService } from './services/i18n.service';
|
2018-05-16 03:11:58 +02:00
|
|
|
import { NodeEnvSecureStorageService } from './services/nodeEnvSecureStorage.service';
|
2019-03-16 03:34:59 +01:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { CliPlatformUtilsService } from 'jslib-node/cli/services/cliPlatformUtils.service';
|
|
|
|
import { ConsoleLogService } from 'jslib-node/cli/services/consoleLog.service';
|
|
|
|
|
|
|
|
import { AppIdService } from 'jslib-common/services/appId.service';
|
|
|
|
import { AuditService } from 'jslib-common/services/audit.service';
|
|
|
|
import { CipherService } from 'jslib-common/services/cipher.service';
|
|
|
|
import { CollectionService } from 'jslib-common/services/collection.service';
|
|
|
|
import { ConstantsService } from 'jslib-common/services/constants.service';
|
|
|
|
import { ContainerService } from 'jslib-common/services/container.service';
|
|
|
|
import { CryptoService } from 'jslib-common/services/crypto.service';
|
|
|
|
import { EnvironmentService } from 'jslib-common/services/environment.service';
|
|
|
|
import { ExportService } from 'jslib-common/services/export.service';
|
|
|
|
import { FileUploadService } from 'jslib-common/services/fileUpload.service';
|
|
|
|
import { FolderService } from 'jslib-common/services/folder.service';
|
|
|
|
import { ImportService } from 'jslib-common/services/import.service';
|
|
|
|
import { NoopMessagingService } from 'jslib-common/services/noopMessaging.service';
|
|
|
|
import { PasswordGenerationService } from 'jslib-common/services/passwordGeneration.service';
|
|
|
|
import { PolicyService } from 'jslib-common/services/policy.service';
|
|
|
|
import { SearchService } from 'jslib-common/services/search.service';
|
|
|
|
import { SendService } from 'jslib-common/services/send.service';
|
|
|
|
import { SettingsService } from 'jslib-common/services/settings.service';
|
|
|
|
import { SyncService } from 'jslib-common/services/sync.service';
|
|
|
|
import { TokenService } from 'jslib-common/services/token.service';
|
|
|
|
import { TotpService } from 'jslib-common/services/totp.service';
|
|
|
|
import { UserService } from 'jslib-common/services/user.service';
|
|
|
|
import { VaultTimeoutService } from 'jslib-common/services/vaultTimeout.service';
|
|
|
|
import { LowdbStorageService } from 'jslib-node/services/lowdbStorage.service';
|
|
|
|
import { NodeApiService } from 'jslib-node/services/nodeApi.service';
|
2021-06-18 14:12:00 +02:00
|
|
|
import { NodeCryptoFunctionService } from 'jslib-node/services/nodeCryptoFunction.service';
|
2018-05-13 06:19:14 +02:00
|
|
|
|
2018-05-14 17:15:54 +02:00
|
|
|
import { Program } from './program';
|
2021-02-03 18:44:33 +01:00
|
|
|
import { SendProgram } from './send.program';
|
|
|
|
import { VaultProgram } from './vault.program';
|
2018-05-13 06:19:14 +02:00
|
|
|
|
2019-02-07 21:46:22 +01:00
|
|
|
// Polyfills
|
2019-02-07 22:56:36 +01:00
|
|
|
(global as any).DOMParser = new jsdom.JSDOM().window.DOMParser;
|
2019-02-07 21:46:22 +01:00
|
|
|
|
2019-03-16 03:34:59 +01:00
|
|
|
// tslint:disable-next-line
|
|
|
|
const packageJson = require('../package.json');
|
|
|
|
|
2018-05-14 17:15:54 +02:00
|
|
|
export class Main {
|
2018-05-16 05:26:36 +02:00
|
|
|
messagingService: NoopMessagingService;
|
2018-05-16 21:22:32 +02:00
|
|
|
storageService: LowdbStorageService;
|
|
|
|
secureStorageService: NodeEnvSecureStorageService;
|
2018-05-14 17:15:54 +02:00
|
|
|
i18nService: I18nService;
|
2019-03-16 03:34:59 +01:00
|
|
|
platformUtilsService: CliPlatformUtilsService;
|
2018-05-14 17:15:54 +02:00
|
|
|
constantsService: ConstantsService;
|
|
|
|
cryptoService: CryptoService;
|
|
|
|
tokenService: TokenService;
|
|
|
|
appIdService: AppIdService;
|
2018-05-15 05:57:02 +02:00
|
|
|
apiService: NodeApiService;
|
2018-05-14 17:15:54 +02:00
|
|
|
environmentService: EnvironmentService;
|
|
|
|
userService: UserService;
|
|
|
|
settingsService: SettingsService;
|
|
|
|
cipherService: CipherService;
|
|
|
|
folderService: FolderService;
|
|
|
|
collectionService: CollectionService;
|
2020-04-03 16:47:45 +02:00
|
|
|
vaultTimeoutService: VaultTimeoutService;
|
2018-05-14 17:15:54 +02:00
|
|
|
syncService: SyncService;
|
|
|
|
passwordGenerationService: PasswordGenerationService;
|
|
|
|
totpService: TotpService;
|
|
|
|
containerService: ContainerService;
|
|
|
|
auditService: AuditService;
|
2018-08-06 15:38:17 +02:00
|
|
|
importService: ImportService;
|
2018-05-17 16:58:30 +02:00
|
|
|
exportService: ExportService;
|
2018-08-13 20:38:04 +02:00
|
|
|
searchService: SearchService;
|
2018-05-14 17:15:54 +02:00
|
|
|
cryptoFunctionService: NodeCryptoFunctionService;
|
|
|
|
authService: AuthService;
|
2020-01-29 04:37:44 +01:00
|
|
|
policyService: PolicyService;
|
2018-05-14 17:15:54 +02:00
|
|
|
program: Program;
|
2021-02-03 18:44:33 +01:00
|
|
|
vaultProgram: VaultProgram;
|
|
|
|
sendProgram: SendProgram;
|
2020-10-20 16:07:36 +02:00
|
|
|
logService: ConsoleLogService;
|
2020-11-23 21:56:40 +01:00
|
|
|
sendService: SendService;
|
2021-03-29 16:47:39 +02:00
|
|
|
fileUploadService: FileUploadService;
|
2018-05-11 19:47:18 +02:00
|
|
|
|
2018-05-14 17:15:54 +02:00
|
|
|
constructor() {
|
2018-05-31 15:08:54 +02:00
|
|
|
let p = null;
|
2020-03-09 18:32:22 +01:00
|
|
|
const relativeDataDir = path.join(path.dirname(process.execPath), 'bw-data');
|
2020-03-09 17:06:38 +01:00
|
|
|
if (fs.existsSync(relativeDataDir)) {
|
|
|
|
p = relativeDataDir;
|
|
|
|
} else if (process.env.BITWARDENCLI_APPDATA_DIR) {
|
2018-05-31 15:08:54 +02:00
|
|
|
p = path.resolve(process.env.BITWARDENCLI_APPDATA_DIR);
|
|
|
|
} else if (process.platform === 'darwin') {
|
|
|
|
p = path.join(process.env.HOME, 'Library/Application Support/Bitwarden CLI');
|
|
|
|
} else if (process.platform === 'win32') {
|
|
|
|
p = path.join(process.env.APPDATA, 'Bitwarden CLI');
|
2019-01-14 21:33:14 +01:00
|
|
|
} else if (process.env.XDG_CONFIG_HOME) {
|
|
|
|
p = path.join(process.env.XDG_CONFIG_HOME, 'Bitwarden CLI');
|
2018-05-31 15:08:54 +02:00
|
|
|
} else {
|
|
|
|
p = path.join(process.env.HOME, '.config/Bitwarden CLI');
|
|
|
|
}
|
|
|
|
|
2018-05-15 05:16:59 +02:00
|
|
|
this.i18nService = new I18nService('en', './locales');
|
2019-03-16 03:34:59 +01:00
|
|
|
this.platformUtilsService = new CliPlatformUtilsService('cli', packageJson);
|
2020-10-20 16:07:36 +02:00
|
|
|
this.logService = new ConsoleLogService(this.platformUtilsService.isDev(),
|
2021-02-04 05:51:59 +01:00
|
|
|
level => process.env.BITWARDENCLI_DEBUG !== 'true' && level <= LogLevelType.Info);
|
2018-05-14 17:15:54 +02:00
|
|
|
this.cryptoFunctionService = new NodeCryptoFunctionService();
|
2020-10-20 16:07:36 +02:00
|
|
|
this.storageService = new LowdbStorageService(this.logService, null, p, true);
|
2020-12-14 18:29:17 +01:00
|
|
|
this.secureStorageService = new NodeEnvSecureStorageService(this.storageService, this.logService,
|
|
|
|
() => this.cryptoService);
|
2018-05-16 03:11:58 +02:00
|
|
|
this.cryptoService = new CryptoService(this.storageService, this.secureStorageService,
|
2020-12-14 18:29:17 +01:00
|
|
|
this.cryptoFunctionService, this.platformUtilsService, this.logService);
|
2018-05-14 17:15:54 +02:00
|
|
|
this.appIdService = new AppIdService(this.storageService);
|
|
|
|
this.tokenService = new TokenService(this.storageService);
|
2018-05-16 05:26:36 +02:00
|
|
|
this.messagingService = new NoopMessagingService();
|
2021-07-23 22:46:32 +02:00
|
|
|
this.environmentService = new EnvironmentService(this.storageService);
|
|
|
|
this.apiService = new NodeApiService(this.tokenService, this.platformUtilsService, this.environmentService,
|
2019-10-07 16:12:39 +02:00
|
|
|
async (expired: boolean) => await this.logout(),
|
|
|
|
'Bitwarden_CLI/' + this.platformUtilsService.getApplicationVersion() +
|
2021-08-13 16:18:49 +02:00
|
|
|
' (' + this.platformUtilsService.getDeviceString().toUpperCase() + ')', (clientId, clientSecret) =>
|
|
|
|
this.authService.logInApiKey(clientId, clientSecret));
|
2018-05-14 17:15:54 +02:00
|
|
|
this.userService = new UserService(this.tokenService, this.storageService);
|
2018-10-14 04:27:33 +02:00
|
|
|
this.containerService = new ContainerService(this.cryptoService);
|
2018-05-14 17:15:54 +02:00
|
|
|
this.settingsService = new SettingsService(this.userService, this.storageService);
|
2021-03-29 16:47:39 +02:00
|
|
|
this.fileUploadService = new FileUploadService(this.logService, this.apiService);
|
2018-05-14 17:15:54 +02:00
|
|
|
this.cipherService = new CipherService(this.cryptoService, this.userService, this.settingsService,
|
2021-10-20 22:41:33 +02:00
|
|
|
this.apiService, this.fileUploadService, this.storageService, this.i18nService, null, this.logService);
|
2018-06-25 21:03:57 +02:00
|
|
|
this.folderService = new FolderService(this.cryptoService, this.userService, this.apiService,
|
|
|
|
this.storageService, this.i18nService, this.cipherService);
|
2018-05-14 17:15:54 +02:00
|
|
|
this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService,
|
|
|
|
this.i18nService);
|
2021-04-12 17:13:00 +02:00
|
|
|
this.searchService = new SearchService(this.cipherService, this.logService, this.i18nService);
|
2021-10-20 22:41:33 +02:00
|
|
|
this.policyService = new PolicyService(this.userService, this.storageService, this.apiService);
|
2021-03-29 16:47:39 +02:00
|
|
|
this.sendService = new SendService(this.cryptoService, this.userService, this.apiService, this.fileUploadService,
|
|
|
|
this.storageService, this.i18nService, this.cryptoFunctionService);
|
2020-04-14 19:04:19 +02:00
|
|
|
this.vaultTimeoutService = new VaultTimeoutService(this.cipherService, this.folderService,
|
|
|
|
this.collectionService, this.cryptoService, this.platformUtilsService, this.storageService,
|
2021-09-15 15:57:43 +02:00
|
|
|
this.messagingService, this.searchService, this.userService, this.tokenService, this.policyService,
|
2021-06-22 19:37:30 +02:00
|
|
|
async () => await this.cryptoService.clearStoredKey('auto'), null);
|
2018-05-14 17:15:54 +02:00
|
|
|
this.syncService = new SyncService(this.userService, this.apiService, this.settingsService,
|
|
|
|
this.folderService, this.cipherService, this.cryptoService, this.collectionService,
|
2020-11-23 21:56:40 +01:00
|
|
|
this.storageService, this.messagingService, this.policyService, this.sendService,
|
2021-10-20 22:41:33 +02:00
|
|
|
this.logService, async (expired: boolean) => await this.logout());
|
2020-03-03 22:29:10 +01:00
|
|
|
this.passwordGenerationService = new PasswordGenerationService(this.cryptoService, this.storageService,
|
|
|
|
this.policyService);
|
2021-10-20 22:41:33 +02:00
|
|
|
this.totpService = new TotpService(this.storageService, this.cryptoFunctionService, this.logService);
|
2018-08-06 15:38:17 +02:00
|
|
|
this.importService = new ImportService(this.cipherService, this.folderService, this.apiService,
|
2021-05-25 23:42:59 +02:00
|
|
|
this.i18nService, this.collectionService, this.platformUtilsService, this.cryptoService);
|
|
|
|
this.exportService = new ExportService(this.folderService, this.cipherService, this.apiService,
|
|
|
|
this.cryptoService);
|
2018-05-14 17:15:54 +02:00
|
|
|
this.authService = new AuthService(this.cryptoService, this.apiService, this.userService, this.tokenService,
|
2020-08-03 18:30:32 +02:00
|
|
|
this.appIdService, this.i18nService, this.platformUtilsService, this.messagingService,
|
2021-10-28 17:17:42 +02:00
|
|
|
this.vaultTimeoutService, this.logService, this.cryptoFunctionService, true);
|
2018-07-08 05:52:06 +02:00
|
|
|
this.auditService = new AuditService(this.cryptoFunctionService, this.apiService);
|
2018-05-14 17:15:54 +02:00
|
|
|
this.program = new Program(this);
|
2021-02-03 18:44:33 +01:00
|
|
|
this.vaultProgram = new VaultProgram(this);
|
|
|
|
this.sendProgram = new SendProgram(this);
|
2018-05-14 17:15:54 +02:00
|
|
|
}
|
2018-05-11 19:47:18 +02:00
|
|
|
|
2018-05-15 05:16:59 +02:00
|
|
|
async run() {
|
|
|
|
await this.init();
|
2021-02-03 18:44:33 +01:00
|
|
|
|
2021-04-12 17:13:00 +02:00
|
|
|
await this.program.register();
|
|
|
|
await this.vaultProgram.register();
|
|
|
|
await this.sendProgram.register();
|
2021-02-03 18:44:33 +01:00
|
|
|
|
|
|
|
program
|
|
|
|
.parse(process.argv);
|
|
|
|
|
|
|
|
if (process.argv.slice(2).length === 0) {
|
|
|
|
program.outputHelp();
|
|
|
|
}
|
|
|
|
|
2018-05-15 05:16:59 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 05:26:36 +02:00
|
|
|
async logout() {
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
await Promise.all([
|
|
|
|
this.syncService.setLastSync(new Date(0)),
|
|
|
|
this.tokenService.clearToken(),
|
|
|
|
this.cryptoService.clearKeys(),
|
|
|
|
this.userService.clear(),
|
|
|
|
this.settingsService.clear(userId),
|
|
|
|
this.cipherService.clear(userId),
|
|
|
|
this.folderService.clear(userId),
|
|
|
|
this.collectionService.clear(userId),
|
2020-01-29 04:37:44 +01:00
|
|
|
this.policyService.clear(userId),
|
2018-05-16 05:26:36 +02:00
|
|
|
this.passwordGenerationService.clear(),
|
|
|
|
]);
|
2018-05-16 05:28:03 +02:00
|
|
|
process.env.BW_SESSION = null;
|
2018-05-16 05:26:36 +02:00
|
|
|
}
|
|
|
|
|
2018-05-14 17:15:54 +02:00
|
|
|
private async init() {
|
2021-03-02 17:05:20 +01:00
|
|
|
await this.storageService.init();
|
2018-05-14 17:15:54 +02:00
|
|
|
this.containerService.attachToWindow(global);
|
|
|
|
await this.environmentService.setUrlsFromStorage();
|
2018-07-30 18:01:24 +02:00
|
|
|
// Dev Server URLs. Comment out the line above.
|
|
|
|
// this.apiService.setUrls({
|
|
|
|
// base: null,
|
|
|
|
// api: 'http://localhost:4000',
|
|
|
|
// identity: 'http://localhost:33656',
|
|
|
|
// });
|
2018-05-14 17:15:54 +02:00
|
|
|
const locale = await this.storageService.get<string>(ConstantsService.localeKey);
|
|
|
|
await this.i18nService.init(locale);
|
2018-10-04 18:05:41 +02:00
|
|
|
this.authService.init();
|
2018-05-17 03:19:23 +02:00
|
|
|
|
|
|
|
const installedVersion = await this.storageService.get<string>(ConstantsService.installedVersionKey);
|
2021-04-12 17:13:00 +02:00
|
|
|
const currentVersion = await this.platformUtilsService.getApplicationVersion();
|
2018-05-17 03:19:23 +02:00
|
|
|
if (installedVersion == null || installedVersion !== currentVersion) {
|
|
|
|
await this.storageService.save(ConstantsService.installedVersionKey, currentVersion);
|
|
|
|
}
|
2018-05-14 17:15:54 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 17:59:34 +02:00
|
|
|
|
2018-05-15 05:16:59 +02:00
|
|
|
const main = new Main();
|
|
|
|
main.run();
|