bitwarden-estensione-browser/src/app/services/services.module.ts

222 lines
13 KiB
TypeScript
Raw Normal View History

2018-06-05 21:02:53 +02:00
import {
APP_INITIALIZER,
2018-07-24 03:47:08 +02:00
LOCALE_ID,
2018-06-05 21:02:53 +02:00
NgModule,
} from '@angular/core';
import { ToasterModule } from 'angular2-toaster';
2018-06-10 04:40:53 +02:00
import { BroadcasterMessagingService } from '../../services/broadcasterMessaging.service';
import { HtmlStorageService } from '../../services/htmlStorage.service';
import { I18nService } from '../../services/i18n.service';
import { MemoryStorageService } from '../../services/memoryStorage.service';
import { WebPlatformUtilsService } from '../../services/webPlatformUtils.service';
2018-06-05 21:02:53 +02:00
2018-07-07 05:08:10 +02:00
import { EventService } from './event.service';
2018-07-05 21:27:23 +02:00
import { OrganizationGuardService } from './organization-guard.service';
import { OrganizationTypeGuardService } from './organization-type-guard.service';
2018-06-10 04:40:53 +02:00
import { RouterService } from './router.service';
import { UnauthGuardService } from './unauth-guard.service';
2018-06-10 04:02:45 +02:00
2018-06-05 21:02:53 +02:00
import { AuthGuardService } from 'jslib/angular/services/auth-guard.service';
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
import { ValidationService } from 'jslib/angular/services/validation.service';
import { Analytics } from 'jslib/misc/analytics';
import { ApiService } from 'jslib/services/api.service';
import { AppIdService } from 'jslib/services/appId.service';
import { AuditService } from 'jslib/services/audit.service';
import { AuthService } from 'jslib/services/auth.service';
import { CipherService } from 'jslib/services/cipher.service';
import { CollectionService } from 'jslib/services/collection.service';
import { ConstantsService } from 'jslib/services/constants.service';
import { ContainerService } from 'jslib/services/container.service';
import { CryptoService } from 'jslib/services/crypto.service';
import { EnvironmentService } from 'jslib/services/environment.service';
import { ExportService } from 'jslib/services/export.service';
import { FolderService } from 'jslib/services/folder.service';
2018-08-06 17:40:12 +02:00
import { ImportService } from 'jslib/services/import.service';
2018-06-05 21:02:53 +02:00
import { LockService } from 'jslib/services/lock.service';
2018-08-21 04:21:13 +02:00
import { NotificationsService } from 'jslib/services/notifications.service';
2018-06-05 21:02:53 +02:00
import { PasswordGenerationService } from 'jslib/services/passwordGeneration.service';
2018-08-13 22:27:17 +02:00
import { SearchService } from 'jslib/services/search.service';
2018-06-05 21:02:53 +02:00
import { SettingsService } from 'jslib/services/settings.service';
import { StateService } from 'jslib/services/state.service';
import { SyncService } from 'jslib/services/sync.service';
import { TokenService } from 'jslib/services/token.service';
import { TotpService } from 'jslib/services/totp.service';
import { UserService } from 'jslib/services/user.service';
import { WebCryptoFunctionService } from 'jslib/services/webCryptoFunction.service';
import { ApiService as ApiServiceAbstraction } from 'jslib/abstractions/api.service';
import { AppIdService as AppIdServiceAbstraction } from 'jslib/abstractions/appId.service';
import { AuditService as AuditServiceAbstraction } from 'jslib/abstractions/audit.service';
import { AuthService as AuthServiceAbstraction } from 'jslib/abstractions/auth.service';
import { CipherService as CipherServiceAbstraction } from 'jslib/abstractions/cipher.service';
import { CollectionService as CollectionServiceAbstraction } from 'jslib/abstractions/collection.service';
import { CryptoService as CryptoServiceAbstraction } from 'jslib/abstractions/crypto.service';
import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from 'jslib/abstractions/cryptoFunction.service';
import { EnvironmentService as EnvironmentServiceAbstraction } from 'jslib/abstractions/environment.service';
import { ExportService as ExportServiceAbstraction } from 'jslib/abstractions/export.service';
import { FolderService as FolderServiceAbstraction } from 'jslib/abstractions/folder.service';
import { I18nService as I18nServiceAbstraction } from 'jslib/abstractions/i18n.service';
2018-08-06 17:40:12 +02:00
import { ImportService as ImportServiceAbstraction } from 'jslib/abstractions/import.service';
2018-06-05 21:02:53 +02:00
import { LockService as LockServiceAbstraction } from 'jslib/abstractions/lock.service';
import { LogService as LogServiceAbstraction } from 'jslib/abstractions/log.service';
import { MessagingService as MessagingServiceAbstraction } from 'jslib/abstractions/messaging.service';
2018-08-21 04:21:13 +02:00
import { NotificationsService as NotificationsServiceAbstraction } from 'jslib/abstractions/notifications.service';
2018-06-05 21:02:53 +02:00
import {
PasswordGenerationService as PasswordGenerationServiceAbstraction,
} from 'jslib/abstractions/passwordGeneration.service';
import { PlatformUtilsService as PlatformUtilsServiceAbstraction } from 'jslib/abstractions/platformUtils.service';
2018-08-13 22:27:17 +02:00
import { SearchService as SearchServiceAbstraction } from 'jslib/abstractions/search.service';
2018-06-05 21:02:53 +02:00
import { SettingsService as SettingsServiceAbstraction } from 'jslib/abstractions/settings.service';
import { StateService as StateServiceAbstraction } from 'jslib/abstractions/state.service';
import { StorageService as StorageServiceAbstraction } from 'jslib/abstractions/storage.service';
import { SyncService as SyncServiceAbstraction } from 'jslib/abstractions/sync.service';
import { TokenService as TokenServiceAbstraction } from 'jslib/abstractions/token.service';
import { TotpService as TotpServiceAbstraction } from 'jslib/abstractions/totp.service';
import { UserService as UserServiceAbstraction } from 'jslib/abstractions/user.service';
const i18nService = new I18nService(window.navigator.language, 'locales');
const stateService = new StateService();
const broadcasterService = new BroadcasterService();
2018-06-08 23:08:19 +02:00
const messagingService = new BroadcasterMessagingService(broadcasterService);
const platformUtilsService = new WebPlatformUtilsService(i18nService, messagingService);
2018-07-28 03:52:36 +02:00
const storageService: StorageServiceAbstraction = new HtmlStorageService(platformUtilsService);
2018-06-09 21:26:21 +02:00
const secureStorageService: StorageServiceAbstraction = new MemoryStorageService();
2018-06-05 21:02:53 +02:00
const cryptoFunctionService: CryptoFunctionServiceAbstraction = new WebCryptoFunctionService(window,
platformUtilsService);
const cryptoService = new CryptoService(storageService,
platformUtilsService.isDev() ? storageService : secureStorageService, cryptoFunctionService);
2018-06-05 21:02:53 +02:00
const tokenService = new TokenService(storageService);
const appIdService = new AppIdService(storageService);
const apiService = new ApiService(tokenService, platformUtilsService,
async (expired: boolean) => messagingService.send('logout', { expired: expired }));
const userService = new UserService(tokenService, storageService);
const settingsService = new SettingsService(userService, storageService);
2018-08-13 22:27:17 +02:00
export let searchService: SearchService = null;
2018-06-05 21:02:53 +02:00
const cipherService = new CipherService(cryptoService, userService, settingsService,
2018-10-14 05:26:38 +02:00
apiService, storageService, i18nService, () => searchService);
2018-06-25 21:20:18 +02:00
const folderService = new FolderService(cryptoService, userService, apiService, storageService,
i18nService, cipherService);
2018-06-05 21:02:53 +02:00
const collectionService = new CollectionService(cryptoService, userService, storageService, i18nService);
2018-08-13 22:27:17 +02:00
searchService = new SearchService(cipherService, platformUtilsService);
2018-06-05 21:02:53 +02:00
const lockService = new LockService(cipherService, folderService, collectionService,
2018-08-13 22:27:17 +02:00
cryptoService, platformUtilsService, storageService, messagingService, searchService, null);
2018-06-05 21:02:53 +02:00
const syncService = new SyncService(userService, apiService, settingsService,
folderService, cipherService, cryptoService, collectionService, storageService, messagingService,
async (expired: boolean) => messagingService.send('logout', { expired: expired }));
const passwordGenerationService = new PasswordGenerationService(cryptoService, storageService);
const totpService = new TotpService(storageService, cryptoFunctionService);
2018-10-14 05:26:38 +02:00
const containerService = new ContainerService(cryptoService);
2018-06-05 21:02:53 +02:00
const authService = new AuthService(cryptoService, apiService,
userService, tokenService, appIdService, i18nService, platformUtilsService, messagingService);
2018-07-05 20:40:53 +02:00
const exportService = new ExportService(folderService, cipherService, apiService);
2018-08-06 17:40:12 +02:00
const importService = new ImportService(cipherService, folderService, apiService, i18nService, collectionService);
2018-08-23 04:37:55 +02:00
const notificationsService = new NotificationsService(userService, syncService, appIdService,
2019-02-14 03:58:16 +01:00
apiService, lockService, async () => messagingService.send('logout', { expired: true }));
2018-08-21 04:21:13 +02:00
const environmentService = new EnvironmentService(apiService, storageService, notificationsService);
2018-07-08 05:50:06 +02:00
const auditService = new AuditService(cryptoFunctionService, apiService);
2018-06-05 21:02:53 +02:00
2018-07-20 01:49:48 +02:00
const analytics = new Analytics(window, () => platformUtilsService.isDev() || platformUtilsService.isSelfHost(),
2018-06-05 21:02:53 +02:00
platformUtilsService, storageService, appIdService);
containerService.attachToWindow(window);
export function initFactory(): Function {
return async () => {
await (storageService as HtmlStorageService).init();
2018-10-08 20:26:10 +02:00
const isDev = platformUtilsService.isDev();
if (!isDev && platformUtilsService.isSelfHost()) {
environmentService.baseUrl = window.location.origin;
} else {
environmentService.notificationsUrl = isDev ? 'http://localhost:61840' :
2018-08-24 03:45:06 +02:00
'https://notifications.bitwarden.com'; // window.location.origin + '/notifications';
}
2018-10-04 17:58:19 +02:00
apiService.setUrls({
2018-06-22 04:10:20 +02:00
base: isDev ? null : window.location.origin,
api: isDev ? 'http://localhost:4000' : null,
identity: isDev ? 'http://localhost:33656' : null,
2018-07-26 03:41:55 +02:00
// Uncomment these (and comment out the above) if you want to target production
// servers for local development.
// base: null,
// api: 'https://api.bitwarden.com',
// identity: 'https://identity.bitwarden.com',
2018-06-22 04:10:20 +02:00
});
2018-08-21 04:21:13 +02:00
setTimeout(() => notificationsService.init(environmentService), 3000);
2018-06-22 03:32:17 +02:00
2018-06-05 21:02:53 +02:00
lockService.init(true);
const locale = await storageService.get<string>(ConstantsService.localeKey);
await i18nService.init(locale);
2018-10-04 17:58:19 +02:00
authService.init();
2018-06-05 21:02:53 +02:00
const htmlEl = window.document.documentElement;
htmlEl.classList.add('locale_' + i18nService.translationLocale);
let theme = await storageService.get<string>(ConstantsService.themeKey);
if (theme == null) {
theme = 'light';
}
htmlEl.classList.add('theme_' + theme);
stateService.save(ConstantsService.disableFaviconKey,
await storageService.get<boolean>(ConstantsService.disableFaviconKey));
2018-07-31 06:02:09 +02:00
stateService.save('enableGravatars', await storageService.get<boolean>('enableGravatars'));
2018-06-05 21:02:53 +02:00
};
}
@NgModule({
imports: [
ToasterModule,
],
declarations: [],
providers: [
ValidationService,
AuthGuardService,
2018-07-05 21:27:23 +02:00
OrganizationGuardService,
OrganizationTypeGuardService,
2018-06-10 04:02:45 +02:00
UnauthGuardService,
2018-06-10 04:40:53 +02:00
RouterService,
2018-07-07 05:08:10 +02:00
EventService,
2018-06-05 21:02:53 +02:00
{ provide: AuditServiceAbstraction, useValue: auditService },
{ provide: AuthServiceAbstraction, useValue: authService },
{ provide: CipherServiceAbstraction, useValue: cipherService },
{ provide: FolderServiceAbstraction, useValue: folderService },
{ provide: CollectionServiceAbstraction, useValue: collectionService },
{ provide: EnvironmentServiceAbstraction, useValue: environmentService },
{ provide: TotpServiceAbstraction, useValue: totpService },
{ provide: TokenServiceAbstraction, useValue: tokenService },
{ provide: I18nServiceAbstraction, useValue: i18nService },
{ provide: CryptoServiceAbstraction, useValue: cryptoService },
{ provide: PlatformUtilsServiceAbstraction, useValue: platformUtilsService },
{ provide: PasswordGenerationServiceAbstraction, useValue: passwordGenerationService },
{ provide: ApiServiceAbstraction, useValue: apiService },
{ provide: SyncServiceAbstraction, useValue: syncService },
{ provide: UserServiceAbstraction, useValue: userService },
{ provide: MessagingServiceAbstraction, useValue: messagingService },
{ provide: BroadcasterService, useValue: broadcasterService },
{ provide: SettingsServiceAbstraction, useValue: settingsService },
{ provide: LockServiceAbstraction, useValue: lockService },
{ provide: StorageServiceAbstraction, useValue: storageService },
{ provide: StateServiceAbstraction, useValue: stateService },
{ provide: ExportServiceAbstraction, useValue: exportService },
2018-08-13 22:27:17 +02:00
{ provide: SearchServiceAbstraction, useValue: searchService },
2018-08-06 17:40:12 +02:00
{ provide: ImportServiceAbstraction, useValue: importService },
2018-08-21 04:21:13 +02:00
{ provide: NotificationsServiceAbstraction, useValue: notificationsService },
2018-07-31 05:52:04 +02:00
{ provide: CryptoFunctionServiceAbstraction, useValue: cryptoFunctionService },
2018-06-05 21:02:53 +02:00
{
provide: APP_INITIALIZER,
useFactory: initFactory,
deps: [],
multi: true,
},
2018-07-24 03:47:08 +02:00
{
provide: LOCALE_ID,
useFactory: () => i18nService.translationLocale,
deps: [],
},
2018-06-05 21:02:53 +02:00
],
})
export class ServicesModule {
}