5
5
#import " LocalAuthenticator.h"
6
6
#import " IdmAuthenticationPlugin.h"
7
7
#import " IDMMobileSDKv2Library.h"
8
+ #import " OMSecureStorage.h"
9
+
8
10
@import LocalAuthentication;
9
11
10
12
#define LOCAL_AUTH_BIOMETRIC @" cordova.plugins.IdmAuthFlows.Biometric"
11
13
#define LOCAL_AUTH_FINGERPRINT @" cordova.plugins.IdmAuthFlows.Fingerprint"
12
14
#define LOCAL_AUTH_PIN @" cordova.plugins.IdmAuthFlows.PIN"
15
+ #define LOCAL_AUTH_DEFAULT @" cordova.plugins.IdmAuthFlows.Default"
16
+ #define DEFAULT_AUTH_ID @" DefaultAuthInstance"
13
17
#define FALLBACK_RESULT @" fallback"
14
18
#define PROMPT_MESSAGE @" promptMessage"
15
19
#define PIN_FALLBACK_BUTTON_LABEL @" pinFallbackButtonLabel"
28
32
#define DISABLE_PIN_BIOMETRIC_ENABLED @" P1017"
29
33
#define ERROR_ENABLING_AUTHENTICATOR @" P1018"
30
34
#define BIOMETRIC_NOT_ENABLED @" P1019"
35
+ #define SAVING_VALUE_TO_SECURED_STORAGE_FAILED @" P1022"
36
+ #define SAVING_VALUE_TO_DEFAULT_STORAGE_FAILED @" P1023"
37
+ #define GETTING_VALUE_FROM_SECURED_STORAGE_FAILED @" P1024"
38
+ #define GETTING_VALUE_FROM_DEFAULT_STORAGE_FAILED @" P1025"
31
39
32
40
#ifdef DEBUG
33
41
# define IdmLog (...) NSLog (__VA_ARGS__)
41
49
@interface LocalAuthenticator ()<OMBiometricFallbackDelegate>
42
50
43
51
@property (nonatomic , assign ) Boolean authenticatedViaPin;
52
+ @property (nonatomic , assign ) Boolean defaultAuthenticationEnabled;
53
+
44
54
@property (nonatomic , strong ) OMFallbackAuthenticationCompletionBlock fallbackHandler;
45
55
46
56
@property (nonatomic , strong , nullable ) CDVCommandDelegateImpl* biometricAuthDelegate;
@@ -56,6 +66,7 @@ +(LocalAuthenticator*) sharedInstance {
56
66
sharedManager = [OMLocalAuthenticationManager sharedManager ];
57
67
[sharedManager useBiometricInsteadOfTouchID: YES ];
58
68
shared = [[LocalAuthenticator alloc ] init ];
69
+ shared.defaultAuthenticationEnabled = [shared enableDefaultAuthenticator ];
59
70
});
60
71
61
72
return shared;
@@ -68,6 +79,37 @@ -(void) enabledLocalAuthsPrimaryFirst:(CDVInvokedUrlCommand*)command delegate:(C
68
79
[commandDelegate sendPluginResult: result callbackId: command.callbackId];
69
80
}
70
81
82
+ - (BOOL ) enableDefaultAuthenticator {
83
+ NSString * authId = DEFAULT_AUTH_ID;
84
+ NSError * enableError = nil ;
85
+ NSString * authenticatorName = LOCAL_AUTH_DEFAULT;
86
+
87
+ OMAuthenticator* authenticator = [self getAuthenticator: authId authenticatorName: authenticatorName];
88
+ if (authenticator != nil ) {
89
+ IdmLog (@" Authenticator is already enabled for type %@ " , authenticatorName);
90
+ return YES ;
91
+ }
92
+
93
+ NSString * instanceId = [self getInstanceId: authId authenticatorName: authenticatorName];
94
+ [self registerAuthenticatorIfNeeded: authenticatorName error: &enableError];
95
+
96
+ if (!enableError) {
97
+ if ([sharedManager enableAuthentication: authenticatorName instanceId: instanceId error: &enableError]) {
98
+ OMAuthenticator* authenticator = [self getAuthenticator: authId authenticatorName: authenticatorName];
99
+ [authenticator authenticate: nil error: &enableError];
100
+ if (authenticator == nil ) {
101
+ IdmLog (@" Something went wrong while enabling Default Authenticator." );
102
+ return NO ;
103
+ }
104
+ }
105
+ }
106
+ if (enableError) {
107
+ IdmLog (@" Error Registering Default Authenticator" );
108
+ return NO ;
109
+ }
110
+ return YES ;
111
+ }
112
+
71
113
-(void ) enable : (CDVInvokedUrlCommand*)command delegate : (CDVCommandDelegateImpl*) commandDelegate {
72
114
NSError * enableError = nil ;
73
115
NSString * authId = command.arguments [0 ];
@@ -177,6 +219,86 @@ -(void) disable:(CDVInvokedUrlCommand*)command delegate:(CDVCommandDelegateImpl*
177
219
callbackId: command.callbackId];
178
220
}
179
221
222
+ -(void ) getPreference : (CDVInvokedUrlCommand*)command delegate : (CDVCommandDelegateImpl*) commandDelegate {
223
+ NSString * authId = command.arguments [0 ];
224
+ NSString * key = command.arguments [1 ];
225
+ NSString * result;
226
+ NSError * getDefaultPreferenceError = nil ;
227
+ NSError * getSecuredPreferenceError = nil ;
228
+
229
+ // Attempt fetching data
230
+ OMPinAuthenticator* pinAuthenticator = [self getPinAuthenticator: authId];
231
+ if (pinAuthenticator == nil ) {
232
+ OMDefaultAuthenticator *defAuth = [self getDefaultAuthenticator: DEFAULT_AUTH_ID];
233
+ result = [defAuth.secureStorage dataForId: key error: &getDefaultPreferenceError];
234
+ }
235
+ else {
236
+ result = [pinAuthenticator.secureStorage dataForId: key error: &getSecuredPreferenceError];
237
+ if (result == nil ) {
238
+ OMDefaultAuthenticator *defAuth = [self getDefaultAuthenticator: DEFAULT_AUTH_ID];
239
+ result = [defAuth.secureStorage dataForId: key error: &getDefaultPreferenceError];
240
+ }
241
+ }
242
+ [commandDelegate sendPluginResult: [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString: result] callbackId: command.callbackId];
243
+ }
244
+
245
+ -(void ) setPreference : (CDVInvokedUrlCommand*)command delegate : (CDVCommandDelegateImpl*) commandDelegate {
246
+ NSString * authId = command.arguments [0 ];
247
+ NSString * key = command.arguments [1 ];
248
+ NSString * value = command.arguments [2 ];
249
+ Boolean secure = [command.arguments[3 ] boolValue ];
250
+ NSError * setPreferenceError = nil ;
251
+
252
+ if (!secure) {
253
+ // Check if Default Authenticator is Enabled
254
+ if (!self.defaultAuthenticationEnabled ) {
255
+ [commandDelegate sendPluginResult: [IdmAuthenticationPlugin errorCodeToPluginResult: ERROR_ENABLING_AUTHENTICATOR]
256
+ callbackId: command.callbackId];
257
+ }
258
+
259
+ // Attempt storing in default storage
260
+ IdmLog (@" Storing in default storage" );
261
+ OMDefaultAuthenticator *defAuth = [self getDefaultAuthenticator: DEFAULT_AUTH_ID];
262
+ if (value == nil )
263
+ [defAuth.secureStorage deleteDataForId: key error: &setPreferenceError];
264
+ else
265
+ [defAuth.secureStorage saveDataForId: key data: value error: &setPreferenceError];
266
+
267
+ // Verify error and send result to plugin
268
+ if (setPreferenceError) {
269
+ [commandDelegate sendPluginResult: [IdmAuthenticationPlugin errorCodeToPluginResult: SAVING_VALUE_TO_DEFAULT_STORAGE_FAILED]
270
+ callbackId: command.callbackId];
271
+ }
272
+ else {
273
+ [commandDelegate sendPluginResult: [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString: @" Default Storage operation Successfull!!" ] callbackId: command.callbackId];
274
+ }
275
+
276
+ return ;
277
+ }
278
+ // Check if PIN Authenticator is Enabled
279
+ OMPinAuthenticator* pinAuthenticator = [self getPinAuthenticator: authId];
280
+ if (pinAuthenticator == nil ) {
281
+ IdmLog (@" No enabled authenticators" );
282
+ [commandDelegate sendPluginResult: [IdmAuthenticationPlugin errorCodeToPluginResult: PIN_AUTHENTICATOR_NOT_ENABLED]
283
+ callbackId: command.callbackId];
284
+ return ;
285
+ }
286
+ // Attempt storing in secured storage
287
+ if (value == nil )
288
+ [pinAuthenticator.secureStorage deleteDataForId: key error: &setPreferenceError];
289
+ else
290
+ [pinAuthenticator.secureStorage saveDataForId: key data: value error: &setPreferenceError];
291
+
292
+ // Verify error and send result to plugin
293
+ if (setPreferenceError) {
294
+ [commandDelegate sendPluginResult: [IdmAuthenticationPlugin errorCodeToPluginResult: SAVING_VALUE_TO_SECURED_STORAGE_FAILED]
295
+ callbackId: command.callbackId];
296
+ }
297
+ else {
298
+ [commandDelegate sendPluginResult: [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString: @" Secure Storage operation Successfull!!" ] callbackId: command.callbackId];
299
+ }
300
+ }
301
+
180
302
-(void ) authenticateBiometric : (CDVInvokedUrlCommand*)command
181
303
delegate : (CDVCommandDelegateImpl*)commandDelegate {
182
304
NSString * authId = command.arguments [0 ];
@@ -396,6 +518,8 @@ - (NSString *)authenticatorClassForType:(NSString*)authenticatorName
396
518
return NSStringFromClass ([OMBiometricAuthenticator class ]);
397
519
if ([LOCAL_AUTH_PIN isEqualToString: authenticatorName])
398
520
return NSStringFromClass ([OMPinAuthenticator class ]);
521
+ if ([LOCAL_AUTH_DEFAULT isEqualToString: authenticatorName])
522
+ return NSStringFromClass ([OMDefaultAuthenticator class ]);
399
523
return nil ;
400
524
}
401
525
@@ -406,6 +530,8 @@ -(OMAuthenticator*) getAuthenticator:(NSString*) authId authenticatorName:(NSStr
406
530
return [self getFingerprintAuthenticator: authId];
407
531
else if ([LOCAL_AUTH_BIOMETRIC isEqualToString: authenticatorName])
408
532
return [self getBiometricAuthenticator: authId];
533
+ else if ([LOCAL_AUTH_DEFAULT isEqualToString: authenticatorName])
534
+ return [self getDefaultAuthenticator: authId];
409
535
return nil ;
410
536
}
411
537
@@ -421,6 +547,18 @@ -(OMPinAuthenticator*) getPinAuthenticator:(NSString*) authId {
421
547
return nil ;
422
548
}
423
549
550
+ -(OMDefaultAuthenticator*) getDefaultAuthenticator : (NSString *) authId {
551
+ NSString * instanceId = [self getInstanceId: authId authenticatorName: LOCAL_AUTH_DEFAULT];
552
+ if (![sharedManager isAuthenticatorRegistered: LOCAL_AUTH_DEFAULT])
553
+ return nil ;
554
+
555
+ OMAuthenticator* auth = [sharedManager authenticatorForInstanceId: instanceId error: nil ];
556
+ if (auth && [auth isKindOfClass: [OMDefaultAuthenticator class ]]) {
557
+ return (OMDefaultAuthenticator*) auth;
558
+ }
559
+ return nil ;
560
+ }
561
+
424
562
-(OMBiometricAuthenticator*) getFingerprintAuthenticator : (NSString *) authId {
425
563
NSString * instanceId = [self getInstanceId: authId authenticatorName: LOCAL_AUTH_FINGERPRINT];
426
564
0 commit comments