Skip to content

Commit 48e7fe2

Browse files
committed
Add helper for determining 401 Unauthorized responses
IOExceptions are thrown certain Android versions with magic strings when a 401 is returned by the server but before the response can be parsed into a RequestException and RequestError. This helpers checks for these strings as well as when an actual unauthorized RequestException is raised.
1 parent 643b7d7 commit 48e7fe2

3 files changed

Lines changed: 36 additions & 18 deletions

File tree

‎app/src/main/java/com/github/mobile/ThrowableLoader.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.github.mobile;
1717

18-
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
1918
import android.accounts.Account;
2019
import android.accounts.AccountsException;
2120
import android.content.Context;
@@ -26,8 +25,6 @@
2625

2726
import java.io.IOException;
2827

29-
import org.eclipse.egit.github.core.client.RequestException;
30-
3128
/**
3229
* Loader that support throwing an exception when loading in the background
3330
*
@@ -64,8 +61,7 @@ public D load(final Account account) {
6461
try {
6562
return loadData();
6663
} catch (Exception e) {
67-
if (e instanceof RequestException
68-
&& HTTP_UNAUTHORIZED == ((RequestException) e).getStatus())
64+
if (AccountUtils.isUnauthorized(e))
6965
try {
7066
AccountUtils.updateAccount(account, activity);
7167
try {

‎app/src/main/java/com/github/mobile/accounts/AccountUtils.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static android.content.DialogInterface.BUTTON_POSITIVE;
2020
import static android.util.Log.DEBUG;
2121
import static com.github.mobile.accounts.AccountConstants.ACCOUNT_TYPE;
22+
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
2223
import android.accounts.Account;
2324
import android.accounts.AccountManager;
2425
import android.accounts.AccountManagerFuture;
@@ -33,6 +34,7 @@
3334
import android.content.DialogInterface.OnCancelListener;
3435
import android.content.DialogInterface.OnClickListener;
3536
import android.os.Bundle;
37+
import android.text.TextUtils;
3638
import android.util.Log;
3739

3840
import com.github.mobile.R.string;
@@ -43,6 +45,7 @@
4345
import java.util.List;
4446

4547
import org.eclipse.egit.github.core.User;
48+
import org.eclipse.egit.github.core.client.RequestException;
4649

4750
/**
4851
* Helpers for accessing {@link AccountManager}
@@ -282,4 +285,35 @@ public void onClick(DialogInterface dialog, int which) {
282285
});
283286
dialog.show();
284287
}
288+
289+
/**
290+
* Is the given {@link Exception} due to a 401 Unauthorized API response?
291+
*
292+
* @param e
293+
* @return true if 401, false otherwise
294+
*/
295+
public static boolean isUnauthorized(final Exception e) {
296+
if (e instanceof RequestException)
297+
return ((RequestException) e).getStatus() == HTTP_UNAUTHORIZED;
298+
299+
String message = null;
300+
if (e instanceof IOException)
301+
message = e.getMessage();
302+
final Throwable cause = e.getCause();
303+
if (cause instanceof IOException) {
304+
String causeMessage = cause.getMessage();
305+
if (!TextUtils.isEmpty(causeMessage))
306+
message = causeMessage;
307+
}
308+
309+
if (TextUtils.isEmpty(message))
310+
return false;
311+
312+
if ("Received authentication challenge is null".equals(message))
313+
return true;
314+
if ("No authentication challenges found".equals(message))
315+
return true;
316+
317+
return false;
318+
}
285319
}

‎app/src/main/java/com/github/mobile/accounts/LoginActivity.java‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070

7171
import org.eclipse.egit.github.core.User;
7272
import org.eclipse.egit.github.core.client.GitHubClient;
73-
import org.eclipse.egit.github.core.client.RequestException;
7473
import org.eclipse.egit.github.core.service.UserService;
7574

7675
import roboguice.inject.InjectView;
@@ -295,18 +294,7 @@ protected void onException(Exception e) throws RuntimeException {
295294

296295
Log.d(TAG, "Exception requesting authenticated user", e);
297296

298-
Throwable cause = e.getCause() != null ? e.getCause() : e;
299-
300-
boolean badCredentials = false;
301-
if (e instanceof RequestException
302-
&& ((RequestException) e).getStatus() == 401)
303-
badCredentials = true;
304-
// A 401 can be returned as an IOException with this message
305-
else if ("Received authentication challenge is null"
306-
.equals(cause.getMessage()))
307-
badCredentials = true;
308-
309-
if (badCredentials)
297+
if (AccountUtils.isUnauthorized(e))
310298
onAuthenticationResult(false);
311299
else
312300
ToastUtils.show(LoginActivity.this, e,

0 commit comments

Comments
 (0)