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';
|
|
|
|
|
2018-05-12 21:12:28 +02:00
|
|
|
import { AuthService } from 'jslib/services/auth.service';
|
|
|
|
|
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
|
|
|
|
|
|
|
import { CliPlatformUtilsService } from 'jslib/cli/services/cliPlatformUtils.service';
|
2018-05-15 05:40:11 +02:00
|
|
|
|
2018-05-13 06:19:14 +02:00
|
|
|
import { AppIdService } from 'jslib/services/appId.service';
|
2018-05-15 05:40:11 +02:00
|
|
|
import { AuditService } from 'jslib/services/audit.service';
|
2018-05-14 17:15:54 +02:00
|
|
|
import { CipherService } from 'jslib/services/cipher.service';
|
|
|
|
import { CollectionService } from 'jslib/services/collection.service';
|
|
|
|
import { ConstantsService } from 'jslib/services/constants.service';
|
2018-05-15 05:40:11 +02:00
|
|
|
import { ContainerService } from 'jslib/services/container.service';
|
|
|
|
import { CryptoService } from 'jslib/services/crypto.service';
|
|
|
|
import { EnvironmentService } from 'jslib/services/environment.service';
|
2018-05-17 16:58:30 +02:00
|
|
|
import { ExportService } from 'jslib/services/export.service';
|
2018-05-15 05:40:11 +02:00
|
|
|
import { FolderService } from 'jslib/services/folder.service';
|
2018-08-06 15:38:17 +02:00
|
|
|
import { ImportService } from 'jslib/services/import.service';
|
2018-05-15 05:40:11 +02:00
|
|
|
import { LockService } from 'jslib/services/lock.service';
|
2018-05-31 15:08:54 +02:00
|
|
|
import { LowdbStorageService } from 'jslib/services/lowdbStorage.service';
|
2018-05-15 05:57:02 +02:00
|
|
|
import { NodeApiService } from 'jslib/services/nodeApi.service';
|
2018-05-15 05:40:11 +02:00
|
|
|
import { NodeCryptoFunctionService } from 'jslib/services/nodeCryptoFunction.service';
|
2019-03-16 03:34:59 +01:00
|
|
|
import { NoopMessagingService } from 'jslib/services/noopMessaging.service';
|
2018-05-14 17:15:54 +02:00
|
|
|
import { PasswordGenerationService } from 'jslib/services/passwordGeneration.service';
|
2018-08-13 20:38:04 +02:00
|
|
|
import { SearchService } from 'jslib/services/search.service';
|
2018-05-15 05:40:11 +02:00
|
|
|
import { SettingsService } from 'jslib/services/settings.service';
|
|
|
|
import { SyncService } from 'jslib/services/sync.service';
|
|
|
|
import { TokenService } from 'jslib/services/token.service';
|
2018-05-14 17:15:54 +02:00
|
|
|
import { TotpService } from 'jslib/services/totp.service';
|
2018-05-15 05:40:11 +02:00
|
|
|
import { UserService } from 'jslib/services/user.service';
|
2018-05-13 06:19:14 +02:00
|
|
|
|
2018-05-14 17:15:54 +02:00
|
|
|
import { Program } from './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;
|
|
|
|
lockService: LockService;
|
|
|
|
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;
|
|
|
|
program: Program;
|
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;
|
|
|
|
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');
|
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);
|
2018-05-14 17:15:54 +02:00
|
|
|
this.cryptoFunctionService = new NodeCryptoFunctionService();
|
2018-06-05 20:44:51 +02:00
|
|
|
this.storageService = new LowdbStorageService(null, p, true);
|
2018-05-16 03:11:58 +02:00
|
|
|
this.secureStorageService = new NodeEnvSecureStorageService(this.storageService, () => this.cryptoService);
|
|
|
|
this.cryptoService = new CryptoService(this.storageService, this.secureStorageService,
|
|
|
|
this.cryptoFunctionService);
|
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();
|
2018-05-15 05:57:02 +02:00
|
|
|
this.apiService = new NodeApiService(this.tokenService, this.platformUtilsService,
|
2018-05-16 05:40:40 +02:00
|
|
|
async (expired: boolean) => await this.logout());
|
2018-08-20 23:10:18 +02:00
|
|
|
this.environmentService = new EnvironmentService(this.apiService, this.storageService, null);
|
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);
|
|
|
|
this.cipherService = new CipherService(this.cryptoService, this.userService, this.settingsService,
|
2018-10-14 04:27:33 +02:00
|
|
|
this.apiService, this.storageService, this.i18nService, null);
|
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);
|
2018-08-13 20:38:04 +02:00
|
|
|
this.searchService = new SearchService(this.cipherService, this.platformUtilsService);
|
2018-05-14 17:15:54 +02:00
|
|
|
this.lockService = new LockService(this.cipherService, this.folderService, this.collectionService,
|
2018-08-13 20:38:04 +02:00
|
|
|
this.cryptoService, this.platformUtilsService, this.storageService, this.messagingService,
|
2019-03-19 20:53:02 +01:00
|
|
|
this.searchService, this.userService, 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,
|
2018-05-16 05:40:40 +02:00
|
|
|
this.storageService, this.messagingService, async (expired: boolean) => await this.logout());
|
2018-05-14 19:37:52 +02:00
|
|
|
this.passwordGenerationService = new PasswordGenerationService(this.cryptoService, this.storageService);
|
|
|
|
this.totpService = new TotpService(this.storageService, this.cryptoFunctionService);
|
2018-08-06 15:38:17 +02:00
|
|
|
this.importService = new ImportService(this.cipherService, this.folderService, this.apiService,
|
2018-08-06 17:43:07 +02:00
|
|
|
this.i18nService, this.collectionService);
|
2018-07-05 20:42:33 +02:00
|
|
|
this.exportService = new ExportService(this.folderService, this.cipherService, this.apiService);
|
2018-05-14 17:15:54 +02:00
|
|
|
this.authService = new AuthService(this.cryptoService, this.apiService, this.userService, this.tokenService,
|
|
|
|
this.appIdService, this.i18nService, this.platformUtilsService, this.messagingService, 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);
|
|
|
|
}
|
2018-05-11 19:47:18 +02:00
|
|
|
|
2018-05-15 05:16:59 +02:00
|
|
|
async run() {
|
|
|
|
await this.init();
|
|
|
|
this.program.run();
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
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() {
|
2018-05-31 15:08:54 +02:00
|
|
|
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);
|
|
|
|
const currentVersion = this.platformUtilsService.getApplicationVersion();
|
|
|
|
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();
|