2016-07-20 06:29:32 +03:00
|
|
|
|
using System;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Plugin.Settings.Abstractions;
|
|
|
|
|
using Plugin.Fingerprint.Abstractions;
|
|
|
|
|
using Bit.App.Enums;
|
2017-06-02 21:46:10 +03:00
|
|
|
|
using System.Threading.Tasks;
|
2016-07-20 06:29:32 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Services
|
|
|
|
|
{
|
|
|
|
|
public class LockService : ILockService
|
|
|
|
|
{
|
|
|
|
|
private readonly ISettings _settings;
|
2017-04-28 18:07:26 +03:00
|
|
|
|
private readonly IAppSettingsService _appSettings;
|
2016-07-20 06:29:32 +03:00
|
|
|
|
private readonly IAuthService _authService;
|
|
|
|
|
private readonly IFingerprint _fingerprint;
|
|
|
|
|
|
|
|
|
|
public LockService(
|
|
|
|
|
ISettings settings,
|
2017-04-28 18:07:26 +03:00
|
|
|
|
IAppSettingsService appSettings,
|
2016-07-20 06:29:32 +03:00
|
|
|
|
IAuthService authService,
|
|
|
|
|
IFingerprint fingerprint)
|
|
|
|
|
{
|
|
|
|
|
_settings = settings;
|
2017-04-28 18:07:26 +03:00
|
|
|
|
_appSettings = appSettings;
|
2016-07-20 06:29:32 +03:00
|
|
|
|
_authService = authService;
|
|
|
|
|
_fingerprint = fingerprint;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-28 18:07:26 +03:00
|
|
|
|
public void UpdateLastActivity(DateTime? activityDate = null)
|
|
|
|
|
{
|
|
|
|
|
if(_appSettings.Locked)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_appSettings.LastActivity = activityDate.GetValueOrDefault(DateTime.UtcNow);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 21:46:10 +03:00
|
|
|
|
public async Task<LockType> GetLockTypeAsync(bool forceLock)
|
2016-07-20 06:29:32 +03:00
|
|
|
|
{
|
|
|
|
|
// 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)
|
2017-04-28 18:07:26 +03:00
|
|
|
|
if(!forceLock && !_appSettings.Locked)
|
2016-07-20 06:29:32 +03:00
|
|
|
|
{
|
2016-07-30 23:39:52 +03:00
|
|
|
|
// Lock seconds tells if they want to lock the app or not
|
|
|
|
|
var lockSeconds = _settings.GetValueOrDefault(Constants.SettingLockSeconds, 60 * 15);
|
|
|
|
|
if(lockSeconds == -1)
|
2016-07-20 06:29:32 +03:00
|
|
|
|
{
|
|
|
|
|
return LockType.None;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-09 02:00:36 +03:00
|
|
|
|
// Has it been longer than lockSeconds since the last time the app was used?
|
2016-08-02 07:32:57 +03:00
|
|
|
|
var now = DateTime.UtcNow;
|
2017-04-28 18:19:43 +03:00
|
|
|
|
if(now > _appSettings.LastActivity && (now - _appSettings.LastActivity).TotalSeconds < lockSeconds)
|
2016-07-20 06:29:32 +03:00
|
|
|
|
{
|
|
|
|
|
return LockType.None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// What method are we using to unlock?
|
2016-08-30 06:06:29 +03:00
|
|
|
|
var fingerprintUnlock = _settings.GetValueOrDefault(Constants.SettingFingerprintUnlockOn, false);
|
|
|
|
|
var pinUnlock = _settings.GetValueOrDefault(Constants.SettingPinUnlockOn, false);
|
2017-06-02 21:46:10 +03:00
|
|
|
|
var fingerprintAvailability = await _fingerprint.GetAvailabilityAsync();
|
|
|
|
|
if(fingerprintUnlock && fingerprintAvailability == FingerprintAvailability.Available)
|
2016-07-20 06:29:32 +03:00
|
|
|
|
{
|
|
|
|
|
return LockType.Fingerprint;
|
|
|
|
|
}
|
|
|
|
|
else if(pinUnlock && !string.IsNullOrWhiteSpace(_authService.PIN))
|
|
|
|
|
{
|
|
|
|
|
return LockType.PIN;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return LockType.Password;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|