Skip to content

Commit 4524370

Browse files
Nirodha PereraNirodha Perera
Nirodha Perera
authored and
Nirodha Perera
committed
Changed to store param values in shared preferences
1 parent 15b4399 commit 4524370

File tree

1 file changed

+49
-21
lines changed

1 file changed

+49
-21
lines changed

android/src/main/java/com/rnappauth/RNAppAuthModule.java

+49-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.Activity;
44
import android.app.PendingIntent;
55
import android.content.ComponentName;
6+
import android.content.SharedPreferences;
67
import android.content.Context;
78
import android.content.Intent;
89
import android.net.Uri;
@@ -62,17 +63,12 @@ public class RNAppAuthModule extends ReactContextBaseJavaModule implements Activ
6263

6364
private final ReactApplicationContext reactContext;
6465
private Promise promise;
65-
private boolean dangerouslyAllowInsecureHttpRequests;
66-
private Boolean skipCodeExchange;
67-
private Boolean usePKCE;
6866
private Boolean useNonce;
69-
private String codeVerifier;
7067
private String clientAuthMethod = "basic";
7168
private Map<String, String> registrationRequestHeaders = null;
7269
private Map<String, String> authorizationRequestHeaders = null;
7370
private Map<String, String> tokenRequestHeaders = null;
7471
private Map<String, String> additionalParametersMap;
75-
private String clientSecret;
7672
private final ConcurrentHashMap<String, AuthorizationServiceConfiguration> mServiceConfigurations = new ConcurrentHashMap<>();
7773
private boolean isPrefetched = false;
7874

@@ -236,13 +232,17 @@ public void authorize(
236232

237233
// store args in private fields for later use in onActivityResult handler
238234
this.promise = promise;
239-
this.dangerouslyAllowInsecureHttpRequests = dangerouslyAllowInsecureHttpRequests;
240235
this.additionalParametersMap = additionalParametersMap;
241-
this.clientSecret = clientSecret;
242236
this.clientAuthMethod = clientAuthMethod;
243-
this.skipCodeExchange = skipCodeExchange;
244237
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();
246246

247247
// when serviceConfiguration is provided, we don't need to hit up the OpenID well-known id endpoint
248248
if (serviceConfiguration != null || hasServiceConfiguration(issuer)) {
@@ -330,7 +330,10 @@ public void refresh(
330330
}
331331

332332
// 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();
334337
this.additionalParametersMap = additionalParametersMap;
335338

336339
// 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,
416419
return;
417420
}
418421

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+
420430
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);
423433
} else {
424434
map = TokenResponseFactory.authorizationResponseToMap(response);
425435
}
@@ -430,16 +440,16 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
430440
return;
431441
}
432442

433-
443+
Boolean dangerouslyAllowInsecureHttpRequests = sharedPref.getBoolean("dangerouslyAllowInsecureHttpRequests", false);
434444
final Promise authorizePromise = this.promise;
435445
final AppAuthConfiguration configuration = createAppAuthConfiguration(
436-
createConnectionBuilder(this.dangerouslyAllowInsecureHttpRequests, this.tokenRequestHeaders),
437-
this.dangerouslyAllowInsecureHttpRequests
446+
createConnectionBuilder(dangerouslyAllowInsecureHttpRequests, this.tokenRequestHeaders),
447+
dangerouslyAllowInsecureHttpRequests
438448
);
439449

440450
AuthorizationService authService = new AuthorizationService(this.reactContext, configuration);
441451

442-
TokenRequest tokenRequest = response.createTokenExchangeRequest(this.additionalParametersMap);
452+
TokenRequest tokenRequest = this.additionalParametersMap? response.createTokenExchangeRequest(this.additionalParametersMap) : response.createTokenExchangeRequest();
443453

444454
AuthorizationService.TokenResponseCallback tokenResponseCallback = new AuthorizationService.TokenResponseCallback() {
445455

@@ -459,14 +469,24 @@ public void onTokenRequestCompleted(
459469
}
460470
};
461471

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);
464475
authService.performTokenRequest(tokenRequest, clientAuth, tokenResponseCallback);
465476

466477
} else {
467478
authService.performTokenRequest(tokenRequest, tokenResponseCallback);
468479
}
469480

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+
470490
}
471491
}
472492

@@ -588,8 +608,12 @@ private void authorizeWithConfiguration(
588608
if (!usePKCE) {
589609
authRequestBuilder.setCodeVerifier(null);
590610
} 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);
593617
}
594618

595619
if(!useNonce) {
@@ -675,6 +699,10 @@ public void onTokenRequestCompleted(@Nullable TokenResponse response, @Nullable
675699
} else {
676700
authService.performTokenRequest(tokenRequest, tokenResponseCallback);
677701
}
702+
SharedPreferences sharedPref = getCurrentActivity().getPreferences(Context.MODE_PRIVATE);
703+
SharedPreferences.Editor editor = sharedPref.edit();
704+
editor.remove("dangerouslyAllowInsecureHttpRequests")
705+
editor.apply()
678706
}
679707

680708
private void parseHeaderMap (ReadableMap headerMap) {

0 commit comments

Comments
 (0)