Skip to content

Commit

Permalink
useNonce android support (FormidableLabs#640)
Browse files Browse the repository at this point in the history
* useNonce android support

* Update tests with useNonce
  • Loading branch information
SleipRecx authored Jun 27, 2021
1 parent 7bf86d9 commit b20561a
Show file tree
Hide file tree
Showing 5 changed files with 10,814 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ with optional overrides.
- **token** - (`{ [key: string]: value }`) headers to be passed during token retrieval request.
- **register** - (`{ [key: string]: value }`) headers to be passed during registration request.
- **additionalHeaders** - (`{ [key: string]: value }`) _IOS_ you can specify additional headers to be passed for all authorize, refresh, and register requests.
- **useNonce** - (`boolean`) _IOS_ (default: true) optionally allows not sending the nonce parameter, to support non-compliant providers
- **useNonce** - (`boolean`) (default: true) optionally allows not sending the nonce parameter, to support non-compliant providers
- **usePKCE** - (`boolean`) (default: true) optionally allows not sending the code_challenge parameter and skipping PKCE code verification, to support non-compliant providers.
- **skipCodeExchange** - (`boolean`) (default: false) just return the authorization response, instead of automatically exchanging the authorization code. This is useful if this exchange needs to be done manually (not client-side)

Expand Down
11 changes: 10 additions & 1 deletion android/src/main/java/com/rnappauth/RNAppAuthModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class RNAppAuthModule extends ReactContextBaseJavaModule implements Activ
private boolean dangerouslyAllowInsecureHttpRequests;
private Boolean skipCodeExchange;
private Boolean usePKCE;
private Boolean useNonce;
private String codeVerifier;
private String clientAuthMethod = "basic";
private Map<String, String> registrationRequestHeaders = null;
Expand Down Expand Up @@ -221,6 +222,7 @@ public void authorize(
final ReadableMap additionalParameters,
final ReadableMap serviceConfiguration,
final Boolean skipCodeExchange,
final Boolean useNonce,
final Boolean usePKCE,
final String clientAuthMethod,
final boolean dangerouslyAllowInsecureHttpRequests,
Expand All @@ -239,6 +241,7 @@ public void authorize(
this.clientSecret = clientSecret;
this.clientAuthMethod = clientAuthMethod;
this.skipCodeExchange = skipCodeExchange;
this.useNonce = useNonce;
this.usePKCE = usePKCE;

// when serviceConfiguration is provided, we don't need to hit up the OpenID well-known id endpoint
Expand All @@ -251,6 +254,7 @@ public void authorize(
clientId,
scopes,
redirectUrl,
useNonce,
usePKCE,
additionalParametersMap
);
Expand Down Expand Up @@ -281,6 +285,7 @@ public void onFetchConfigurationCompleted(
clientId,
scopes,
redirectUrl,
useNonce,
usePKCE,
additionalParametersMap
);
Expand Down Expand Up @@ -532,6 +537,7 @@ private void authorizeWithConfiguration(
final String clientId,
final ReadableArray scopes,
final String redirectUrl,
final Boolean useNonce,
final Boolean usePKCE,
final Map<String, String> additionalParametersMap
) {
Expand All @@ -557,7 +563,6 @@ private void authorizeWithConfiguration(
authRequestBuilder.setScope(scopesString);
}


if (additionalParametersMap != null) {
// handle additional parameters separately to avoid exceptions from AppAuth
if (additionalParametersMap.containsKey("display")) {
Expand Down Expand Up @@ -587,6 +592,10 @@ private void authorizeWithConfiguration(
authRequestBuilder.setCodeVerifier(this.codeVerifier);
}

if(!useNonce) {
authRequestBuilder.setNonce(null);
}

AuthorizationRequest authRequest = authRequestBuilder.build();

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export const authorize = ({
];

if (Platform.OS === 'android') {
nativeMethodArguments.push(useNonce);
nativeMethodArguments.push(usePKCE);
nativeMethodArguments.push(clientAuthMethod);
nativeMethodArguments.push(dangerouslyAllowInsecureHttpRequests);
Expand Down
5 changes: 5 additions & 0 deletions index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ describe('AppAuth', () => {
config.additionalParameters,
config.serviceConfiguration,
config.skipCodeExchange,
config.useNonce,
config.usePKCE,
config.clientAuthMethod,
false,
Expand All @@ -600,6 +601,7 @@ describe('AppAuth', () => {
config.additionalParameters,
config.serviceConfiguration,
false,
config.useNonce,
config.usePKCE,
config.clientAuthMethod,
false,
Expand All @@ -618,6 +620,7 @@ describe('AppAuth', () => {
config.additionalParameters,
config.serviceConfiguration,
false,
config.useNonce,
config.usePKCE,
config.clientAuthMethod,
true,
Expand Down Expand Up @@ -645,6 +648,7 @@ describe('AppAuth', () => {
config.additionalParameters,
config.serviceConfiguration,
false,
config.useNonce,
config.usePKCE,
config.clientAuthMethod,
false,
Expand Down Expand Up @@ -838,6 +842,7 @@ describe('AppAuth', () => {
config.additionalParameters,
config.serviceConfiguration,
false,
config.useNonce,
config.usePKCE,
config.clientAuthMethod,
false,
Expand Down
Loading

0 comments on commit b20561a

Please sign in to comment.