bitwarden-estensione-browser/src/bw.ts

148 lines
7.5 KiB
TypeScript
Raw Normal View History

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';
2018-05-15 05:40:11 +02:00
import { NodePlatformUtilsService } from './services/nodePlatformUtils.service';
2018-05-16 05:26:36 +02:00
import { NoopMessagingService } from './services/noopMessaging.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';
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';
2018-05-14 17:15:54 +02:00
import { PasswordGenerationService } from 'jslib/services/passwordGeneration.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
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;
platformUtilsService: NodePlatformUtilsService;
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-05-17 16:58:30 +02:00
exportService: ExportService;
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');
} else {
p = path.join(process.env.HOME, '.config/Bitwarden CLI');
}
2018-05-15 05:16:59 +02:00
this.i18nService = new I18nService('en', './locales');
2018-05-14 17:15:54 +02:00
this.platformUtilsService = new NodePlatformUtilsService();
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-05-14 17:15:54 +02:00
this.environmentService = new EnvironmentService(this.apiService, this.storageService);
this.userService = new UserService(this.tokenService, this.storageService);
this.containerService = new ContainerService(this.cryptoService, this.platformUtilsService);
this.settingsService = new SettingsService(this.userService, this.storageService);
this.cipherService = new CipherService(this.cryptoService, this.userService, this.settingsService,
this.apiService, this.storageService, this.i18nService, this.platformUtilsService);
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);
this.lockService = new LockService(this.cipherService, this.folderService, this.collectionService,
2018-05-16 16:25:25 +02:00
this.cryptoService, this.platformUtilsService, this.storageService, this.messagingService, 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-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-05-17 17:07:53 +02:00
this.auditService = new AuditService(this.cryptoFunctionService);
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();
const locale = await this.storageService.get<string>(ConstantsService.localeKey);
await this.i18nService.init(locale);
await 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();