forked from tradle/react-native-local-auth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RNLocalAuth.m
169 lines (144 loc) · 6.38 KB
/
RNLocalAuth.m
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#import "RNLocalAuth.h"
#import "RCTUtils.h"
#import <LocalAuthentication/LocalAuthentication.h>
@implementation RNLocalAuth
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(hasTouchID: (RCTResponseSenderBlock)callback)
{
LAContext *context = [[LAContext alloc] init];
NSError *error;
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
callback(@[[NSNull null], @true]);
// Device does not support TouchID
} else {
callback(@[RCTMakeError(@"RCTTouchIDNotSupported", nil, nil)]);
return;
}
}
RCT_EXPORT_METHOD(authenticate: (NSString*)reason
fallbackToPasscode:(BOOL)fallbackToPasscode
suppressEnterPassword:(BOOL)suppressEnterPassword
callback: (RCTResponseSenderBlock)callback)
{
[self authenticateWithPolicyAsync:LAPolicyDeviceOwnerAuthentication
reason:reason
fallbackToPasscode:fallbackToPasscode
suppressEnterPassword:suppressEnterPassword
millisDelay:@0
callback:callback];
}
RCT_EXPORT_METHOD(authenticateWithTouchID: (NSString*)reason
fallbackToPasscode:(BOOL)fallbackToPasscode
suppressEnterPassword:(BOOL)suppressEnterPassword
callback: (RCTResponseSenderBlock)callback)
{
[self authenticateWithPolicyAsync:LAPolicyDeviceOwnerAuthenticationWithBiometrics
reason:reason
fallbackToPasscode:fallbackToPasscode
suppressEnterPassword:suppressEnterPassword
millisDelay:@0
callback:callback];
}
- (void) authenticateWithPolicyAsync: (LAPolicy) policy
reason:(NSString*)reason
fallbackToPasscode:(BOOL)fallbackToPasscode
suppressEnterPassword:(BOOL)suppressEnterPassword
millisDelay:(NSNumber*) millisDelay
callback: (RCTResponseSenderBlock)callback
{
[self authenticateWithPolicy:policy
reason:reason
suppressEnterPassword:suppressEnterPassword
millisDelay:millisDelay
completionHandler:^(NSInteger errorCode, NSString* errorReason) {
if (errorCode == errSecSuccess) {
callback(@[[NSNull null], @"Authenticated with Touch ID."]);
return;
}
if (errorCode == LAErrorTouchIDLockout) {
[self authenticateWithPolicyAsync:LAPolicyDeviceOwnerAuthentication
reason:reason
fallbackToPasscode:fallbackToPasscode
suppressEnterPassword:suppressEnterPassword
millisDelay:@1000
callback:callback];
return;
}
callback(@[RCTMakeError(errorReason, nil, nil)]);
}];
}
- (void) authenticateWithPolicy:(LAPolicy) policy
reason:(NSString *)reason
suppressEnterPassword: (BOOL) suppressEnterPassword
millisDelay: (NSNumber *) millisDelay
completionHandler:(void(^) (NSInteger, NSString *))handler
{
// per http://stackoverflow.com/questions/26463196/touch-id-causing-app-to-become-non-responsive
if (millisDelay == nil) millisDelay = @0;
dispatch_queue_t highPriorityQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, [millisDelay intValue] * NSEC_PER_MSEC), highPriorityQueue, ^{
LAContext *context = [[LAContext alloc] init];
if (suppressEnterPassword) {
context.localizedFallbackTitle = @"";
}
__block NSString* errorReason;
NSError* error;
if (![context canEvaluatePolicy:policy error:&error]) {
NSInteger errCode;
NSString* errReason;
if (policy == LAPolicyDeviceOwnerAuthenticationWithBiometrics) {
errCode = LAErrorTouchIDNotAvailable;
errReason = @"RCTTouchIDNotSupported";
} else {
errCode = LAErrorAuthenticationFailed;
errReason = @"LAErrorAuthenticationFailed";
}
handler(errCode, errReason);
return;
}
// Attempt Authentification
[context evaluatePolicy:policy
localizedReason:reason
reply:^(BOOL success, NSError *error)
{
// Failed Authentication
if (error) {
switch (error.code) {
case LAErrorAuthenticationFailed:
errorReason = @"LAErrorAuthenticationFailed";
break;
case LAErrorUserCancel:
errorReason = @"LAErrorUserCancel";
break;
case LAErrorUserFallback:
errorReason = @"LAErrorUserFallback";
break;
case LAErrorSystemCancel:
errorReason = @"LAErrorSystemCancel";
break;
case LAErrorPasscodeNotSet:
errorReason = @"LAErrorPasscodeNotSet";
break;
case LAErrorTouchIDNotAvailable:
errorReason = @"LAErrorTouchIDNotAvailable";
break;
case LAErrorTouchIDNotEnrolled:
errorReason = @"LAErrorTouchIDNotEnrolled";
break;
case LAErrorTouchIDLockout:
errorReason = @"LAErrorTouchIDLockout";
break;
default:
errorReason = @"RCTTouchIDUnknownError";
break;
}
NSLog(@"Authentication failed: %@", errorReason);
handler(error.code, errorReason);
return;
}
handler(errSecSuccess, nil);
// Authenticated Successfully
}];
});
}
@end