2020-02-10 20:07:06 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2019-06-28 14:21:44 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Bit.App.Abstractions;
|
2020-05-29 18:26:36 +02:00
|
|
|
|
using Bit.App.Models;
|
|
|
|
|
using Bit.App.Resources;
|
2019-06-28 14:21:44 +02:00
|
|
|
|
using Bit.App.Services;
|
|
|
|
|
using Bit.App.Utilities;
|
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using Bit.iOS.Core.Services;
|
2020-05-29 18:26:36 +02:00
|
|
|
|
using CoreNFC;
|
2019-06-28 14:21:44 +02:00
|
|
|
|
using Foundation;
|
|
|
|
|
using UIKit;
|
|
|
|
|
|
|
|
|
|
namespace Bit.iOS.Core.Utilities
|
|
|
|
|
{
|
|
|
|
|
public static class iOSCoreHelpers
|
|
|
|
|
{
|
|
|
|
|
public static string AppId = "com.8bit.bitwarden";
|
2019-07-05 23:10:37 +02:00
|
|
|
|
public static string AppAutofillId = "com.8bit.bitwarden.autofill";
|
|
|
|
|
public static string AppExtensionId = "com.8bit.bitwarden.find-login-action-extension";
|
2019-06-28 14:21:44 +02:00
|
|
|
|
public static string AppGroupId = "group.com.8bit.bitwarden";
|
|
|
|
|
public static string AccessGroup = "LTZ2PFU5D6.com.8bit.bitwarden";
|
|
|
|
|
|
2020-04-02 15:02:38 +02:00
|
|
|
|
public static void RegisterAppCenter()
|
2019-06-28 14:21:44 +02:00
|
|
|
|
{
|
2020-04-02 15:02:38 +02:00
|
|
|
|
var appCenterHelper = new AppCenterHelper(
|
2019-06-28 14:21:44 +02:00
|
|
|
|
ServiceContainer.Resolve<IAppIdService>("appIdService"),
|
|
|
|
|
ServiceContainer.Resolve<IUserService>("userService"));
|
2020-04-02 15:02:38 +02:00
|
|
|
|
var appCenterTask = appCenterHelper.InitAsync();
|
2019-06-28 14:21:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RegisterLocalServices()
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (ServiceContainer.Resolve<ILogService>("logService", true) == null)
|
2019-06-28 14:21:44 +02:00
|
|
|
|
{
|
|
|
|
|
ServiceContainer.Register<ILogService>("logService", new ConsoleLogService());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
var messagingService = new MobileBroadcasterMessagingService(broadcasterService);
|
|
|
|
|
var i18nService = new MobileI18nService(localizeService.GetCurrentCultureInfo());
|
2019-07-04 02:04:23 +02:00
|
|
|
|
var secureStorageService = new KeyChainStorageService(AppId, AccessGroup,
|
|
|
|
|
() => ServiceContainer.Resolve<IAppIdService>("appIdService").GetAppIdAsync());
|
2019-06-28 14:21:44 +02:00
|
|
|
|
var cryptoPrimitiveService = new CryptoPrimitiveService();
|
|
|
|
|
var mobileStorageService = new MobileStorageService(preferencesStorage, liteDbStorage);
|
|
|
|
|
var deviceActionService = new DeviceActionService(mobileStorageService, messagingService);
|
|
|
|
|
var platformUtilsService = new MobilePlatformUtilsService(deviceActionService, messagingService,
|
|
|
|
|
broadcasterService);
|
2020-08-09 03:33:49 +02:00
|
|
|
|
var biometricService = new BiometricService(mobileStorageService);
|
2021-05-21 15:13:54 +02:00
|
|
|
|
var cryptoFunctionService = new PclCryptoFunctionService(cryptoPrimitiveService);
|
|
|
|
|
var cryptoService = new CryptoService(mobileStorageService, secureStorageService, cryptoFunctionService);
|
|
|
|
|
var passwordRepromptService = new MobilePasswordRepromptService(platformUtilsService, cryptoService);
|
2019-06-28 14:21:44 +02:00
|
|
|
|
|
|
|
|
|
ServiceContainer.Register<IBroadcasterService>("broadcasterService", broadcasterService);
|
|
|
|
|
ServiceContainer.Register<IMessagingService>("messagingService", messagingService);
|
|
|
|
|
ServiceContainer.Register<ILocalizeService>("localizeService", localizeService);
|
|
|
|
|
ServiceContainer.Register<II18nService>("i18nService", i18nService);
|
|
|
|
|
ServiceContainer.Register<ICryptoPrimitiveService>("cryptoPrimitiveService", cryptoPrimitiveService);
|
|
|
|
|
ServiceContainer.Register<IStorageService>("storageService", mobileStorageService);
|
|
|
|
|
ServiceContainer.Register<IStorageService>("secureStorageService", secureStorageService);
|
|
|
|
|
ServiceContainer.Register<IDeviceActionService>("deviceActionService", deviceActionService);
|
|
|
|
|
ServiceContainer.Register<IPlatformUtilsService>("platformUtilsService", platformUtilsService);
|
2020-08-09 03:33:49 +02:00
|
|
|
|
ServiceContainer.Register<IBiometricService>("biometricService", biometricService);
|
2021-05-21 15:13:54 +02:00
|
|
|
|
ServiceContainer.Register<ICryptoFunctionService>("cryptoFunctionService", cryptoFunctionService);
|
|
|
|
|
ServiceContainer.Register<ICryptoService>("cryptoService", cryptoService);
|
|
|
|
|
ServiceContainer.Register<IPasswordRepromptService>("passwordRepromptService", passwordRepromptService);
|
2019-06-28 14:21:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 20:07:06 +01:00
|
|
|
|
public static void Bootstrap(Func<Task> postBootstrapFunc = null)
|
2019-06-28 14:21:44 +02:00
|
|
|
|
{
|
|
|
|
|
(ServiceContainer.Resolve<II18nService>("i18nService") as MobileI18nService).Init();
|
|
|
|
|
ServiceContainer.Resolve<IAuthService>("authService").Init();
|
2020-05-29 18:26:36 +02:00
|
|
|
|
(ServiceContainer.
|
|
|
|
|
Resolve<IPlatformUtilsService>("platformUtilsService") as MobilePlatformUtilsService).Init();
|
2019-06-28 14:21:44 +02:00
|
|
|
|
// Note: This is not awaited
|
2020-02-10 20:07:06 +01:00
|
|
|
|
var bootstrapTask = BootstrapAsync(postBootstrapFunc);
|
2019-06-28 14:21:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-08 14:47:40 +02:00
|
|
|
|
public static void AppearanceAdjustments()
|
2019-06-28 14:21:44 +02:00
|
|
|
|
{
|
2021-10-08 14:47:40 +02:00
|
|
|
|
ThemeHelpers.SetAppearance(ThemeManager.GetTheme(false), ThemeManager.OsDarkModeEnabled());
|
2019-06-28 14:21:44 +02:00
|
|
|
|
UIApplication.SharedApplication.StatusBarHidden = false;
|
|
|
|
|
UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 18:26:36 +02:00
|
|
|
|
public static void SubscribeBroadcastReceiver(UIViewController controller, NFCNdefReaderSession nfcSession,
|
|
|
|
|
NFCReaderDelegate nfcDelegate)
|
|
|
|
|
{
|
|
|
|
|
var broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
|
|
|
|
|
var messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
|
|
|
|
|
var deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("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, details.ConfirmText);
|
|
|
|
|
var confirmed = result == details.ConfirmText;
|
|
|
|
|
messagingService.Send("showDialogResolve", new Tuple<int, bool>(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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 20:07:06 +01:00
|
|
|
|
private static async Task BootstrapAsync(Func<Task> postBootstrapFunc = null)
|
2019-06-28 14:21:44 +02:00
|
|
|
|
{
|
|
|
|
|
var disableFavicon = await ServiceContainer.Resolve<IStorageService>("storageService").GetAsync<bool?>(
|
|
|
|
|
Bit.Core.Constants.DisableFaviconKey);
|
|
|
|
|
await ServiceContainer.Resolve<IStateService>("stateService").SaveAsync(
|
|
|
|
|
Bit.Core.Constants.DisableFaviconKey, disableFavicon);
|
|
|
|
|
await ServiceContainer.Resolve<IEnvironmentService>("environmentService").SetUrlsFromStorageAsync();
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (postBootstrapFunc != null)
|
2020-02-10 20:07:06 +01:00
|
|
|
|
{
|
|
|
|
|
await postBootstrapFunc.Invoke();
|
|
|
|
|
}
|
2019-06-28 14:21:44 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-10 20:07:06 +01:00
|
|
|
|
}
|