using System; using System.IO; using System.Threading.Tasks; using Bit.App.Abstractions; using Bit.App.Controls; using Bit.App.Models; using Bit.App.Pages; using Bit.App.Resources; using Bit.App.Services; using Bit.App.Utilities; using Bit.App.Utilities.AccountManagement; using Bit.Core.Abstractions; using Bit.Core.Services; using Bit.Core.Utilities; using Bit.iOS.Core.Services; using CoreNFC; using Foundation; using UIKit; using Xamarin.Forms; namespace Bit.iOS.Core.Utilities { public static class iOSCoreHelpers { public static string AppId = "com.8bit.bitwarden"; public static string AppAutofillId = "com.8bit.bitwarden.autofill"; public static string AppExtensionId = "com.8bit.bitwarden.find-login-action-extension"; public static string AppGroupId = "group.com.8bit.bitwarden"; public static string AccessGroup = "LTZ2PFU5D6.com.8bit.bitwarden"; public static void InitApp(T rootController, string clearCipherCacheKey, NFCNdefReaderSession nfcSession, out NFCReaderDelegate nfcDelegate, out IAccountsManager accountsManager) where T : UIViewController, IAccountsManagerHost { Forms.Init(); if (ServiceContainer.RegisteredServices.Count > 0) { ServiceContainer.Reset(); } RegisterLocalServices(); var deviceActionService = ServiceContainer.Resolve("deviceActionService"); var messagingService = ServiceContainer.Resolve("messagingService"); ServiceContainer.Init(deviceActionService.DeviceUserAgent, clearCipherCacheKey, Bit.Core.Constants.iOSAllClearCipherCacheKeys); InitLogger(); RegisterFinallyBeforeBootstrap(); Bootstrap(); var appOptions = new AppOptions { IosExtension = true }; var app = new App.App(appOptions); ThemeManager.SetTheme(app.Resources); AppearanceAdjustments(); nfcDelegate = new Core.NFCReaderDelegate((success, message) => messagingService.Send("gotYubiKeyOTP", message)); SubscribeBroadcastReceiver(rootController, nfcSession, nfcDelegate); accountsManager = ServiceContainer.Resolve("accountsManager"); accountsManager.Init(() => appOptions, rootController); } public static void InitLogger() { ServiceContainer.Resolve("logger").InitAsync(); } public static void RegisterLocalServices() { if (ServiceContainer.Resolve("nativeLogService", true) == null) { ServiceContainer.Register("nativeLogService", new ConsoleLogService()); } ILogger logger = null; if (ServiceContainer.Resolve("logger", true) == null) { #if DEBUG logger = DebugLogger.Instance; #else logger = Logger.Instance; #endif ServiceContainer.Register("logger", logger); } var preferencesStorage = new PreferencesStorageService(AppGroupId); var appGroupContainer = new NSFileManager().GetContainerUrl(AppGroupId); var liteDbStorage = new LiteDbStorageService( Path.Combine(appGroupContainer.Path, "Library", "bitwarden.db")); var localizeService = new LocalizeService(); var broadcasterService = new BroadcasterService(logger); var messagingService = new MobileBroadcasterMessagingService(broadcasterService); var i18nService = new MobileI18nService(localizeService.GetCurrentCultureInfo()); var secureStorageService = new KeyChainStorageService(AppId, AccessGroup, () => ServiceContainer.Resolve("appIdService").GetAppIdAsync()); var cryptoPrimitiveService = new CryptoPrimitiveService(); var mobileStorageService = new MobileStorageService(preferencesStorage, liteDbStorage); var stateService = new StateService(mobileStorageService, secureStorageService, messagingService); var stateMigrationService = new StateMigrationService(liteDbStorage, preferencesStorage, secureStorageService); var deviceActionService = new DeviceActionService(); var fileService = new FileService(stateService, messagingService); var clipboardService = new ClipboardService(stateService); var platformUtilsService = new MobilePlatformUtilsService(deviceActionService, clipboardService, messagingService, broadcasterService); var biometricService = new BiometricService(mobileStorageService); var cryptoFunctionService = new PclCryptoFunctionService(cryptoPrimitiveService); var cryptoService = new CryptoService(stateService, cryptoFunctionService); var passwordRepromptService = new MobilePasswordRepromptService(platformUtilsService, cryptoService); ServiceContainer.Register("broadcasterService", broadcasterService); ServiceContainer.Register("messagingService", messagingService); ServiceContainer.Register("localizeService", localizeService); ServiceContainer.Register("i18nService", i18nService); ServiceContainer.Register("cryptoPrimitiveService", cryptoPrimitiveService); ServiceContainer.Register("storageService", mobileStorageService); ServiceContainer.Register("secureStorageService", secureStorageService); ServiceContainer.Register("stateService", stateService); ServiceContainer.Register("stateMigrationService", stateMigrationService); ServiceContainer.Register("deviceActionService", deviceActionService); ServiceContainer.Register(fileService); ServiceContainer.Register(new AutofillHandler()); ServiceContainer.Register("clipboardService", clipboardService); ServiceContainer.Register("platformUtilsService", platformUtilsService); ServiceContainer.Register("biometricService", biometricService); ServiceContainer.Register("cryptoFunctionService", cryptoFunctionService); ServiceContainer.Register("cryptoService", cryptoService); ServiceContainer.Register("passwordRepromptService", passwordRepromptService); ServiceContainer.Register("avatarImageSourcePool", new AvatarImageSourcePool()); } public static void RegisterFinallyBeforeBootstrap() { ServiceContainer.Register(new WatchDeviceService(ServiceContainer.Resolve(), ServiceContainer.Resolve(), ServiceContainer.Resolve(), ServiceContainer.Resolve())); } public static void Bootstrap(Func postBootstrapFunc = null) { (ServiceContainer.Resolve("i18nService") as MobileI18nService).Init(); ServiceContainer.Resolve("authService").Init(); (ServiceContainer. Resolve("platformUtilsService") as MobilePlatformUtilsService).Init(); // Note: This is not awaited var bootstrapTask = BootstrapAsync(postBootstrapFunc); } public static void AppearanceAdjustments() { ThemeHelpers.SetAppearance(ThemeManager.GetTheme(), ThemeManager.OsDarkModeEnabled()); UIApplication.SharedApplication.StatusBarHidden = false; UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent; } public static void SubscribeBroadcastReceiver(UIViewController controller, NFCNdefReaderSession nfcSession, NFCReaderDelegate nfcDelegate) { var broadcasterService = ServiceContainer.Resolve("broadcasterService"); var messagingService = ServiceContainer.Resolve("messagingService"); var deviceActionService = ServiceContainer.Resolve("deviceActionService"); broadcasterService.Subscribe(nameof(controller), (message) => { if (message.Command == "showDialog") { var details = message.Data as DialogDetails; var confirmText = string.IsNullOrWhiteSpace(details.ConfirmText) ? AppResources.Ok : details.ConfirmText; NSRunLoop.Main.BeginInvokeOnMainThread(async () => { var result = await deviceActionService.DisplayAlertAsync(details.Title, details.Text, details.CancelText, confirmText); var confirmed = result == details.ConfirmText; messagingService.Send("showDialogResolve", new Tuple(details.DialogId, confirmed)); }); } else if (message.Command == "listenYubiKeyOTP") { ListenYubiKey((bool)message.Data, deviceActionService, nfcSession, nfcDelegate); } }); } public static void ListenYubiKey(bool listen, IDeviceActionService deviceActionService, NFCNdefReaderSession nfcSession, NFCReaderDelegate nfcDelegate) { if (deviceActionService.SupportsNfc()) { nfcSession?.InvalidateSession(); nfcSession?.Dispose(); nfcSession = null; if (listen) { nfcSession = new NFCNdefReaderSession(nfcDelegate, null, true) { AlertMessage = AppResources.HoldYubikeyNearTop }; nfcSession.BeginSession(); } } } private static async Task BootstrapAsync(Func postBootstrapFunc = null) { await ServiceContainer.Resolve("environmentService").SetUrlsFromStorageAsync(); InitializeAppSetup(); // TODO: Update when https://github.com/bitwarden/mobile/pull/1662 gets merged var deleteAccountActionFlowExecutioner = new DeleteAccountActionFlowExecutioner( ServiceContainer.Resolve("apiService"), ServiceContainer.Resolve("messagingService"), ServiceContainer.Resolve("platformUtilsService"), ServiceContainer.Resolve("deviceActionService"), ServiceContainer.Resolve("logger")); ServiceContainer.Register("deleteAccountActionFlowExecutioner", deleteAccountActionFlowExecutioner); var verificationActionsFlowHelper = new VerificationActionsFlowHelper( ServiceContainer.Resolve("keyConnectorService"), ServiceContainer.Resolve("passwordRepromptService"), ServiceContainer.Resolve("cryptoService")); ServiceContainer.Register("verificationActionsFlowHelper", verificationActionsFlowHelper); var accountsManager = new AccountsManager( ServiceContainer.Resolve("broadcasterService"), ServiceContainer.Resolve("vaultTimeoutService"), ServiceContainer.Resolve("secureStorageService"), ServiceContainer.Resolve("stateService"), ServiceContainer.Resolve("platformUtilsService"), ServiceContainer.Resolve("authService"), ServiceContainer.Resolve("logger"), ServiceContainer.Resolve("messagingService"), ServiceContainer.Resolve()); ServiceContainer.Register("accountsManager", accountsManager); if (postBootstrapFunc != null) { await postBootstrapFunc.Invoke(); } } private static void InitializeAppSetup() { var appSetup = new AppSetup(); appSetup.InitializeServicesLastChance(); ServiceContainer.Register("appSetup", appSetup); } } }