3
3
import android .app .Activity ;
4
4
import android .app .PendingIntent ;
5
5
import android .content .ComponentName ;
6
+ import android .content .SharedPreferences ;
6
7
import android .content .Context ;
7
8
import android .content .Intent ;
8
9
import android .net .Uri ;
@@ -62,17 +63,12 @@ public class RNAppAuthModule extends ReactContextBaseJavaModule implements Activ
62
63
63
64
private final ReactApplicationContext reactContext ;
64
65
private Promise promise ;
65
- private boolean dangerouslyAllowInsecureHttpRequests ;
66
- private Boolean skipCodeExchange ;
67
- private Boolean usePKCE ;
68
66
private Boolean useNonce ;
69
- private String codeVerifier ;
70
67
private String clientAuthMethod = "basic" ;
71
68
private Map <String , String > registrationRequestHeaders = null ;
72
69
private Map <String , String > authorizationRequestHeaders = null ;
73
70
private Map <String , String > tokenRequestHeaders = null ;
74
71
private Map <String , String > additionalParametersMap ;
75
- private String clientSecret ;
76
72
private final ConcurrentHashMap <String , AuthorizationServiceConfiguration > mServiceConfigurations = new ConcurrentHashMap <>();
77
73
private boolean isPrefetched = false ;
78
74
@@ -236,13 +232,17 @@ public void authorize(
236
232
237
233
// store args in private fields for later use in onActivityResult handler
238
234
this .promise = promise ;
239
- this .dangerouslyAllowInsecureHttpRequests = dangerouslyAllowInsecureHttpRequests ;
240
235
this .additionalParametersMap = additionalParametersMap ;
241
- this .clientSecret = clientSecret ;
242
236
this .clientAuthMethod = clientAuthMethod ;
243
- this .skipCodeExchange = skipCodeExchange ;
244
237
this .useNonce = useNonce ;
245
- this .usePKCE = usePKCE ;
238
+
239
+ SharedPreferences sharedPref = getCurrentActivity ().getPreferences (Context .MODE_PRIVATE );
240
+ SharedPreferences .Editor editor = sharedPref .edit ();
241
+ editor .putBoolean ("dangerouslyAllowInsecureHttpRequests" , dangerouslyAllowInsecureHttpRequests );
242
+ editor .putBoolean ("skipCodeExchange" , skipCodeExchange );
243
+ editor .putBoolean ("usePKCE" , usePKCE );
244
+ editor .putString ("clientSecret" , clientSecret );
245
+ editor .apply ();
246
246
247
247
// when serviceConfiguration is provided, we don't need to hit up the OpenID well-known id endpoint
248
248
if (serviceConfiguration != null || hasServiceConfiguration (issuer )) {
@@ -330,7 +330,10 @@ public void refresh(
330
330
}
331
331
332
332
// store setting in private field for later use in onActivityResult handler
333
- this .dangerouslyAllowInsecureHttpRequests = dangerouslyAllowInsecureHttpRequests ;
333
+ SharedPreferences sharedPref = getCurrentActivity ().getPreferences (Context .MODE_PRIVATE );
334
+ SharedPreferences .Editor editor = sharedPref .edit ();
335
+ editor .putBoolean ("dangerouslyAllowInsecureHttpRequests" , dangerouslyAllowInsecureHttpRequests );
336
+ editor .apply ();
334
337
this .additionalParametersMap = additionalParametersMap ;
335
338
336
339
// when serviceConfiguration is provided, we don't need to hit up the OpenID well-known id endpoint
@@ -416,10 +419,17 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
416
419
return ;
417
420
}
418
421
419
- if (this .skipCodeExchange ) {
422
+ SharedPreferences sharedPref = getCurrentActivity ().getPreferences (Context .MODE_PRIVATE );
423
+ Boolean skipCodeExchange = sharedPref .getBoolean ("skipCodeExchange" , false );
424
+
425
+ if (skipCodeExchange ) {
426
+
427
+ String codeVerifier = sharedPref .getString ("codeVerifier" , null );
428
+ Boolean usePKCE = sharedPref .getBoolean ("usePKCE" , true );
429
+
420
430
WritableMap map ;
421
- if (this . usePKCE && this . codeVerifier != null ) {
422
- map = TokenResponseFactory .authorizationCodeResponseToMap (response , this . codeVerifier );
431
+ if (usePKCE && codeVerifier != null ) {
432
+ map = TokenResponseFactory .authorizationCodeResponseToMap (response , codeVerifier );
423
433
} else {
424
434
map = TokenResponseFactory .authorizationResponseToMap (response );
425
435
}
@@ -430,16 +440,16 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
430
440
return ;
431
441
}
432
442
433
-
443
+ Boolean dangerouslyAllowInsecureHttpRequests = sharedPref . getBoolean ( "dangerouslyAllowInsecureHttpRequests" , false );
434
444
final Promise authorizePromise = this .promise ;
435
445
final AppAuthConfiguration configuration = createAppAuthConfiguration (
436
- createConnectionBuilder (this . dangerouslyAllowInsecureHttpRequests , this .tokenRequestHeaders ),
437
- this . dangerouslyAllowInsecureHttpRequests
446
+ createConnectionBuilder (dangerouslyAllowInsecureHttpRequests , this .tokenRequestHeaders ),
447
+ dangerouslyAllowInsecureHttpRequests
438
448
);
439
449
440
450
AuthorizationService authService = new AuthorizationService (this .reactContext , configuration );
441
451
442
- TokenRequest tokenRequest = response .createTokenExchangeRequest (this .additionalParametersMap );
452
+ TokenRequest tokenRequest = this . additionalParametersMap ? response .createTokenExchangeRequest (this .additionalParametersMap ) : response . createTokenExchangeRequest ( );
443
453
444
454
AuthorizationService .TokenResponseCallback tokenResponseCallback = new AuthorizationService .TokenResponseCallback () {
445
455
@@ -459,14 +469,24 @@ public void onTokenRequestCompleted(
459
469
}
460
470
};
461
471
462
- if (this .clientSecret != null ) {
463
- ClientAuthentication clientAuth = this .getClientAuthentication (this .clientSecret , this .clientAuthMethod );
472
+ String clientSecret = sharedPref .getString ("clientSecret" , null );
473
+ if (clientSecret != null ) {
474
+ ClientAuthentication clientAuth = this .getClientAuthentication (clientSecret , this .clientAuthMethod );
464
475
authService .performTokenRequest (tokenRequest , clientAuth , tokenResponseCallback );
465
476
466
477
} else {
467
478
authService .performTokenRequest (tokenRequest , tokenResponseCallback );
468
479
}
469
480
481
+ SharedPreferences .Editor editor = sharedPref .edit ();
482
+ editor .remove ("dangerouslyAllowInsecureHttpRequests" )
483
+ editor .remove ("clientSecret" )
484
+ editor .remove ("dangerouslyAllowInsecureHttpRequests" )
485
+ editor .remove ("skipCodeExchange" )
486
+ editor .remove ("usePKCE" )
487
+ editor .remove ("codeVerifier" )
488
+ editor .apply ();
489
+
470
490
}
471
491
}
472
492
@@ -588,8 +608,12 @@ private void authorizeWithConfiguration(
588
608
if (!usePKCE ) {
589
609
authRequestBuilder .setCodeVerifier (null );
590
610
} else {
591
- this .codeVerifier = CodeVerifierUtil .generateRandomCodeVerifier ();
592
- authRequestBuilder .setCodeVerifier (this .codeVerifier );
611
+ String codeVerifier = CodeVerifierUtil .generateRandomCodeVerifier ();
612
+ SharedPreferences sharedPref = getCurrentActivity ().getPreferences (Context .MODE_PRIVATE );
613
+ SharedPreferences .Editor editor = sharedPref .edit ();
614
+ editor .putString ("codeVerifier" , codeVerifier );
615
+ editor .apply ();
616
+ authRequestBuilder .setCodeVerifier (codeVerifier );
593
617
}
594
618
595
619
if (!useNonce ) {
@@ -675,6 +699,10 @@ public void onTokenRequestCompleted(@Nullable TokenResponse response, @Nullable
675
699
} else {
676
700
authService .performTokenRequest (tokenRequest , tokenResponseCallback );
677
701
}
702
+ SharedPreferences sharedPref = getCurrentActivity ().getPreferences (Context .MODE_PRIVATE );
703
+ SharedPreferences .Editor editor = sharedPref .edit ();
704
+ editor .remove ("dangerouslyAllowInsecureHttpRequests" )
705
+ editor .apply ()
678
706
}
679
707
680
708
private void parseHeaderMap (ReadableMap headerMap ) {
0 commit comments