Skip to content

Commit

Permalink
fix ambiguous error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaAvraimov-okta committed Dec 21, 2021
1 parent 6e0596b commit 7319931
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
26 changes: 23 additions & 3 deletions library/src/main/java/com/okta/oidc/net/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;

import com.okta.oidc.util.AuthorizationException;
import com.okta.oidc.util.UriUtil;

import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -111,11 +114,28 @@ public JSONObject asJson() throws IOException, JSONException {
mStatusCode >= HttpURLConnection.HTTP_MULT_CHOICE) {
throw new HttpStatusCodeException(mStatusCode, mHttpClient.getResponseMessage());
}
InputStream is = getContent();
if (is == null) {
return getJsonObjectFromResponseInputStream(getContent());
}

public JSONObject asJsonWithErrorDescription() throws IOException, JSONException {
if (mStatusCode < HttpURLConnection.HTTP_OK ||
mStatusCode >= HttpURLConnection.HTTP_MULT_CHOICE) {
try {
return getJsonObjectFromResponseInputStream(getContent());
} catch (Exception any) {
throw new HttpStatusCodeException(mStatusCode, mHttpClient.getResponseMessage());
}
}
return getJsonObjectFromResponseInputStream(getContent());
}

private static JSONObject getJsonObjectFromResponseInputStream(
final InputStream inputStream
) throws IOException, JSONException {
if (inputStream == null) {
throw new IOException("Input stream must not be null");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
Writer writer = new StringWriter();
String line = reader.readLine();
while (line != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public TokenResponse executeRequest(OktaHttpClient client) throws AuthorizationE
TokenResponse tokenResponse;
try {
response = openConnection(client);
JSONObject json = response.asJson();
JSONObject json = response.asJsonWithErrorDescription();
if (json.has(AuthorizationException.PARAM_ERROR)) {
try {
final String error = json.getString(AuthorizationException.PARAM_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public String getRefreshToken() {
return refresh_token;
}

public void setRefreshToken() {
refresh_token = "fljsdhgfjkhsafjk";
}

public String getIdToken() {
return id_token;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public void refreshTokenFailure() throws InterruptedException {
latch.await();
assertNull(cb.getResult());
assertNotNull(cb.getException());
assertEquals(cb.getException().getMessage(), "Invalid status code 401 Client Error");
assertEquals(cb.getException().getMessage(), "No client credentials found.");
}

@Test
Expand Down

0 comments on commit 7319931

Please sign in to comment.