Skip to content

Commit a9f6960

Browse files
committed
Verify registered authenticator has expected package name
This ensures that expected authenticator will be used before an account is requested to be added. Without this check the account manager future will hang if another application has registered an authenticator for the account type id being used. Closes issue pockethub#88
1 parent 1623b2b commit a9f6960

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.accounts.AccountManager;
2424
import android.accounts.AccountManagerFuture;
2525
import android.accounts.AccountsException;
26+
import android.accounts.AuthenticatorDescription;
2627
import android.accounts.AuthenticatorException;
2728
import android.accounts.OperationCanceledException;
2829
import android.app.Activity;
@@ -50,11 +51,40 @@ public class AccountUtils {
5051

5152
private static final String TAG = "AccountUtils";
5253

54+
private static boolean AUTHENTICATOR_CHECKED;
55+
56+
private static boolean HAS_AUTHENTICATOR;
57+
5358
private static class AuthenticatorConflictException extends IOException {
5459

5560
private static final long serialVersionUID = 641279204734869183L;
5661
}
5762

63+
/**
64+
* Verify authenticator registered for account type matches the package name
65+
* of this application
66+
*
67+
* @param manager
68+
* @return true is authenticator registered, false otherwise
69+
*/
70+
public static boolean hasAuthenticator(final AccountManager manager) {
71+
if (!AUTHENTICATOR_CHECKED) {
72+
final AuthenticatorDescription[] types = manager
73+
.getAuthenticatorTypes();
74+
if (types != null && types.length > 0)
75+
for (AuthenticatorDescription descriptor : types)
76+
if (descriptor != null
77+
&& ACCOUNT_TYPE.equals(descriptor.type)) {
78+
HAS_AUTHENTICATOR = "com.github.mobile"
79+
.equals(descriptor.packageName);
80+
break;
81+
}
82+
AUTHENTICATOR_CHECKED = true;
83+
}
84+
85+
return HAS_AUTHENTICATOR;
86+
}
87+
5888
/**
5989
* Is the given user the owner of the default account?
6090
*
@@ -146,6 +176,9 @@ public static Account getAccount(final AccountManager manager,
146176

147177
Account[] accounts;
148178
try {
179+
if (!hasAuthenticator(manager))
180+
throw new AuthenticatorConflictException();
181+
149182
while ((accounts = getAccounts(manager)).length == 0) {
150183
if (loggable)
151184
Log.d(TAG, "No GitHub accounts for activity=" + activity);

0 commit comments

Comments
 (0)