mirror of
https://github.com/bitwarden/mobile
synced 2025-12-23 19:53:50 +00:00
Invalidate biometric on change (#1026)
* Initial working version for Android * Add a fallback for when upgrading from older app version. * Ensure biometric validity is re-checked on focus * Only setup biometric integrity key if biometric is turned on. * Fix styling according to comments * Fallback for Android 5. * Improve comment * Add boilerplate for iOS * Change BiometricService to public * Untested iOS implementation. * Convert IBiometricService to async. Fix code style for iOS. * Base64 NSData. * Review comments for Android BiometricService. * Rename methods in BiometricService to append Async * Ensure we wait for async SetupBiometricAsync. * Update BiometricService.cs Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>
This commit is contained in:
62
src/iOS.Core/Services/BiometricService.cs
Normal file
62
src/iOS.Core/Services/BiometricService.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Foundation;
|
||||
using LocalAuthentication;
|
||||
|
||||
namespace Bit.iOS.Core.Services
|
||||
{
|
||||
public class BiometricService : IBiometricService
|
||||
{
|
||||
private IStorageService _storageService;
|
||||
|
||||
public BiometricService(IStorageService storageService)
|
||||
{
|
||||
_storageService = storageService;
|
||||
}
|
||||
|
||||
public async Task<bool> SetupBiometricAsync()
|
||||
{
|
||||
var state = GetState();
|
||||
await _storageService.SaveAsync("biometricState", ToBase64(state));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> ValidateIntegrityAsync()
|
||||
{
|
||||
var oldState = await _storageService.GetAsync<string>("biometricState");
|
||||
if (oldState == null)
|
||||
{
|
||||
// Fallback for upgraded devices
|
||||
await SetupBiometricAsync();
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
var state = GetState();
|
||||
|
||||
return FromBase64(oldState) == state;
|
||||
}
|
||||
}
|
||||
|
||||
private NSData GetState()
|
||||
{
|
||||
var context = new LAContext();
|
||||
context.CanEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, out _);
|
||||
|
||||
return context.EvaluatedPolicyDomainState;
|
||||
}
|
||||
|
||||
private string ToBase64(NSData data)
|
||||
{
|
||||
return System.Convert.ToBase64String(data.ToArray());
|
||||
}
|
||||
|
||||
private NSData FromBase64(string data)
|
||||
{
|
||||
var bytes = System.Convert.FromBase64String(data);
|
||||
return NSData.FromArray(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,7 @@ namespace Bit.iOS.Core.Utilities
|
||||
var deviceActionService = new DeviceActionService(mobileStorageService, messagingService);
|
||||
var platformUtilsService = new MobilePlatformUtilsService(deviceActionService, messagingService,
|
||||
broadcasterService);
|
||||
var biometricService = new BiometricService(mobileStorageService);
|
||||
|
||||
ServiceContainer.Register<IBroadcasterService>("broadcasterService", broadcasterService);
|
||||
ServiceContainer.Register<IMessagingService>("messagingService", messagingService);
|
||||
@@ -65,6 +66,7 @@ namespace Bit.iOS.Core.Utilities
|
||||
ServiceContainer.Register<IStorageService>("secureStorageService", secureStorageService);
|
||||
ServiceContainer.Register<IDeviceActionService>("deviceActionService", deviceActionService);
|
||||
ServiceContainer.Register<IPlatformUtilsService>("platformUtilsService", platformUtilsService);
|
||||
ServiceContainer.Register<IBiometricService>("biometricService", biometricService);
|
||||
}
|
||||
|
||||
public static void Bootstrap(Func<Task> postBootstrapFunc = null)
|
||||
|
||||
@@ -163,6 +163,7 @@
|
||||
<Compile Include="Renderers\CustomTabbedRenderer.cs" />
|
||||
<Compile Include="Renderers\CustomViewCellRenderer.cs" />
|
||||
<Compile Include="Renderers\HybridWebViewRenderer.cs" />
|
||||
<Compile Include="Services\BiometricService.cs" />
|
||||
<Compile Include="Services\DeviceActionService.cs" />
|
||||
<Compile Include="Utilities\ASHelpers.cs" />
|
||||
<Compile Include="Utilities\Dialogs.cs" />
|
||||
|
||||
Reference in New Issue
Block a user