Skip to content

Commit

Permalink
Retrieve password or token each time it is requested
Browse files Browse the repository at this point in the history
Previously the field on GitHubAccount were not being updated
if the account was updated due to authentication issues.
  • Loading branch information
kevinsawicki committed Oct 5, 2012
1 parent 63e1f1c commit 47af07d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ protected HttpURLConnection configureRequest(final HttpURLConnection request) {
Log.d(TAG, "Authenticating using " + account);

// Credentials setting must come before super call
if(!TextUtils.isEmpty(account.authToken)) // Use token if it exists
setOAuth2Token(account.authToken);
String token = account.getAuthToken();
if (!TextUtils.isEmpty(token))
setOAuth2Token(token);
else
setCredentials(account.username, account.password);
setCredentials(account.getUsername(), account.getPassword());

return super.configureRequest(request);
}
Expand Down
39 changes: 1 addition & 38 deletions app/src/main/java/com/github/mobile/accounts/AccountScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,14 @@
*/
package com.github.mobile.accounts;

import static android.accounts.AccountManager.KEY_AUTHTOKEN;
import static com.github.mobile.accounts.AccountConstants.ACCOUNT_TYPE;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.google.inject.AbstractModule;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.OutOfScopeException;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -44,8 +35,6 @@ public class AccountScope extends ScopeBase {
private static final Key<GitHubAccount> GITHUB_ACCOUNT_KEY = Key
.get(GitHubAccount.class);

private static final String TAG = "GitHubAccountScope";

/**
* Create new module
*
Expand Down Expand Up @@ -77,33 +66,7 @@ AccountScope.<GitHubAccount> seededKeyProvider()).in(
*/
public void enterWith(final Account account,
final AccountManager accountManager) {
@SuppressWarnings("deprecation")
AccountManagerFuture<Bundle> future = accountManager.getAuthToken(
account, ACCOUNT_TYPE, false, null, null);

String authToken;
try {
authToken = future.getResult().getString(KEY_AUTHTOKEN);
} catch (AuthenticatorException ae) {
authToken = null;
// Authenticator failed to respond
Log.e(TAG, ae.getMessage());
} catch (OperationCanceledException oce) {
authToken = null;
// User canceled operation
Log.e(TAG, oce.getMessage());
} catch (IOException ioe) {
authToken = null;
// Possible network issues
Log.e(TAG, ioe.getMessage());
}

String password;
if (TextUtils.isEmpty(authToken))
password = accountManager.getPassword(account);
else
password = null;
enterWith(new GitHubAccount(account.name, password, authToken));
enterWith(new GitHubAccount(account, accountManager));
}

/**
Expand Down
70 changes: 55 additions & 15 deletions app/src/main/java/com/github/mobile/accounts/GitHubAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,81 @@
*/
package com.github.mobile.accounts;

import static android.accounts.AccountManager.KEY_AUTHTOKEN;
import static com.github.mobile.accounts.AccountConstants.ACCOUNT_TYPE;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.accounts.AccountsException;
import android.os.Bundle;
import android.util.Log;

import java.io.IOException;

/**
* GitHub account model
*/
public class GitHubAccount {

private static final String TAG = "GitHubAccount";

private final Account account;

private final AccountManager manager;

/**
* Account username
* Create account wrapper
*
* @param account
* @param manager
*/
public final String username;
public GitHubAccount(final Account account, final AccountManager manager) {
this.account = account;
this.manager = manager;
}

/**
* Account password
* Get username
*
* @return username
*/
public final String password;
public String getUsername() {
return account.name;
}

/**
* Account OAuth token
* Get password
*
* @return password
*/
public final String authToken;
public String getPassword() {
return manager.getPassword(account);
}

/**
* Create account with username and password
* Get auth token
*
* @param username
* @param password
* @param authToken
* @return token
*/
public GitHubAccount(String username, String password, String authToken) {
this.username = username;
this.password = password;
this.authToken = authToken;
public String getAuthToken() {
@SuppressWarnings("deprecation")
AccountManagerFuture<Bundle> future = manager.getAuthToken(account,
ACCOUNT_TYPE, false, null, null);

try {
Bundle result = future.getResult();
return result != null ? result.getString(KEY_AUTHTOKEN) : null;
} catch (AccountsException e) {
Log.e(TAG, "Auth token lookup failed", e);
return null;
} catch (IOException e) {
Log.e(TAG, "Auth token lookup failed", e);
return null;
}
}

@Override
public String toString() {
return getClass().getSimpleName() + '[' + username + ']';
return getClass().getSimpleName() + '[' + account.name + ']';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class UserComparator implements Comparator<User> {
*/
@Inject
public UserComparator(final GitHubAccount account) {
login = account.username;
login = account.getUsername();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public int compare(final Repository repo1,
}

private boolean isAuthenticatedUser() {
return org.getLogin().equals(accountProvider.get().username);
return org.getLogin().equals(accountProvider.get().getUsername());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ protected ResourcePager<Gist> createPager() {

@Override
public PageIterator<Gist> createIterator(int page, int size) {
return service.pageGists(accountProvider.get().username, page,
size);
return service.pageGists(accountProvider.get().getUsername(),
page, size);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.github.mobile.tests.user;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.test.AndroidTestCase;

import com.github.mobile.accounts.GitHubAccount;
Expand All @@ -31,7 +33,8 @@ public class UserComparatorTest extends AndroidTestCase {
* Test sorting of users that match login
*/
public void testLoginMatch() {
GitHubAccount account = new GitHubAccount("m", "n", "a");
GitHubAccount account = new GitHubAccount(new Account("m", "t"),
AccountManager.get(getContext()));
UserComparator comparator = new UserComparator(account);

assertTrue(comparator.compare(new User().setLogin("m"),
Expand All @@ -52,7 +55,8 @@ public void testLoginMatch() {
* Test sorting of users that don't match login
*/
public void testNoLoginMatch() {
GitHubAccount account = new GitHubAccount("m", "n", "a");
GitHubAccount account = new GitHubAccount(new Account("m", "t"),
AccountManager.get(getContext()));
UserComparator comparator = new UserComparator(account);

assertTrue(comparator.compare(new User().setLogin("a"),
Expand Down

0 comments on commit 47af07d

Please sign in to comment.