2021-12-20 18:04:00 +01:00
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
|
|
|
|
2022-03-03 18:24:41 +01:00
|
|
|
import * as program from "commander";
|
|
|
|
import * as jsdom from "jsdom";
|
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { ClientType } from "@bitwarden/common/enums/clientType";
|
|
|
|
import { KeySuffixOptions } from "@bitwarden/common/enums/keySuffixOptions";
|
|
|
|
import { LogLevelType } from "@bitwarden/common/enums/logLevelType";
|
|
|
|
import { StateFactory } from "@bitwarden/common/factories/stateFactory";
|
|
|
|
import { Account } from "@bitwarden/common/models/domain/account";
|
|
|
|
import { GlobalState } from "@bitwarden/common/models/domain/globalState";
|
|
|
|
import { AppIdService } from "@bitwarden/common/services/appId.service";
|
|
|
|
import { AuditService } from "@bitwarden/common/services/audit.service";
|
|
|
|
import { AuthService } from "@bitwarden/common/services/auth.service";
|
|
|
|
import { CipherService } from "@bitwarden/common/services/cipher.service";
|
|
|
|
import { CollectionService } from "@bitwarden/common/services/collection.service";
|
|
|
|
import { ContainerService } from "@bitwarden/common/services/container.service";
|
|
|
|
import { CryptoService } from "@bitwarden/common/services/crypto.service";
|
|
|
|
import { EnvironmentService } from "@bitwarden/common/services/environment.service";
|
|
|
|
import { ExportService } from "@bitwarden/common/services/export.service";
|
|
|
|
import { FileUploadService } from "@bitwarden/common/services/fileUpload.service";
|
|
|
|
import { FolderService } from "@bitwarden/common/services/folder.service";
|
|
|
|
import { ImportService } from "@bitwarden/common/services/import.service";
|
|
|
|
import { KeyConnectorService } from "@bitwarden/common/services/keyConnector.service";
|
|
|
|
import { NoopMessagingService } from "@bitwarden/common/services/noopMessaging.service";
|
|
|
|
import { OrganizationService } from "@bitwarden/common/services/organization.service";
|
|
|
|
import { PasswordGenerationService } from "@bitwarden/common/services/passwordGeneration.service";
|
|
|
|
import { PolicyService } from "@bitwarden/common/services/policy.service";
|
|
|
|
import { ProviderService } from "@bitwarden/common/services/provider.service";
|
|
|
|
import { SearchService } from "@bitwarden/common/services/search.service";
|
|
|
|
import { SendService } from "@bitwarden/common/services/send.service";
|
|
|
|
import { SettingsService } from "@bitwarden/common/services/settings.service";
|
|
|
|
import { StateService } from "@bitwarden/common/services/state.service";
|
|
|
|
import { StateMigrationService } from "@bitwarden/common/services/stateMigration.service";
|
|
|
|
import { SyncService } from "@bitwarden/common/services/sync.service";
|
|
|
|
import { TokenService } from "@bitwarden/common/services/token.service";
|
|
|
|
import { TotpService } from "@bitwarden/common/services/totp.service";
|
|
|
|
import { TwoFactorService } from "@bitwarden/common/services/twoFactor.service";
|
|
|
|
import { UserVerificationService } from "@bitwarden/common/services/userVerification.service";
|
|
|
|
import { VaultTimeoutService } from "@bitwarden/common/services/vaultTimeout.service";
|
|
|
|
import { CliPlatformUtilsService } from "@bitwarden/node/cli/services/cliPlatformUtils.service";
|
|
|
|
import { ConsoleLogService } from "@bitwarden/node/cli/services/consoleLog.service";
|
|
|
|
import { NodeApiService } from "@bitwarden/node/services/nodeApi.service";
|
|
|
|
import { NodeCryptoFunctionService } from "@bitwarden/node/services/nodeCryptoFunction.service";
|
2021-12-20 18:04:00 +01:00
|
|
|
|
|
|
|
import { Program } from "./program";
|
|
|
|
import { SendProgram } from "./send.program";
|
2022-03-03 18:24:41 +01:00
|
|
|
import { I18nService } from "./services/i18n.service";
|
|
|
|
import { LowdbStorageService } from "./services/lowdbStorage.service";
|
|
|
|
import { NodeEnvSecureStorageService } from "./services/nodeEnvSecureStorage.service";
|
2021-12-20 18:04:00 +01:00
|
|
|
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
|
|
|
|
2022-03-03 18:24:41 +01:00
|
|
|
// eslint-disable-next-line
|
2021-12-20 18:04:00 +01:00
|
|
|
const packageJson = require("../package.json");
|
2019-03-16 03:34:59 +01:00
|
|
|
|
2018-05-14 17:15:54 +02:00
|
|
|
export class Main {
|
2021-12-20 18:04:00 +01:00
|
|
|
messagingService: NoopMessagingService;
|
|
|
|
storageService: LowdbStorageService;
|
|
|
|
secureStorageService: NodeEnvSecureStorageService;
|
|
|
|
i18nService: I18nService;
|
|
|
|
platformUtilsService: CliPlatformUtilsService;
|
|
|
|
cryptoService: CryptoService;
|
|
|
|
tokenService: TokenService;
|
|
|
|
appIdService: AppIdService;
|
|
|
|
apiService: NodeApiService;
|
|
|
|
environmentService: EnvironmentService;
|
|
|
|
settingsService: SettingsService;
|
|
|
|
cipherService: CipherService;
|
|
|
|
folderService: FolderService;
|
|
|
|
collectionService: CollectionService;
|
|
|
|
vaultTimeoutService: VaultTimeoutService;
|
|
|
|
syncService: SyncService;
|
|
|
|
passwordGenerationService: PasswordGenerationService;
|
|
|
|
totpService: TotpService;
|
|
|
|
containerService: ContainerService;
|
|
|
|
auditService: AuditService;
|
|
|
|
importService: ImportService;
|
|
|
|
exportService: ExportService;
|
|
|
|
searchService: SearchService;
|
|
|
|
cryptoFunctionService: NodeCryptoFunctionService;
|
|
|
|
authService: AuthService;
|
|
|
|
policyService: PolicyService;
|
|
|
|
program: Program;
|
|
|
|
vaultProgram: VaultProgram;
|
|
|
|
sendProgram: SendProgram;
|
|
|
|
logService: ConsoleLogService;
|
|
|
|
sendService: SendService;
|
|
|
|
fileUploadService: FileUploadService;
|
|
|
|
keyConnectorService: KeyConnectorService;
|
|
|
|
userVerificationService: UserVerificationService;
|
2021-12-28 21:38:51 +01:00
|
|
|
stateService: StateService;
|
|
|
|
stateMigrationService: StateMigrationService;
|
|
|
|
organizationService: OrganizationService;
|
|
|
|
providerService: ProviderService;
|
2022-02-08 00:31:36 +01:00
|
|
|
twoFactorService: TwoFactorService;
|
2021-12-20 18:04:00 +01:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
let p = null;
|
|
|
|
const relativeDataDir = path.join(path.dirname(process.execPath), "bw-data");
|
|
|
|
if (fs.existsSync(relativeDataDir)) {
|
|
|
|
p = relativeDataDir;
|
|
|
|
} else if (process.env.BITWARDENCLI_APPDATA_DIR) {
|
|
|
|
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");
|
|
|
|
} else if (process.env.XDG_CONFIG_HOME) {
|
|
|
|
p = path.join(process.env.XDG_CONFIG_HOME, "Bitwarden CLI");
|
|
|
|
} else {
|
|
|
|
p = path.join(process.env.HOME, ".config/Bitwarden CLI");
|
2018-05-14 17:15:54 +02:00
|
|
|
}
|
2018-05-11 19:47:18 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.i18nService = new I18nService("en", "./locales");
|
2022-02-08 15:29:22 +01:00
|
|
|
this.platformUtilsService = new CliPlatformUtilsService(ClientType.Cli, packageJson);
|
2021-12-20 18:04:00 +01:00
|
|
|
this.logService = new ConsoleLogService(
|
|
|
|
this.platformUtilsService.isDev(),
|
|
|
|
(level) => process.env.BITWARDENCLI_DEBUG !== "true" && level <= LogLevelType.Info
|
|
|
|
);
|
|
|
|
this.cryptoFunctionService = new NodeCryptoFunctionService();
|
2022-02-10 17:24:41 +01:00
|
|
|
this.storageService = new LowdbStorageService(this.logService, null, p, false, true);
|
2021-12-20 18:04:00 +01:00
|
|
|
this.secureStorageService = new NodeEnvSecureStorageService(
|
|
|
|
this.storageService,
|
|
|
|
this.logService,
|
|
|
|
() => this.cryptoService
|
|
|
|
);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
|
|
|
this.stateMigrationService = new StateMigrationService(
|
|
|
|
this.storageService,
|
2022-02-01 00:00:19 +01:00
|
|
|
this.secureStorageService,
|
2022-02-03 20:08:45 +01:00
|
|
|
new StateFactory(GlobalState, Account)
|
2021-12-28 21:38:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
this.stateService = new StateService(
|
2021-12-20 18:04:00 +01:00
|
|
|
this.storageService,
|
|
|
|
this.secureStorageService,
|
2021-12-28 21:38:51 +01:00
|
|
|
this.logService,
|
2022-01-20 21:03:37 +01:00
|
|
|
this.stateMigrationService,
|
2022-02-01 00:00:19 +01:00
|
|
|
new StateFactory(GlobalState, Account)
|
2021-12-28 21:38:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
this.cryptoService = new CryptoService(
|
2021-12-20 18:04:00 +01:00
|
|
|
this.cryptoFunctionService,
|
|
|
|
this.platformUtilsService,
|
2021-12-28 21:38:51 +01:00
|
|
|
this.logService,
|
|
|
|
this.stateService
|
2021-12-20 18:04:00 +01:00
|
|
|
);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.appIdService = new AppIdService(this.storageService);
|
2021-12-28 21:38:51 +01:00
|
|
|
this.tokenService = new TokenService(this.stateService);
|
2021-12-20 18:04:00 +01:00
|
|
|
this.messagingService = new NoopMessagingService();
|
2021-12-28 21:38:51 +01:00
|
|
|
this.environmentService = new EnvironmentService(this.stateService);
|
2022-04-01 03:50:48 +02:00
|
|
|
|
|
|
|
const customUserAgent =
|
|
|
|
"Bitwarden_CLI/" +
|
|
|
|
this.platformUtilsService.getApplicationVersionSync() +
|
|
|
|
" (" +
|
|
|
|
this.platformUtilsService.getDeviceString().toUpperCase() +
|
|
|
|
")";
|
2021-12-20 18:04:00 +01:00
|
|
|
this.apiService = new NodeApiService(
|
|
|
|
this.tokenService,
|
|
|
|
this.platformUtilsService,
|
|
|
|
this.environmentService,
|
2022-04-01 03:50:48 +02:00
|
|
|
this.appIdService,
|
2021-12-20 18:04:00 +01:00
|
|
|
async (expired: boolean) => await this.logout(),
|
2022-04-01 03:50:48 +02:00
|
|
|
customUserAgent
|
2021-12-20 18:04:00 +01:00
|
|
|
);
|
|
|
|
this.containerService = new ContainerService(this.cryptoService);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
|
|
|
this.settingsService = new SettingsService(this.stateService);
|
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.fileUploadService = new FileUploadService(this.logService, this.apiService);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.cipherService = new CipherService(
|
|
|
|
this.cryptoService,
|
|
|
|
this.settingsService,
|
|
|
|
this.apiService,
|
|
|
|
this.fileUploadService,
|
|
|
|
this.i18nService,
|
|
|
|
null,
|
2021-12-28 21:38:51 +01:00
|
|
|
this.logService,
|
|
|
|
this.stateService
|
2021-12-20 18:04:00 +01:00
|
|
|
);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.folderService = new FolderService(
|
|
|
|
this.cryptoService,
|
|
|
|
this.apiService,
|
|
|
|
this.i18nService,
|
2021-12-28 21:38:51 +01:00
|
|
|
this.cipherService,
|
|
|
|
this.stateService
|
2021-12-20 18:04:00 +01:00
|
|
|
);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.collectionService = new CollectionService(
|
|
|
|
this.cryptoService,
|
2021-12-28 21:38:51 +01:00
|
|
|
this.i18nService,
|
|
|
|
this.stateService
|
2021-12-20 18:04:00 +01:00
|
|
|
);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.searchService = new SearchService(this.cipherService, this.logService, this.i18nService);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
|
|
|
this.providerService = new ProviderService(this.stateService);
|
|
|
|
|
|
|
|
this.organizationService = new OrganizationService(this.stateService);
|
|
|
|
|
|
|
|
this.policyService = new PolicyService(
|
|
|
|
this.stateService,
|
|
|
|
this.organizationService,
|
|
|
|
this.apiService
|
|
|
|
);
|
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.sendService = new SendService(
|
|
|
|
this.cryptoService,
|
|
|
|
this.apiService,
|
|
|
|
this.fileUploadService,
|
|
|
|
this.i18nService,
|
2021-12-28 21:38:51 +01:00
|
|
|
this.cryptoFunctionService,
|
|
|
|
this.stateService
|
2021-12-20 18:04:00 +01:00
|
|
|
);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.keyConnectorService = new KeyConnectorService(
|
2021-12-28 21:38:51 +01:00
|
|
|
this.stateService,
|
2021-12-20 18:04:00 +01:00
|
|
|
this.cryptoService,
|
|
|
|
this.apiService,
|
|
|
|
this.tokenService,
|
2021-12-28 21:38:51 +01:00
|
|
|
this.logService,
|
2022-02-08 00:31:36 +01:00
|
|
|
this.organizationService,
|
2022-05-26 03:16:05 +02:00
|
|
|
this.cryptoFunctionService,
|
|
|
|
async (expired: boolean) => await this.logout()
|
2021-12-20 18:04:00 +01:00
|
|
|
);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
2022-05-16 03:58:36 +02:00
|
|
|
this.twoFactorService = new TwoFactorService(this.i18nService, this.platformUtilsService);
|
|
|
|
|
|
|
|
this.authService = new AuthService(
|
|
|
|
this.cryptoService,
|
|
|
|
this.apiService,
|
|
|
|
this.tokenService,
|
|
|
|
this.appIdService,
|
|
|
|
this.platformUtilsService,
|
|
|
|
this.messagingService,
|
|
|
|
this.logService,
|
|
|
|
this.keyConnectorService,
|
|
|
|
this.environmentService,
|
|
|
|
this.stateService,
|
|
|
|
this.twoFactorService,
|
|
|
|
this.i18nService
|
|
|
|
);
|
|
|
|
|
|
|
|
const lockedCallback = async () =>
|
|
|
|
await this.cryptoService.clearStoredKey(KeySuffixOptions.Auto);
|
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.vaultTimeoutService = new VaultTimeoutService(
|
|
|
|
this.cipherService,
|
|
|
|
this.folderService,
|
|
|
|
this.collectionService,
|
|
|
|
this.cryptoService,
|
|
|
|
this.platformUtilsService,
|
|
|
|
this.messagingService,
|
|
|
|
this.searchService,
|
|
|
|
this.tokenService,
|
|
|
|
this.policyService,
|
|
|
|
this.keyConnectorService,
|
2021-12-28 21:38:51 +01:00
|
|
|
this.stateService,
|
2022-05-16 03:58:36 +02:00
|
|
|
this.authService,
|
|
|
|
lockedCallback,
|
2021-12-20 18:04:00 +01:00
|
|
|
null
|
|
|
|
);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.syncService = new SyncService(
|
|
|
|
this.apiService,
|
|
|
|
this.settingsService,
|
|
|
|
this.folderService,
|
|
|
|
this.cipherService,
|
|
|
|
this.cryptoService,
|
|
|
|
this.collectionService,
|
|
|
|
this.messagingService,
|
|
|
|
this.policyService,
|
|
|
|
this.sendService,
|
|
|
|
this.logService,
|
|
|
|
this.keyConnectorService,
|
2021-12-28 21:38:51 +01:00
|
|
|
this.stateService,
|
|
|
|
this.organizationService,
|
|
|
|
this.providerService,
|
2021-12-20 18:04:00 +01:00
|
|
|
async (expired: boolean) => await this.logout()
|
|
|
|
);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.passwordGenerationService = new PasswordGenerationService(
|
|
|
|
this.cryptoService,
|
2021-12-28 21:38:51 +01:00
|
|
|
this.policyService,
|
|
|
|
this.stateService
|
2021-12-20 18:04:00 +01:00
|
|
|
);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.totpService = new TotpService(
|
|
|
|
this.cryptoFunctionService,
|
2021-12-28 21:38:51 +01:00
|
|
|
this.logService,
|
|
|
|
this.stateService
|
2021-12-20 18:04:00 +01:00
|
|
|
);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.importService = new ImportService(
|
|
|
|
this.cipherService,
|
|
|
|
this.folderService,
|
|
|
|
this.apiService,
|
|
|
|
this.i18nService,
|
|
|
|
this.collectionService,
|
|
|
|
this.platformUtilsService,
|
|
|
|
this.cryptoService
|
|
|
|
);
|
|
|
|
this.exportService = new ExportService(
|
|
|
|
this.folderService,
|
|
|
|
this.cipherService,
|
|
|
|
this.apiService,
|
2022-02-08 00:31:36 +01:00
|
|
|
this.cryptoService,
|
|
|
|
this.cryptoFunctionService
|
2021-12-20 18:04:00 +01:00
|
|
|
);
|
2021-12-28 21:38:51 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
this.auditService = new AuditService(this.cryptoFunctionService, this.apiService);
|
|
|
|
this.program = new Program(this);
|
|
|
|
this.vaultProgram = new VaultProgram(this);
|
|
|
|
this.sendProgram = new SendProgram(this);
|
|
|
|
this.userVerificationService = new UserVerificationService(
|
|
|
|
this.cryptoService,
|
|
|
|
this.i18nService,
|
|
|
|
this.apiService
|
|
|
|
);
|
|
|
|
}
|
2021-02-03 18:44:33 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
async run() {
|
|
|
|
await this.init();
|
2021-02-03 18:44:33 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
await this.program.register();
|
|
|
|
await this.vaultProgram.register();
|
|
|
|
await this.sendProgram.register();
|
2021-02-03 18:44:33 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
program.parse(process.argv);
|
2021-02-03 18:44:33 +01:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
if (process.argv.slice(2).length === 0) {
|
|
|
|
program.outputHelp();
|
2018-05-15 05:16:59 +02:00
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2018-05-15 05:16:59 +02:00
|
|
|
|
2021-12-20 18:04:00 +01:00
|
|
|
async logout() {
|
2022-01-20 21:03:37 +01:00
|
|
|
this.authService.logOut(() => {
|
|
|
|
/* Do nothing */
|
|
|
|
});
|
2021-12-28 21:38:51 +01:00
|
|
|
const userId = await this.stateService.getUserId();
|
2021-12-20 18:04:00 +01:00
|
|
|
await Promise.all([
|
|
|
|
this.syncService.setLastSync(new Date(0)),
|
|
|
|
this.cryptoService.clearKeys(),
|
|
|
|
this.settingsService.clear(userId),
|
|
|
|
this.cipherService.clear(userId),
|
|
|
|
this.folderService.clear(userId),
|
|
|
|
this.collectionService.clear(userId),
|
|
|
|
this.policyService.clear(userId),
|
|
|
|
this.passwordGenerationService.clear(),
|
|
|
|
]);
|
2021-12-28 21:38:51 +01:00
|
|
|
await this.stateService.clean();
|
2021-12-20 18:04:00 +01:00
|
|
|
process.env.BW_SESSION = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async init() {
|
|
|
|
await this.storageService.init();
|
2021-12-28 21:38:51 +01:00
|
|
|
await this.stateService.init();
|
2021-12-20 18:04:00 +01:00
|
|
|
this.containerService.attachToWindow(global);
|
|
|
|
await this.environmentService.setUrlsFromStorage();
|
2021-12-28 21:38:51 +01:00
|
|
|
const locale = await this.stateService.getLocale();
|
2021-12-20 18:04:00 +01:00
|
|
|
await this.i18nService.init(locale);
|
2022-02-08 00:31:36 +01:00
|
|
|
this.twoFactorService.init();
|
2018-05-16 05:26:36 +02:00
|
|
|
|
2021-12-28 21:38:51 +01:00
|
|
|
const installedVersion = await this.stateService.getInstalledVersion();
|
2021-12-20 18:04:00 +01:00
|
|
|
const currentVersion = await this.platformUtilsService.getApplicationVersion();
|
|
|
|
if (installedVersion == null || installedVersion !== currentVersion) {
|
2021-12-28 21:38:51 +01:00
|
|
|
await this.stateService.setInstalledVersion(currentVersion);
|
2018-05-14 17:15:54 +02:00
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
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();
|