mirror of
https://github.com/bitwarden/android.git
synced 2024-12-20 00:02:58 +03:00
centralized lock logic into a new lock service to be shared to extension
This commit is contained in:
parent
7fb51b5aa4
commit
d0bf141c5d
10 changed files with 141 additions and 64 deletions
|
@ -34,7 +34,8 @@ namespace Bit.Android
|
|||
Resolver.Resolve<ISyncService>(),
|
||||
Resolver.Resolve<IFingerprint>(),
|
||||
Resolver.Resolve<ISettings>(),
|
||||
Resolver.Resolve<IPushNotification>()));
|
||||
Resolver.Resolve<IPushNotification>(),
|
||||
Resolver.Resolve<ILockService>()));
|
||||
}
|
||||
|
||||
protected override void OnPause()
|
||||
|
|
|
@ -123,6 +123,7 @@ namespace Bit.Android
|
|||
.RegisterType<IAppIdService, AppIdService>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<IPasswordGenerationService, PasswordGenerationService>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<IReflectionService, ReflectionService>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<ILockService, LockService>(new ContainerControlledLifetimeManager())
|
||||
// Repositories
|
||||
.RegisterType<IFolderRepository, FolderRepository>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<IFolderApiRepository, FolderApiRepository>(new ContainerControlledLifetimeManager())
|
||||
|
|
9
src/App/Abstractions/Services/ILockService.cs
Normal file
9
src/App/Abstractions/Services/ILockService.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
using Bit.App.Enums;
|
||||
|
||||
namespace Bit.App.Abstractions
|
||||
{
|
||||
public interface ILockService
|
||||
{
|
||||
LockType GetLockType(bool forceLock);
|
||||
}
|
||||
}
|
|
@ -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<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 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -69,6 +69,7 @@
|
|||
<Compile Include="Controls\FormPickerCell.cs" />
|
||||
<Compile Include="Controls\FormEntryCell.cs" />
|
||||
<Compile Include="Controls\PinControl.cs" />
|
||||
<Compile Include="Enums\LockType.cs" />
|
||||
<Compile Include="Enums\CipherType.cs" />
|
||||
<Compile Include="Enums\PushType.cs" />
|
||||
<Compile Include="Enums\ReturnType.cs" />
|
||||
|
@ -146,6 +147,8 @@
|
|||
<DependentUpon>AppResources.resx</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Services\AppIdService.cs" />
|
||||
<Compile Include="Abstractions\Services\ILockService.cs" />
|
||||
<Compile Include="Services\LockService.cs" />
|
||||
<Compile Include="Services\DatabaseService.cs" />
|
||||
<Compile Include="Services\FolderService.cs" />
|
||||
<Compile Include="Repositories\Repository.cs" />
|
||||
|
|
10
src/App/Enums/LockType.cs
Normal file
10
src/App/Enums/LockType.cs
Normal file
|
@ -0,0 +1,10 @@
|
|||
namespace Bit.App.Enums
|
||||
{
|
||||
public enum LockType : short
|
||||
{
|
||||
None = 0,
|
||||
Fingerprint = 1,
|
||||
PIN = 2,
|
||||
Password = 3
|
||||
}
|
||||
}
|
69
src/App/Services/LockService.cs
Normal file
69
src/App/Services/LockService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -98,6 +98,7 @@ namespace Bit.iOS.Extension
|
|||
.RegisterType<ISyncService, SyncService>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<IPasswordGenerationService, PasswordGenerationService>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<IAppIdService, AppIdService>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<ILockService, LockService>(new ContainerControlledLifetimeManager())
|
||||
// Repositories
|
||||
.RegisterType<IFolderRepository, FolderRepository>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<IFolderApiRepository, FolderApiRepository>(new ContainerControlledLifetimeManager())
|
||||
|
|
|
@ -32,18 +32,21 @@ namespace Bit.iOS.Extension
|
|||
{
|
||||
base.ViewDidLoad();
|
||||
|
||||
// TODO: lock logic
|
||||
if(true)
|
||||
var lockService = Resolver.Resolve<ILockService>();
|
||||
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<SiteViewModel> filteredSiteModels = new List<SiteViewModel>();
|
||||
|
|
|
@ -50,7 +50,8 @@ namespace Bit.iOS
|
|||
Resolver.Resolve<ISyncService>(),
|
||||
Resolver.Resolve<IFingerprint>(),
|
||||
Resolver.Resolve<ISettings>(),
|
||||
Resolver.Resolve<IPushNotification>()));
|
||||
Resolver.Resolve<IPushNotification>(),
|
||||
Resolver.Resolve<ILockService>()));
|
||||
|
||||
// Appearance stuff
|
||||
|
||||
|
@ -219,6 +220,7 @@ namespace Bit.iOS
|
|||
.RegisterType<IAppIdService, AppIdService>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<IPasswordGenerationService, PasswordGenerationService>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<IReflectionService, ReflectionService>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<ILockService, LockService>(new ContainerControlledLifetimeManager())
|
||||
// Repositories
|
||||
.RegisterType<IFolderRepository, FolderRepository>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<IFolderApiRepository, FolderApiRepository>(new ContainerControlledLifetimeManager())
|
||||
|
|
Loading…
Reference in a new issue