Skip to content

Commit

Permalink
Avoid null pointer exception because of error description (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
poovamraj authored Jul 18, 2023
1 parent 2d7bf6b commit 3761a13
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ private static Map<String, String> asMap(@Nullable String valueString) {
final String[] entries = valueString.length() > 0 ? valueString.split("&") : new String[]{};
Map<String, String> values = new HashMap<>(entries.length);
for (String entry : entries) {
final String[] value = entry.split("=");
if (value.length == 2) {
values.put(value[0], value[1]);
int idx = entry.indexOf("=");
final String key = idx > 0 ? entry.substring(0, idx) : entry;
final String value = idx > 0 && entry.length() > idx + 1 ? entry.substring(idx + 1) : null;
if (value != null) {
values.put(key, value);
}
}
return values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ internal class OAuthManager(
TAG,
"Error, access denied. Check that the required Permissions are granted and that the Application has this Connection configured in Auth0 Dashboard."
)
val unknownErrorDescription = "An unexpected error occurred."
when {
ERROR_VALUE_ACCESS_DENIED.equals(errorValue, ignoreCase = true) -> {
throw AuthenticationException(
Expand All @@ -200,16 +201,16 @@ internal class OAuthManager(
)
}
ERROR_VALUE_UNAUTHORIZED.equals(errorValue, ignoreCase = true) -> {
throw AuthenticationException(ERROR_VALUE_UNAUTHORIZED, errorDescription!!)
throw AuthenticationException(ERROR_VALUE_UNAUTHORIZED, errorDescription ?: unknownErrorDescription)
}
ERROR_VALUE_LOGIN_REQUIRED == errorValue -> {
//Whitelist to allow SSO errors go through
throw AuthenticationException(errorValue, errorDescription!!)
throw AuthenticationException(errorValue, errorDescription ?: unknownErrorDescription)
}
else -> {
throw AuthenticationException(
errorValue,
errorDescription ?: "An unexpected error occurred."
errorDescription ?: unknownErrorDescription
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ public void shouldGetNullCallbackURIIfInvalidDomain() {
assertThat(uri, nullValue());
}

@Test
public void shouldParseQueryValuesWithEqual() {
String uriString = "testandroid://auth.testandroid.com/android/com.testandroid.app/callback?error=unauthorized&error_description=Please%20verify%20your%20email%20before%20logging%20in.e%3Dfoo%2Bt2%40gmail.com&state=abscefg-QWERTYUIOPasdfgHJKLMNBVCXZdd";
Uri uri = Uri.parse(uriString);
final Map<String, String> values = CallbackHelper.getValuesFromUri(uri);

assertThat(values, is(notNullValue()));
assertThat(values, aMapWithSize(3));
assertThat(values, hasEntry("error", "unauthorized"));
assertThat(values, hasEntry("state", "abscefg-QWERTYUIOPasdfgHJKLMNBVCXZdd"));
assertThat(values, hasEntry("error_description", "Please verify your email before logging [email protected]"));
}

@Test
public void shouldParseQueryValues() {
String uriString = "https://lbalmaceda.auth0.com/android/com.auth0.android.lock.app/callback?code=soMec0d3ML8B&state=810132b-486aa-4aa8-1768-a1dcd3368fae";
Expand Down

0 comments on commit 3761a13

Please sign in to comment.