Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Account Manager not caching the authToken

Hi I'm having issues recovering my authToken when I call

mAccountManager.blockingGetAuthToken(Auth.getAccount(), Auth.AUTH_TOKEN_TYPE, true)

I get a null string back, which lead me to look into my AbstractAccountAuthenticator class, specifically getAuth(). Here's what its doing:

public Bundle getAuthToken(AccountAuthenticatorResponse response,
        Account account, String authTokenType, Bundle options)
        throws NetworkErrorException {

    final AccountManager am = AccountManager.get(mContext);

    String authToken = am.peekAuthToken(account, authTokenType);
    String uid = am.getUserData(account, AccountManager.KEY_CALLER_UID);


    // return bundle with authToken
    if (!TextUtils.isEmpty(authToken)) {
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
        result.putString(AccountManager.KEY_CALLER_UID, uid);
        return result;
    }


    return null;
}

The peekAuthToken is returning a null, however I am getting the correct uid from getUserData which lead me to believe I am adding the account correctly. This is how I set the authToken:

mAccountManager.addAccountExplicitly(account, accountPassword, extraData);
//The addAccount is working, and I can obtain the extraData in getAuth
mAccountManager.setAuthToken(account, Auth.AUTH_TOKEN_TYPE, authtoken);
//I assume this is where the authToken is to be cached…but I can't retrieve it…
//The token does exist at this point

Any suggestions?

like image 421
AIntel Avatar asked Aug 28 '14 22:08

AIntel


2 Answers

As you can read in the documentation, the peek method only gets the authToken from the authtoken-cache. If this is returning null, it means your authtoken has been invalidated, because otherwise the method AccountManager#getAuthToken would have returned you the cached one.

This is a bit confusing, but I'll try to explain.

You should be aware of that the getAuthToken from the AccountManager IS NOT the same as the getAuthToken-Method in the authenticator. The AccountManager is doing some caching in between. Means that if you call getAuthToken on the Manager, it'll return your AuthToken as long as it is in the cache WITHOUT calling the getAuthToken method of the Authenticator.

For my understanding that means, that it is totally pointless to call peek inside the getAuthToken Method.

How I handle this now:

In the implementation of getAuthToken (in the authenticator), I am re-requesting the authtoken from the server and update the account with the new token, which will store them in the cache. No need for peeking on that part.

like image 179
andre Avatar answered Oct 14 '22 05:10

andre


Make sure you add

setAccountAuthenticatorResult(authIntent.getExtras());
setResult(RESULT_OK,authIntent);

once you set authToken from your code.

like image 34
Aniruddh Ambarkar Avatar answered Oct 14 '22 05:10

Aniruddh Ambarkar