-
Notifications
You must be signed in to change notification settings - Fork 0
/
touchid_darwin.go
67 lines (62 loc) · 1.54 KB
/
touchid_darwin.go
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
63
64
65
66
67
package touchid
/*
#cgo CFLAGS: -x objective-c -fmodules -fblocks
#cgo LDFLAGS: -framework CoreFoundation -framework LocalAuthentication -framework Foundation
#include <stdlib.h>
#include <stdio.h>
#import <LocalAuthentication/LocalAuthentication.h>
int Authenticate(int lap, char const* reason) {
LAContext *laContext = [[LAContext alloc] init];
NSError *authError = nil;
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
NSString *nsReason = [NSString stringWithUTF8String:reason];
__block int result = 0;
LAPolicy laPolicy;
switch (lap){
case 1:
laPolicy = LAPolicyDeviceOwnerAuthentication;
break;
case 2:
laContext.localizedFallbackTitle = @"";
laPolicy = LAPolicyDeviceOwnerAuthenticationWithBiometrics;
break;
default:
laPolicy = LAPolicyDeviceOwnerAuthentication;
break;
}
if ([laContext canEvaluatePolicy:laPolicy error:&authError]) {
[laContext evaluatePolicy:laPolicy
localizedReason:nsReason
reply:^(BOOL success, NSError *error) {
if (success) {
result = 1;
} else {
result = 2;
}
dispatch_semaphore_signal(sema);
}];
}
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
dispatch_release(sema);
return result;
}
*/
import (
"C"
)
import (
"errors"
"unsafe"
)
func Authenticate(dType int, reason string) (bool, error) {
reasonStr := C.CString(reason)
defer C.free(unsafe.Pointer(reasonStr))
result := C.Authenticate(C.int(dType), reasonStr)
switch result {
case 1:
return true, nil
case 2:
return false, nil
}
return false, errors.New("error occurred accessing biometrics")
}