Skip to content

Commit 505f050

Browse files
authored
Merge pull request #144 from mattinger/master
Check Response Code before using conn.getInputStream
2 parents ea04fef + e8c6a32 commit 505f050

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

library/java/net/openid/appauth/AuthorizationService.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,12 @@ protected JSONObject doInBackground(Void... voids) {
345345
wr.write(queryData);
346346
wr.flush();
347347

348-
is = conn.getInputStream();
348+
if (conn.getResponseCode() >= HttpURLConnection.HTTP_OK
349+
&& conn.getResponseCode() < HttpURLConnection.HTTP_MULT_CHOICE) {
350+
is = conn.getInputStream();
351+
} else {
352+
is = conn.getErrorStream();
353+
}
349354
String response = Utils.readInputStream(is);
350355
return new JSONObject(response);
351356
} catch (IOException ex) {
@@ -378,7 +383,7 @@ protected void onPostExecute(JSONObject json) {
378383
error,
379384
json.getString(AuthorizationException.PARAM_ERROR_DESCRIPTION),
380385
UriUtil.parseUriIfAvailable(
381-
json.getString(AuthorizationException.PARAM_ERROR_URI)));
386+
json.optString(AuthorizationException.PARAM_ERROR_URI)));
382387
} catch (JSONException jsonEx) {
383388
ex = AuthorizationException.fromTemplate(
384389
GeneralErrors.JSON_DESERIALIZATION_ERROR,

library/javatests/net/openid/appauth/AuthorizationServiceTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ public class AuthorizationServiceTest {
9494
+ " \"application_type\": " + RegistrationRequest.APPLICATION_TYPE_NATIVE + "\n"
9595
+ "}";
9696

97+
private static final String INVALID_GRANT_RESPONSE_JSON = "{\n"
98+
+ " \"error\": \"invalid_grant\",\n"
99+
+ " \"error_description\": \"invalid_grant description\"\n"
100+
+ "}";
101+
private static final int TEST_INVALID_GRANT_CODE = 2002;
102+
97103
private AuthorizationCallback mAuthCallback;
98104
private RegistrationCallback mRegistrationCallback;
99105
private AuthorizationService mService;
@@ -170,6 +176,7 @@ public void testAuthorizationRequest_afterDispose() throws Exception {
170176
public void testTokenRequest() throws Exception {
171177
InputStream is = new ByteArrayInputStream(AUTH_CODE_EXCHANGE_RESPONSE_JSON.getBytes());
172178
when(mHttpConnection.getInputStream()).thenReturn(is);
179+
when(mHttpConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
173180
TokenRequest request = getTestAuthCodeExchangeRequest();
174181
mService.performTokenRequest(request, mAuthCallback);
175182
mAuthCallback.waitForCallback();
@@ -182,6 +189,7 @@ public void testTokenRequest() throws Exception {
182189
public void testTokenRequest_withBasicAuth() throws Exception {
183190
ClientSecretBasic csb = new ClientSecretBasic(TEST_CLIENT_SECRET);
184191
InputStream is = new ByteArrayInputStream(AUTH_CODE_EXCHANGE_RESPONSE_JSON.getBytes());
192+
when(mHttpConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
185193
when(mHttpConnection.getInputStream()).thenReturn(is);
186194
TokenRequest request = getTestAuthCodeExchangeRequest();
187195
mService.performTokenRequest(request, csb, mAuthCallback);
@@ -198,6 +206,7 @@ public void testTokenRequest_withPostAuth() throws Exception {
198206
ClientSecretPost csp = new ClientSecretPost(TEST_CLIENT_SECRET);
199207
InputStream is = new ByteArrayInputStream(AUTH_CODE_EXCHANGE_RESPONSE_JSON.getBytes());
200208
when(mHttpConnection.getInputStream()).thenReturn(is);
209+
when(mHttpConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
201210
TokenRequest request = getTestAuthCodeExchangeRequest();
202211
mService.performTokenRequest(request, csp, mAuthCallback);
203212
mAuthCallback.waitForCallback();
@@ -209,10 +218,35 @@ public void testTokenRequest_withPostAuth() throws Exception {
209218
assertTokenRequestBody(postBody, expectedRequestBody);
210219
}
211220

221+
@Test
222+
public void testTokenRequest_withInvalidGrant() throws Exception {
223+
ClientSecretPost csp = new ClientSecretPost(TEST_CLIENT_SECRET);
224+
InputStream is = new ByteArrayInputStream(INVALID_GRANT_RESPONSE_JSON.getBytes());
225+
when(mHttpConnection.getErrorStream()).thenReturn(is);
226+
when(mHttpConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_BAD_REQUEST);
227+
TokenRequest request = getTestAuthCodeExchangeRequest();
228+
mService.performTokenRequest(request, csp, mAuthCallback);
229+
mAuthCallback.waitForCallback();
230+
assertInvalidGrant(mAuthCallback.error);
231+
}
232+
233+
@Test
234+
public void testTokenRequest_withInvalidGrant2() throws Exception {
235+
ClientSecretPost csp = new ClientSecretPost(TEST_CLIENT_SECRET);
236+
InputStream is = new ByteArrayInputStream(INVALID_GRANT_RESPONSE_JSON.getBytes());
237+
when(mHttpConnection.getErrorStream()).thenReturn(is);
238+
when(mHttpConnection.getResponseCode()).thenReturn(199);
239+
TokenRequest request = getTestAuthCodeExchangeRequest();
240+
mService.performTokenRequest(request, csp, mAuthCallback);
241+
mAuthCallback.waitForCallback();
242+
assertInvalidGrant(mAuthCallback.error);
243+
}
244+
212245
@Test
213246
public void testTokenRequest_IoException() throws Exception {
214247
Exception ex = new IOException();
215248
when(mHttpConnection.getInputStream()).thenThrow(ex);
249+
when(mHttpConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
216250
mService.performTokenRequest(getTestAuthCodeExchangeRequest(), mAuthCallback);
217251
mAuthCallback.waitForCallback();
218252
assertNotNull(mAuthCallback.error);
@@ -272,6 +306,14 @@ private void assertTokenResponse(TokenResponse response, TokenRequest expectedRe
272306
assertEquals(TEST_ID_TOKEN, response.idToken);
273307
}
274308

309+
private void assertInvalidGrant(AuthorizationException error) {
310+
assertNotNull(error);
311+
assertEquals(AuthorizationException.TYPE_OAUTH_TOKEN_ERROR, error.type);
312+
assertEquals(TEST_INVALID_GRANT_CODE, error.code);
313+
assertEquals("invalid_grant", error.error);
314+
assertEquals("invalid_grant description", error.errorDescription);
315+
}
316+
275317
private void assertRegistrationResponse(RegistrationResponse response,
276318
RegistrationRequest expectedRequest) {
277319
assertThat(response).isNotNull();

0 commit comments

Comments
 (0)