1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-06-28 10:54:59 +02:00
bitwarden-mobile/src/Core/Services/LockService.cs

164 lines
5.5 KiB
C#
Raw Normal View History

2019-05-15 20:22:28 +02:00
using Bit.Core.Abstractions;
2019-06-03 22:04:04 +02:00
using Bit.Core.Utilities;
2019-05-15 20:22:28 +02:00
using System;
using System.Threading.Tasks;
namespace Bit.Core.Services
{
public class LockService : ILockService
{
private readonly ICryptoService _cryptoService;
private readonly IUserService _userService;
private readonly IPlatformUtilsService _platformUtilsService;
private readonly IStorageService _storageService;
private readonly IFolderService _folderService;
private readonly ICipherService _cipherService;
private readonly ICollectionService _collectionService;
private readonly ISearchService _searchService;
private readonly IMessagingService _messagingService;
public LockService(
ICryptoService cryptoService,
IUserService userService,
IPlatformUtilsService platformUtilsService,
IStorageService storageService,
IFolderService folderService,
ICipherService cipherService,
ICollectionService collectionService,
ISearchService searchService,
IMessagingService messagingService)
{
_cryptoService = cryptoService;
_userService = userService;
_platformUtilsService = platformUtilsService;
_storageService = storageService;
_folderService = folderService;
_cipherService = cipherService;
_collectionService = collectionService;
_searchService = searchService;
_messagingService = messagingService;
}
public bool PinLocked { get; set; }
2019-05-16 23:30:07 +02:00
public bool FingerprintLocked { get; set; } = true;
2019-05-15 20:22:28 +02:00
public async Task<bool> IsLockedAsync()
{
var hasKey = await _cryptoService.HasKeyAsync();
2019-05-16 23:30:07 +02:00
if(hasKey)
2019-05-15 20:22:28 +02:00
{
2019-05-16 23:30:07 +02:00
if(PinLocked)
{
return true;
}
else
{
var fingerprintSet = await IsFingerprintLockSetAsync();
if(fingerprintSet && FingerprintLocked)
{
return true;
}
}
2019-05-15 20:22:28 +02:00
}
return !hasKey;
}
public async Task CheckLockAsync()
{
2019-05-29 14:27:58 +02:00
if(_platformUtilsService.IsViewOpen())
2019-05-15 20:22:28 +02:00
{
return;
}
var authed = await _userService.IsAuthenticatedAsync();
if(!authed)
{
return;
}
2019-05-16 18:29:55 +02:00
if(await IsLockedAsync())
{
return;
}
2019-05-15 20:22:28 +02:00
var lockOption = _platformUtilsService.LockTimeout();
if(lockOption == null)
{
lockOption = await _storageService.GetAsync<int?>(Constants.LockOptionKey);
}
if(lockOption.GetValueOrDefault(-1) < 0)
{
return;
}
var lastActive = await _storageService.GetAsync<DateTime?>(Constants.LastActiveKey);
if(lastActive == null)
{
return;
}
var diff = DateTime.UtcNow - lastActive.Value;
2019-05-16 18:29:55 +02:00
if(diff.TotalSeconds >= lockOption.Value)
2019-05-15 20:22:28 +02:00
{
// need to lock now
await LockAsync(true);
}
}
public async Task LockAsync(bool allowSoftLock = false, bool userInitiated = false)
2019-05-15 20:22:28 +02:00
{
var authed = await _userService.IsAuthenticatedAsync();
if(!authed)
{
return;
}
if(allowSoftLock)
{
var pinSet = await IsPinLockSetAsync();
if(pinSet.Item1)
{
PinLocked = true;
2019-05-16 23:30:07 +02:00
}
FingerprintLocked = await IsFingerprintLockSetAsync();
2019-05-16 23:30:07 +02:00
if(FingerprintLocked || PinLocked)
{
_messagingService.Send("locked", userInitiated);
2019-05-15 20:22:28 +02:00
// TODO: locked callback?
return;
}
}
await Task.WhenAll(
_cryptoService.ClearKeyAsync(),
_cryptoService.ClearOrgKeysAsync(true),
_cryptoService.ClearKeyPairAsync(true),
_cryptoService.ClearEncKeyAsync(true));
_folderService.ClearCache();
_cipherService.ClearCache();
_collectionService.ClearCache();
_searchService.ClearIndex();
_messagingService.Send("locked", userInitiated);
2019-05-15 20:22:28 +02:00
// TODO: locked callback?
}
2019-05-16 22:00:16 +02:00
public async Task SetLockOptionAsync(int? lockOption)
2019-05-15 20:22:28 +02:00
{
await _storageService.SaveAsync(Constants.LockOptionKey, lockOption);
await _cryptoService.ToggleKeyAsync();
}
public async Task<Tuple<bool, bool>> IsPinLockSetAsync()
{
var protectedPin = await _storageService.GetAsync<string>(Constants.ProtectedPin);
var pinProtectedKey = await _storageService.GetAsync<string>(Constants.PinProtectedKey);
return new Tuple<bool, bool>(protectedPin != null, pinProtectedKey != null);
}
2019-05-16 23:30:07 +02:00
public async Task<bool> IsFingerprintLockSetAsync()
{
var fingerprintLock = await _storageService.GetAsync<bool?>(Constants.FingerprintUnlockKey);
return fingerprintLock.GetValueOrDefault();
}
2019-05-15 20:22:28 +02:00
public async Task ClearAsync()
{
await _storageService.RemoveAsync(Constants.ProtectedPin);
}
}
}