diff --git a/src/Android/MainActivity.cs b/src/Android/MainActivity.cs index 965707c2b..5aff7d624 100644 --- a/src/Android/MainActivity.cs +++ b/src/Android/MainActivity.cs @@ -34,7 +34,8 @@ namespace Bit.Android Resolver.Resolve(), Resolver.Resolve(), Resolver.Resolve(), - Resolver.Resolve())); + Resolver.Resolve(), + Resolver.Resolve())); } protected override void OnPause() diff --git a/src/Android/MainApplication.cs b/src/Android/MainApplication.cs index e04ee560d..0aa0d78d3 100644 --- a/src/Android/MainApplication.cs +++ b/src/Android/MainApplication.cs @@ -123,6 +123,7 @@ namespace Bit.Android .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) + .RegisterType(new ContainerControlledLifetimeManager()) // Repositories .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) diff --git a/src/App/Abstractions/Services/ILockService.cs b/src/App/Abstractions/Services/ILockService.cs new file mode 100644 index 000000000..5efa7ee32 --- /dev/null +++ b/src/App/Abstractions/Services/ILockService.cs @@ -0,0 +1,9 @@ +using Bit.App.Enums; + +namespace Bit.App.Abstractions +{ + public interface ILockService + { + LockType GetLockType(bool forceLock); + } +} \ No newline at end of file diff --git a/src/App/App.cs b/src/App/App.cs index 76fb9f5bd..5f8936185 100644 --- a/src/App/App.cs +++ b/src/App/App.cs @@ -27,6 +27,7 @@ namespace Bit.App private readonly IFingerprint _fingerprint; private readonly ISettings _settings; private readonly IPushNotification _pushNotification; + private readonly ILockService _lockService; public App( IAuthService authService, @@ -36,7 +37,8 @@ namespace Bit.App ISyncService syncService, IFingerprint fingerprint, ISettings settings, - IPushNotification pushNotification) + IPushNotification pushNotification, + ILockService lockService) { _databaseService = databaseService; _connectivity = connectivity; @@ -46,6 +48,7 @@ namespace Bit.App _fingerprint = fingerprint; _settings = settings; _pushNotification = pushNotification; + _lockService = lockService; SetStyles(); @@ -188,58 +191,33 @@ namespace Bit.App private async Task CheckLockAsync(bool forceLock) { - // Only lock if they are logged in - if(!_authService.IsAuthenticated) - { - return; - } - - // Are we forcing a lock? (i.e. clicking a button to lock the app manually, immediately) - if(!forceLock && !_settings.GetValueOrDefault(Constants.SettingLocked, false)) - { - // Lock seconds tells if if they want to lock the app or not - var lockSeconds = _settings.GetValueOrDefault(Constants.SettingLockSeconds); - if(!lockSeconds.HasValue) - { - return; - } - - // Has it been longer than lockSeconds since the last time the app was backgrounded? - var now = DateTime.UtcNow; - var lastBackground = _settings.GetValueOrDefault(Constants.SettingLastBackgroundedDate, now.AddYears(-1)); - if((now - lastBackground).TotalSeconds < lockSeconds.Value) - { - return; - } - } - - // What method are we using to unlock? - var fingerprintUnlock = _settings.GetValueOrDefault(Constants.SettingFingerprintUnlockOn); - var pinUnlock = _settings.GetValueOrDefault(Constants.SettingPinUnlockOn); + var lockType = _lockService.GetLockType(forceLock); var currentPage = Current.MainPage.Navigation.ModalStack.LastOrDefault() as ExtendedNavigationPage; - if(fingerprintUnlock && _fingerprint.IsAvailable) + switch(lockType) { - if((currentPage?.CurrentPage as LockFingerprintPage) == null) - { - await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(new LockFingerprintPage(!forceLock)), false); - } - } - else if(pinUnlock && !string.IsNullOrWhiteSpace(_authService.PIN)) - { - var lockPinPage = (currentPage?.CurrentPage as LockPinPage); - if(lockPinPage == null) - { - lockPinPage = new LockPinPage(); - await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(lockPinPage), false); - lockPinPage.PinControl.Entry.Focus(); - } - } - else - { - if((currentPage?.CurrentPage as LockPasswordPage) == null) - { - await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(new LockPasswordPage()), false); - } + case Enums.LockType.Fingerprint: + if((currentPage?.CurrentPage as LockFingerprintPage) == null) + { + await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(new LockFingerprintPage(!forceLock)), false); + } + break; + case Enums.LockType.PIN: + var lockPinPage = (currentPage?.CurrentPage as LockPinPage); + if(lockPinPage == null) + { + lockPinPage = new LockPinPage(); + await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(lockPinPage), false); + lockPinPage.PinControl.Entry.Focus(); + } + break; + case Enums.LockType.Password: + if((currentPage?.CurrentPage as LockPasswordPage) == null) + { + await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(new LockPasswordPage()), false); + } + break; + default: + break; } } diff --git a/src/App/App.csproj b/src/App/App.csproj index 33c08fb2e..58da6f503 100644 --- a/src/App/App.csproj +++ b/src/App/App.csproj @@ -69,6 +69,7 @@ + @@ -146,6 +147,8 @@ AppResources.resx + + diff --git a/src/App/Enums/LockType.cs b/src/App/Enums/LockType.cs new file mode 100644 index 000000000..803a39568 --- /dev/null +++ b/src/App/Enums/LockType.cs @@ -0,0 +1,10 @@ +namespace Bit.App.Enums +{ + public enum LockType : short + { + None = 0, + Fingerprint = 1, + PIN = 2, + Password = 3 + } +} diff --git a/src/App/Services/LockService.cs b/src/App/Services/LockService.cs new file mode 100644 index 000000000..592bffb59 --- /dev/null +++ b/src/App/Services/LockService.cs @@ -0,0 +1,69 @@ +using System; +using Bit.App.Abstractions; +using Plugin.Settings.Abstractions; +using Plugin.Fingerprint.Abstractions; +using Bit.App.Enums; + +namespace Bit.App.Services +{ + public class LockService : ILockService + { + private readonly ISettings _settings; + private readonly IAuthService _authService; + private readonly IFingerprint _fingerprint; + + public LockService( + ISettings settings, + IAuthService authService, + IFingerprint fingerprint) + { + _settings = settings; + _authService = authService; + _fingerprint = fingerprint; + } + + public LockType GetLockType(bool forceLock) + { + // Only lock if they are logged in + if(!_authService.IsAuthenticated) + { + return LockType.None; + } + + // Are we forcing a lock? (i.e. clicking a button to lock the app manually, immediately) + if(!forceLock && !_settings.GetValueOrDefault(Constants.SettingLocked, false)) + { + // Lock seconds tells if if they want to lock the app or not + var lockSeconds = _settings.GetValueOrDefault(Constants.SettingLockSeconds); + if(!lockSeconds.HasValue) + { + return LockType.None; + } + + // Has it been longer than lockSeconds since the last time the app was backgrounded? + var now = DateTime.UtcNow; + var lastBackground = _settings.GetValueOrDefault(Constants.SettingLastBackgroundedDate, now.AddYears(-1)); + if((now - lastBackground).TotalSeconds < lockSeconds.Value) + { + return LockType.None; + } + } + + // What method are we using to unlock? + var fingerprintUnlock = _settings.GetValueOrDefault(Constants.SettingFingerprintUnlockOn); + var pinUnlock = _settings.GetValueOrDefault(Constants.SettingPinUnlockOn); + if(fingerprintUnlock && _fingerprint.IsAvailable) + { + return LockType.Fingerprint; + } + else if(pinUnlock && !string.IsNullOrWhiteSpace(_authService.PIN)) + { + return LockType.PIN; + } + else + { + return LockType.Password; + } + } + } +} diff --git a/src/iOS.Extension/LoadingViewController.cs b/src/iOS.Extension/LoadingViewController.cs index 4d2c09add..818914f66 100644 --- a/src/iOS.Extension/LoadingViewController.cs +++ b/src/iOS.Extension/LoadingViewController.cs @@ -98,6 +98,7 @@ namespace Bit.iOS.Extension .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) + .RegisterType(new ContainerControlledLifetimeManager()) // Repositories .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) diff --git a/src/iOS.Extension/SiteListViewController.cs b/src/iOS.Extension/SiteListViewController.cs index c12a888f6..562b4b8be 100644 --- a/src/iOS.Extension/SiteListViewController.cs +++ b/src/iOS.Extension/SiteListViewController.cs @@ -32,18 +32,21 @@ namespace Bit.iOS.Extension { base.ViewDidLoad(); - // TODO: lock logic - if(true) + var lockService = Resolver.Resolve(); + var lockType = lockService.GetLockType(false); + switch(lockType) { - PerformSegue("lockFingerprintSegue", this); - } - else if(true) - { - PerformSegue("lockPinSegue", this); - } - else - { - PerformSegue("lockPasswordSegue", this); + case App.Enums.LockType.Fingerprint: + PerformSegue("lockFingerprintSegue", this); + break; + case App.Enums.LockType.PIN: + PerformSegue("lockPinSegue", this); + break; + case App.Enums.LockType.Password: + PerformSegue("lockPasswordSegue", this); + break; + default: + break; } IEnumerable filteredSiteModels = new List(); diff --git a/src/iOS/AppDelegate.cs b/src/iOS/AppDelegate.cs index 35e01398f..26e6185c5 100644 --- a/src/iOS/AppDelegate.cs +++ b/src/iOS/AppDelegate.cs @@ -50,7 +50,8 @@ namespace Bit.iOS Resolver.Resolve(), Resolver.Resolve(), Resolver.Resolve(), - Resolver.Resolve())); + Resolver.Resolve(), + Resolver.Resolve())); // Appearance stuff @@ -219,6 +220,7 @@ namespace Bit.iOS .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager()) + .RegisterType(new ContainerControlledLifetimeManager()) // Repositories .RegisterType(new ContainerControlledLifetimeManager()) .RegisterType(new ContainerControlledLifetimeManager())