/
BiometricService.cs
62 lines (51 loc) · 1.61 KB
/
BiometricService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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);
}
}
}