Skip to content

Commit

Permalink
fix (Android) Correctly parse AuthorizationException type other to ja…
Browse files Browse the repository at this point in the history
…vascript side (FormidableLabs#646)

AuthorizationExceptions from non standard oauth errors for example when the server returns a custom response do not have a exception.getLocalizedMessage() but only put the server response in exeption.error. With this fix, the Error object on the javascript is equal for iOS and Android.

Co-authored-by: Bob Smits <bob.smits@weareyou.com>
  • Loading branch information
bobsmits and Bob Smits authored Jun 10, 2021
1 parent 0f97aca commit 7f46adc
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions android/src/main/java/com/rnappauth/RNAppAuthModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,10 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
}

final AuthorizationResponse response = AuthorizationResponse.fromIntent(data);
AuthorizationException exception = AuthorizationException.fromIntent(data);
if (exception != null) {
AuthorizationException ex = AuthorizationException.fromIntent(data);
if (ex != null) {
if (promise != null) {
promise.reject(exception.error != null ? exception.error: "authentication_error", exception.getLocalizedMessage(), exception);
handleAuthorizationException("authentication_error", ex, promise);
}
return;
}
Expand Down Expand Up @@ -447,7 +447,7 @@ public void onTokenRequestCompleted(
}
} else {
if (promise != null) {
promise.reject(ex.error != null ? ex.error: "token_exchange_failed", ex.getLocalizedMessage(), ex);
handleAuthorizationException("token_exchange_failed", ex, promise);
}
}
}
Expand Down Expand Up @@ -514,7 +514,7 @@ public void onRegistrationRequestCompleted(@Nullable RegistrationResponse respon
WritableMap map = RegistrationResponseFactory.registrationResponseToMap(response);
promise.resolve(map);
} else {
promise.reject(ex.error != null ? ex.error: "registration_failed", ex.getLocalizedMessage(), ex);
handleAuthorizationException("registration_failed", ex, promise);
}
}
};
Expand Down Expand Up @@ -652,7 +652,7 @@ public void onTokenRequestCompleted(@Nullable TokenResponse response, @Nullable
WritableMap map = TokenResponseFactory.tokenResponseToMap(response);
promise.resolve(map);
} else {
promise.reject(ex.error != null ? ex.error: "token_refresh_failed", ex.getLocalizedMessage(), ex);
handleAuthorizationException("token_refresh_failed", ex, promise);
}
}
};
Expand Down Expand Up @@ -822,6 +822,14 @@ private AuthorizationServiceConfiguration getServiceConfiguration(@Nullable Stri
}
}

private void handleAuthorizationException(final String fallbackErrorCode, final AuthorizationException ex, final Promise promise) {
if (ex.getLocalizedMessage() == null) {
promise.reject(fallbackErrorCode, ex.error, ex);
} else {
promise.reject(ex.error != null ? ex.error: fallbackErrorCode, ex.getLocalizedMessage(), ex);
}
}

private void setServiceConfiguration(@Nullable String issuer, AuthorizationServiceConfiguration serviceConfiguration) {
if (issuer != null) {
mServiceConfigurations.put(issuer, serviceConfiguration);
Expand Down

0 comments on commit 7f46adc

Please sign in to comment.