centralized lock logic into a new lock service to be shared to extension

This commit is contained in:
Kyle Spearrin 2016-07-19 23:29:32 -04:00
parent 7fb51b5aa4
commit d0bf141c5d
10 changed files with 141 additions and 64 deletions

View file

@ -34,7 +34,8 @@ namespace Bit.Android
Resolver.Resolve<ISyncService>(), Resolver.Resolve<ISyncService>(),
Resolver.Resolve<IFingerprint>(), Resolver.Resolve<IFingerprint>(),
Resolver.Resolve<ISettings>(), Resolver.Resolve<ISettings>(),
Resolver.Resolve<IPushNotification>())); Resolver.Resolve<IPushNotification>(),
Resolver.Resolve<ILockService>()));
} }
protected override void OnPause() protected override void OnPause()

View file

@ -123,6 +123,7 @@ namespace Bit.Android
.RegisterType<IAppIdService, AppIdService>(new ContainerControlledLifetimeManager()) .RegisterType<IAppIdService, AppIdService>(new ContainerControlledLifetimeManager())
.RegisterType<IPasswordGenerationService, PasswordGenerationService>(new ContainerControlledLifetimeManager()) .RegisterType<IPasswordGenerationService, PasswordGenerationService>(new ContainerControlledLifetimeManager())
.RegisterType<IReflectionService, ReflectionService>(new ContainerControlledLifetimeManager()) .RegisterType<IReflectionService, ReflectionService>(new ContainerControlledLifetimeManager())
.RegisterType<ILockService, LockService>(new ContainerControlledLifetimeManager())
// Repositories // Repositories
.RegisterType<IFolderRepository, FolderRepository>(new ContainerControlledLifetimeManager()) .RegisterType<IFolderRepository, FolderRepository>(new ContainerControlledLifetimeManager())
.RegisterType<IFolderApiRepository, FolderApiRepository>(new ContainerControlledLifetimeManager()) .RegisterType<IFolderApiRepository, FolderApiRepository>(new ContainerControlledLifetimeManager())

View file

@ -0,0 +1,9 @@
using Bit.App.Enums;
namespace Bit.App.Abstractions
{
public interface ILockService
{
LockType GetLockType(bool forceLock);
}
}

View file

@ -27,6 +27,7 @@ namespace Bit.App
private readonly IFingerprint _fingerprint; private readonly IFingerprint _fingerprint;
private readonly ISettings _settings; private readonly ISettings _settings;
private readonly IPushNotification _pushNotification; private readonly IPushNotification _pushNotification;
private readonly ILockService _lockService;
public App( public App(
IAuthService authService, IAuthService authService,
@ -36,7 +37,8 @@ namespace Bit.App
ISyncService syncService, ISyncService syncService,
IFingerprint fingerprint, IFingerprint fingerprint,
ISettings settings, ISettings settings,
IPushNotification pushNotification) IPushNotification pushNotification,
ILockService lockService)
{ {
_databaseService = databaseService; _databaseService = databaseService;
_connectivity = connectivity; _connectivity = connectivity;
@ -46,6 +48,7 @@ namespace Bit.App
_fingerprint = fingerprint; _fingerprint = fingerprint;
_settings = settings; _settings = settings;
_pushNotification = pushNotification; _pushNotification = pushNotification;
_lockService = lockService;
SetStyles(); SetStyles();
@ -188,58 +191,33 @@ namespace Bit.App
private async Task CheckLockAsync(bool forceLock) private async Task CheckLockAsync(bool forceLock)
{ {
// Only lock if they are logged in var lockType = _lockService.GetLockType(forceLock);
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<int?>(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<bool>(Constants.SettingFingerprintUnlockOn);
var pinUnlock = _settings.GetValueOrDefault<bool>(Constants.SettingPinUnlockOn);
var currentPage = Current.MainPage.Navigation.ModalStack.LastOrDefault() as ExtendedNavigationPage; var currentPage = Current.MainPage.Navigation.ModalStack.LastOrDefault() as ExtendedNavigationPage;
if(fingerprintUnlock && _fingerprint.IsAvailable) switch(lockType)
{ {
if((currentPage?.CurrentPage as LockFingerprintPage) == null) case Enums.LockType.Fingerprint:
{ if((currentPage?.CurrentPage as LockFingerprintPage) == null)
await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(new LockFingerprintPage(!forceLock)), false); {
} await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(new LockFingerprintPage(!forceLock)), false);
} }
else if(pinUnlock && !string.IsNullOrWhiteSpace(_authService.PIN)) break;
{ case Enums.LockType.PIN:
var lockPinPage = (currentPage?.CurrentPage as LockPinPage); var lockPinPage = (currentPage?.CurrentPage as LockPinPage);
if(lockPinPage == null) if(lockPinPage == null)
{ {
lockPinPage = new LockPinPage(); lockPinPage = new LockPinPage();
await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(lockPinPage), false); await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(lockPinPage), false);
lockPinPage.PinControl.Entry.Focus(); lockPinPage.PinControl.Entry.Focus();
} }
} break;
else case Enums.LockType.Password:
{ if((currentPage?.CurrentPage as LockPasswordPage) == null)
if((currentPage?.CurrentPage as LockPasswordPage) == null) {
{ await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(new LockPasswordPage()), false);
await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(new LockPasswordPage()), false); }
} break;
default:
break;
} }
} }

View file

@ -69,6 +69,7 @@
<Compile Include="Controls\FormPickerCell.cs" /> <Compile Include="Controls\FormPickerCell.cs" />
<Compile Include="Controls\FormEntryCell.cs" /> <Compile Include="Controls\FormEntryCell.cs" />
<Compile Include="Controls\PinControl.cs" /> <Compile Include="Controls\PinControl.cs" />
<Compile Include="Enums\LockType.cs" />
<Compile Include="Enums\CipherType.cs" /> <Compile Include="Enums\CipherType.cs" />
<Compile Include="Enums\PushType.cs" /> <Compile Include="Enums\PushType.cs" />
<Compile Include="Enums\ReturnType.cs" /> <Compile Include="Enums\ReturnType.cs" />
@ -146,6 +147,8 @@
<DependentUpon>AppResources.resx</DependentUpon> <DependentUpon>AppResources.resx</DependentUpon>
</Compile> </Compile>
<Compile Include="Services\AppIdService.cs" /> <Compile Include="Services\AppIdService.cs" />
<Compile Include="Abstractions\Services\ILockService.cs" />
<Compile Include="Services\LockService.cs" />
<Compile Include="Services\DatabaseService.cs" /> <Compile Include="Services\DatabaseService.cs" />
<Compile Include="Services\FolderService.cs" /> <Compile Include="Services\FolderService.cs" />
<Compile Include="Repositories\Repository.cs" /> <Compile Include="Repositories\Repository.cs" />

10
src/App/Enums/LockType.cs Normal file
View file

@ -0,0 +1,10 @@
namespace Bit.App.Enums
{
public enum LockType : short
{
None = 0,
Fingerprint = 1,
PIN = 2,
Password = 3
}
}

View file

@ -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<int?>(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<bool>(Constants.SettingFingerprintUnlockOn);
var pinUnlock = _settings.GetValueOrDefault<bool>(Constants.SettingPinUnlockOn);
if(fingerprintUnlock && _fingerprint.IsAvailable)
{
return LockType.Fingerprint;
}
else if(pinUnlock && !string.IsNullOrWhiteSpace(_authService.PIN))
{
return LockType.PIN;
}
else
{
return LockType.Password;
}
}
}
}

View file

@ -98,6 +98,7 @@ namespace Bit.iOS.Extension
.RegisterType<ISyncService, SyncService>(new ContainerControlledLifetimeManager()) .RegisterType<ISyncService, SyncService>(new ContainerControlledLifetimeManager())
.RegisterType<IPasswordGenerationService, PasswordGenerationService>(new ContainerControlledLifetimeManager()) .RegisterType<IPasswordGenerationService, PasswordGenerationService>(new ContainerControlledLifetimeManager())
.RegisterType<IAppIdService, AppIdService>(new ContainerControlledLifetimeManager()) .RegisterType<IAppIdService, AppIdService>(new ContainerControlledLifetimeManager())
.RegisterType<ILockService, LockService>(new ContainerControlledLifetimeManager())
// Repositories // Repositories
.RegisterType<IFolderRepository, FolderRepository>(new ContainerControlledLifetimeManager()) .RegisterType<IFolderRepository, FolderRepository>(new ContainerControlledLifetimeManager())
.RegisterType<IFolderApiRepository, FolderApiRepository>(new ContainerControlledLifetimeManager()) .RegisterType<IFolderApiRepository, FolderApiRepository>(new ContainerControlledLifetimeManager())

View file

@ -32,18 +32,21 @@ namespace Bit.iOS.Extension
{ {
base.ViewDidLoad(); base.ViewDidLoad();
// TODO: lock logic var lockService = Resolver.Resolve<ILockService>();
if(true) var lockType = lockService.GetLockType(false);
switch(lockType)
{ {
PerformSegue("lockFingerprintSegue", this); case App.Enums.LockType.Fingerprint:
} PerformSegue("lockFingerprintSegue", this);
else if(true) break;
{ case App.Enums.LockType.PIN:
PerformSegue("lockPinSegue", this); PerformSegue("lockPinSegue", this);
} break;
else case App.Enums.LockType.Password:
{ PerformSegue("lockPasswordSegue", this);
PerformSegue("lockPasswordSegue", this); break;
default:
break;
} }
IEnumerable<SiteViewModel> filteredSiteModels = new List<SiteViewModel>(); IEnumerable<SiteViewModel> filteredSiteModels = new List<SiteViewModel>();

View file

@ -50,7 +50,8 @@ namespace Bit.iOS
Resolver.Resolve<ISyncService>(), Resolver.Resolve<ISyncService>(),
Resolver.Resolve<IFingerprint>(), Resolver.Resolve<IFingerprint>(),
Resolver.Resolve<ISettings>(), Resolver.Resolve<ISettings>(),
Resolver.Resolve<IPushNotification>())); Resolver.Resolve<IPushNotification>(),
Resolver.Resolve<ILockService>()));
// Appearance stuff // Appearance stuff
@ -219,6 +220,7 @@ namespace Bit.iOS
.RegisterType<IAppIdService, AppIdService>(new ContainerControlledLifetimeManager()) .RegisterType<IAppIdService, AppIdService>(new ContainerControlledLifetimeManager())
.RegisterType<IPasswordGenerationService, PasswordGenerationService>(new ContainerControlledLifetimeManager()) .RegisterType<IPasswordGenerationService, PasswordGenerationService>(new ContainerControlledLifetimeManager())
.RegisterType<IReflectionService, ReflectionService>(new ContainerControlledLifetimeManager()) .RegisterType<IReflectionService, ReflectionService>(new ContainerControlledLifetimeManager())
.RegisterType<ILockService, LockService>(new ContainerControlledLifetimeManager())
// Repositories // Repositories
.RegisterType<IFolderRepository, FolderRepository>(new ContainerControlledLifetimeManager()) .RegisterType<IFolderRepository, FolderRepository>(new ContainerControlledLifetimeManager())
.RegisterType<IFolderApiRepository, FolderApiRepository>(new ContainerControlledLifetimeManager()) .RegisterType<IFolderApiRepository, FolderApiRepository>(new ContainerControlledLifetimeManager())