2019-04-11 20:14:34 +02:00
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Utilities
|
|
|
|
|
{
|
|
|
|
|
public static class ServiceContainer
|
|
|
|
|
{
|
|
|
|
|
public static Dictionary<string, object> RegisteredServices { get; set; } = new Dictionary<string, object>();
|
|
|
|
|
public static bool Inited { get; set; }
|
|
|
|
|
|
2019-09-04 17:52:32 +02:00
|
|
|
|
public static void Init(string customUserAgent = null)
|
2019-04-11 20:14:34 +02:00
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (Inited)
|
2019-04-11 20:14:34 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Inited = true;
|
|
|
|
|
|
|
|
|
|
var platformUtilsService = Resolve<IPlatformUtilsService>("platformUtilsService");
|
|
|
|
|
var storageService = Resolve<IStorageService>("storageService");
|
|
|
|
|
var secureStorageService = Resolve<IStorageService>("secureStorageService");
|
|
|
|
|
var cryptoPrimitiveService = Resolve<ICryptoPrimitiveService>("cryptoPrimitiveService");
|
2019-04-18 16:40:57 +02:00
|
|
|
|
var i18nService = Resolve<II18nService>("i18nService");
|
|
|
|
|
var messagingService = Resolve<IMessagingService>("messagingService");
|
2019-04-24 22:50:34 +02:00
|
|
|
|
SearchService searchService = null;
|
2019-04-11 20:14:34 +02:00
|
|
|
|
|
|
|
|
|
var stateService = new StateService();
|
|
|
|
|
var cryptoFunctionService = new PclCryptoFunctionService(cryptoPrimitiveService);
|
|
|
|
|
var cryptoService = new CryptoService(storageService, secureStorageService, cryptoFunctionService);
|
|
|
|
|
var tokenService = new TokenService(storageService);
|
2019-10-22 22:30:28 +02:00
|
|
|
|
var apiService = new ApiService(tokenService, platformUtilsService, (bool expired) =>
|
|
|
|
|
{
|
|
|
|
|
messagingService.Send("logout", expired);
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
}, customUserAgent);
|
2019-04-11 20:14:34 +02:00
|
|
|
|
var appIdService = new AppIdService(storageService);
|
|
|
|
|
var userService = new UserService(storageService, tokenService);
|
2019-04-18 16:40:57 +02:00
|
|
|
|
var settingsService = new SettingsService(userService, storageService);
|
|
|
|
|
var cipherService = new CipherService(cryptoService, userService, settingsService, apiService,
|
2019-04-24 20:52:26 +02:00
|
|
|
|
storageService, i18nService, () => searchService);
|
2019-04-18 16:40:57 +02:00
|
|
|
|
var folderService = new FolderService(cryptoService, userService, apiService, storageService,
|
|
|
|
|
i18nService, cipherService);
|
|
|
|
|
var collectionService = new CollectionService(cryptoService, userService, storageService, i18nService);
|
2019-04-24 20:52:26 +02:00
|
|
|
|
searchService = new SearchService(cipherService);
|
2020-05-29 18:26:36 +02:00
|
|
|
|
var vaultTimeoutService = new VaultTimeoutService(cryptoService, userService, platformUtilsService,
|
|
|
|
|
storageService, folderService, cipherService, collectionService, searchService, messagingService, tokenService,
|
|
|
|
|
null, (expired) =>
|
|
|
|
|
{
|
|
|
|
|
messagingService.Send("logout", expired);
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
});
|
2020-02-21 16:23:38 +01:00
|
|
|
|
var policyService = new PolicyService(storageService, userService);
|
2019-04-18 16:40:57 +02:00
|
|
|
|
var syncService = new SyncService(userService, apiService, settingsService, folderService,
|
2020-02-21 16:23:38 +01:00
|
|
|
|
cipherService, cryptoService, collectionService, storageService, messagingService, policyService,
|
|
|
|
|
(bool expired) =>
|
2019-10-22 22:30:28 +02:00
|
|
|
|
{
|
|
|
|
|
messagingService.Send("logout", expired);
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
});
|
2019-04-18 16:40:57 +02:00
|
|
|
|
var passwordGenerationService = new PasswordGenerationService(cryptoService, storageService,
|
2020-02-28 01:53:02 +01:00
|
|
|
|
cryptoFunctionService, policyService);
|
2019-04-18 16:40:57 +02:00
|
|
|
|
var totpService = new TotpService(storageService, cryptoFunctionService);
|
|
|
|
|
var authService = new AuthService(cryptoService, apiService, userService, tokenService, appIdService,
|
2020-05-29 18:26:36 +02:00
|
|
|
|
i18nService, platformUtilsService, messagingService, vaultTimeoutService);
|
2020-02-14 22:10:58 +01:00
|
|
|
|
var exportService = new ExportService(folderService, cipherService);
|
2019-04-18 16:40:57 +02:00
|
|
|
|
var auditService = new AuditService(cryptoFunctionService, apiService);
|
2019-04-18 18:21:35 +02:00
|
|
|
|
var environmentService = new EnvironmentService(apiService, storageService);
|
2019-07-12 21:51:47 +02:00
|
|
|
|
var eventService = new EventService(storageService, apiService, userService, cipherService);
|
2019-04-11 20:14:34 +02:00
|
|
|
|
|
|
|
|
|
Register<IStateService>("stateService", stateService);
|
|
|
|
|
Register<ICryptoFunctionService>("cryptoFunctionService", cryptoFunctionService);
|
|
|
|
|
Register<ICryptoService>("cryptoService", cryptoService);
|
|
|
|
|
Register<ITokenService>("tokenService", tokenService);
|
2019-04-18 16:40:57 +02:00
|
|
|
|
Register<IApiService>("apiService", apiService);
|
2019-04-11 20:14:34 +02:00
|
|
|
|
Register<IAppIdService>("appIdService", appIdService);
|
|
|
|
|
Register<IUserService>("userService", userService);
|
2019-04-18 16:40:57 +02:00
|
|
|
|
Register<ISettingsService>("settingsService", settingsService);
|
|
|
|
|
Register<ICipherService>("cipherService", cipherService);
|
|
|
|
|
Register<IFolderService>("folderService", folderService);
|
|
|
|
|
Register<ICollectionService>("collectionService", collectionService);
|
2019-04-24 22:50:34 +02:00
|
|
|
|
Register<ISearchService>("searchService", searchService);
|
2020-02-21 16:23:38 +01:00
|
|
|
|
Register<IPolicyService>("policyService", policyService);
|
2019-04-18 16:40:57 +02:00
|
|
|
|
Register<ISyncService>("syncService", syncService);
|
2020-05-29 18:26:36 +02:00
|
|
|
|
Register<IVaultTimeoutService>("vaultTimeoutService", vaultTimeoutService);
|
2019-04-18 16:40:57 +02:00
|
|
|
|
Register<IPasswordGenerationService>("passwordGenerationService", passwordGenerationService);
|
|
|
|
|
Register<ITotpService>("totpService", totpService);
|
|
|
|
|
Register<IAuthService>("authService", authService);
|
2020-02-14 22:10:58 +01:00
|
|
|
|
Register<IExportService>("exportService", exportService);
|
2019-04-18 16:40:57 +02:00
|
|
|
|
Register<IAuditService>("auditService", auditService);
|
2019-04-18 18:21:35 +02:00
|
|
|
|
Register<IEnvironmentService>("environmentService", environmentService);
|
2019-07-12 21:51:47 +02:00
|
|
|
|
Register<IEventService>("eventService", eventService);
|
2019-04-11 20:14:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Register<T>(string serviceName, T obj)
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (RegisteredServices.ContainsKey(serviceName))
|
2019-04-11 20:14:34 +02:00
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Service {serviceName} has already been registered.");
|
|
|
|
|
}
|
|
|
|
|
RegisteredServices.Add(serviceName, obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T Resolve<T>(string serviceName, bool dontThrow = false)
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (RegisteredServices.ContainsKey(serviceName))
|
2019-04-11 20:14:34 +02:00
|
|
|
|
{
|
|
|
|
|
return (T)RegisteredServices[serviceName];
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (dontThrow)
|
2019-04-11 20:14:34 +02:00
|
|
|
|
{
|
|
|
|
|
return (T)(object)null;
|
|
|
|
|
}
|
|
|
|
|
throw new Exception($"Service {serviceName} is not registered.");
|
|
|
|
|
}
|
2019-06-28 17:47:04 +02:00
|
|
|
|
|
|
|
|
|
public static void Reset()
|
|
|
|
|
{
|
2020-05-28 21:44:27 +02:00
|
|
|
|
foreach (var service in RegisteredServices)
|
|
|
|
|
{
|
|
|
|
|
if (service.Value != null && service.Value is IDisposable disposableService)
|
|
|
|
|
{
|
|
|
|
|
disposableService.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-28 17:47:04 +02:00
|
|
|
|
Inited = false;
|
|
|
|
|
RegisteredServices.Clear();
|
2019-07-03 22:49:47 +02:00
|
|
|
|
RegisteredServices = new Dictionary<string, object>();
|
2019-06-28 17:47:04 +02:00
|
|
|
|
}
|
2019-04-11 20:14:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|