Merge "Add expiration time to the response when token is returned from the cached. We only need to add expiration time for the custom tokens" into tm-dev
This commit is contained in:
@@ -2977,19 +2977,21 @@ public class AccountManagerService
|
|||||||
* outside of those expected to be injected by the AccountManager, e.g.
|
* outside of those expected to be injected by the AccountManager, e.g.
|
||||||
* ANDORID_PACKAGE_NAME.
|
* ANDORID_PACKAGE_NAME.
|
||||||
*/
|
*/
|
||||||
String token = readCachedTokenInternal(
|
TokenCache.Value cachedToken = readCachedTokenInternal(
|
||||||
accounts,
|
accounts,
|
||||||
account,
|
account,
|
||||||
authTokenType,
|
authTokenType,
|
||||||
callerPkg,
|
callerPkg,
|
||||||
callerPkgSigDigest);
|
callerPkgSigDigest);
|
||||||
if (token != null) {
|
if (cachedToken != null) {
|
||||||
logGetAuthTokenMetrics(callerPkg, account.type);
|
logGetAuthTokenMetrics(callerPkg, account.type);
|
||||||
if (Log.isLoggable(TAG, Log.VERBOSE)) {
|
if (Log.isLoggable(TAG, Log.VERBOSE)) {
|
||||||
Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator.");
|
Log.v(TAG, "getAuthToken: cache hit ofr custom token authenticator.");
|
||||||
}
|
}
|
||||||
Bundle result = new Bundle();
|
Bundle result = new Bundle();
|
||||||
result.putString(AccountManager.KEY_AUTHTOKEN, token);
|
result.putString(AccountManager.KEY_AUTHTOKEN, cachedToken.token);
|
||||||
|
result.putLong(AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY,
|
||||||
|
cachedToken.expiryEpochMillis);
|
||||||
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
|
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
|
||||||
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
|
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
|
||||||
onResult(response, result);
|
onResult(response, result);
|
||||||
@@ -6121,7 +6123,7 @@ public class AccountManagerService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String readCachedTokenInternal(
|
protected TokenCache.Value readCachedTokenInternal(
|
||||||
UserAccounts accounts,
|
UserAccounts accounts,
|
||||||
Account account,
|
Account account,
|
||||||
String tokenType,
|
String tokenType,
|
||||||
|
|||||||
@@ -20,8 +20,6 @@ import android.accounts.Account;
|
|||||||
import android.util.LruCache;
|
import android.util.LruCache;
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
|
|
||||||
import com.android.internal.util.Preconditions;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -35,7 +33,8 @@ import java.util.Objects;
|
|||||||
|
|
||||||
private static final int MAX_CACHE_CHARS = 64000;
|
private static final int MAX_CACHE_CHARS = 64000;
|
||||||
|
|
||||||
private static class Value {
|
/** Package private*/
|
||||||
|
static class Value {
|
||||||
public final String token;
|
public final String token;
|
||||||
public final long expiryEpochMillis;
|
public final long expiryEpochMillis;
|
||||||
|
|
||||||
@@ -217,12 +216,12 @@ import java.util.Objects;
|
|||||||
/**
|
/**
|
||||||
* Gets a token from the cache if possible.
|
* Gets a token from the cache if possible.
|
||||||
*/
|
*/
|
||||||
public String get(Account account, String tokenType, String packageName, byte[] sigDigest) {
|
public Value get(Account account, String tokenType, String packageName, byte[] sigDigest) {
|
||||||
Key k = new Key(account, tokenType, packageName, sigDigest);
|
Key k = new Key(account, tokenType, packageName, sigDigest);
|
||||||
Value v = mCachedTokens.get(k);
|
Value v = mCachedTokens.get(k);
|
||||||
long currentTime = System.currentTimeMillis();
|
long currentTime = System.currentTimeMillis();
|
||||||
if (v != null && currentTime < v.expiryEpochMillis) {
|
if (v != null && currentTime < v.expiryEpochMillis) {
|
||||||
return v.token;
|
return v;
|
||||||
} else if (v != null) {
|
} else if (v != null) {
|
||||||
remove(account.type, v.token);
|
remove(account.type, v.token);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,4 +18,5 @@
|
|||||||
android:accountType="@string/test_account_type1"
|
android:accountType="@string/test_account_type1"
|
||||||
android:icon="@drawable/icon1"
|
android:icon="@drawable/icon1"
|
||||||
android:smallIcon="@drawable/icon1"
|
android:smallIcon="@drawable/icon1"
|
||||||
|
android:customTokens="true"
|
||||||
android:label="@string/test_account_type1_authenticator_label" />
|
android:label="@string/test_account_type1_authenticator_label" />
|
||||||
|
|||||||
@@ -26,9 +26,11 @@ import static org.mockito.Matchers.eq;
|
|||||||
import static org.mockito.Mockito.atLeast;
|
import static org.mockito.Mockito.atLeast;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.nullable;
|
import static org.mockito.Mockito.nullable;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.accounts.AbstractAccountAuthenticator;
|
||||||
import android.accounts.Account;
|
import android.accounts.Account;
|
||||||
import android.accounts.AccountManager;
|
import android.accounts.AccountManager;
|
||||||
import android.accounts.AccountManagerInternal;
|
import android.accounts.AccountManagerInternal;
|
||||||
@@ -1698,13 +1700,14 @@ public class AccountManagerServiceTest extends AndroidTestCase {
|
|||||||
|
|
||||||
final CountDownLatch latch = new CountDownLatch(1);
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
Response response = new Response(latch, mMockAccountManagerResponse);
|
Response response = new Response(latch, mMockAccountManagerResponse);
|
||||||
|
long expiryEpochTimeInMillis = System.currentTimeMillis() + ONE_DAY_IN_MILLISECOND;
|
||||||
mAms.getAuthToken(
|
mAms.getAuthToken(
|
||||||
response, // response
|
response, // response
|
||||||
AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS,
|
AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS,
|
||||||
"authTokenType", // authTokenType
|
"authTokenType", // authTokenType
|
||||||
true, // notifyOnAuthFailure
|
true, // notifyOnAuthFailure
|
||||||
false, // expectActivityLaunch
|
false, // expectActivityLaunch
|
||||||
createGetAuthTokenOptions());
|
createGetAuthTokenOptionsWithExpiry(expiryEpochTimeInMillis));
|
||||||
waitForLatch(latch);
|
waitForLatch(latch);
|
||||||
|
|
||||||
verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture());
|
verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture());
|
||||||
@@ -1715,6 +1718,58 @@ public class AccountManagerServiceTest extends AndroidTestCase {
|
|||||||
AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS);
|
AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS);
|
||||||
assertEquals(result.getString(AccountManager.KEY_ACCOUNT_TYPE),
|
assertEquals(result.getString(AccountManager.KEY_ACCOUNT_TYPE),
|
||||||
AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1);
|
AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1);
|
||||||
|
assertEquals(result.getLong(AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY),
|
||||||
|
expiryEpochTimeInMillis);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SmallTest
|
||||||
|
public void testGetAuthTokenCachedSuccess() throws Exception {
|
||||||
|
unlockSystemUser();
|
||||||
|
when(mMockContext.createPackageContextAsUser(
|
||||||
|
anyString(), anyInt(), any(UserHandle.class))).thenReturn(mMockContext);
|
||||||
|
when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager);
|
||||||
|
String[] list = new String[]{AccountManagerServiceTestFixtures.CALLER_PACKAGE};
|
||||||
|
when(mMockPackageManager.getPackagesForUid(anyInt())).thenReturn(list);
|
||||||
|
|
||||||
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
Response response = new Response(latch, mMockAccountManagerResponse);
|
||||||
|
long expiryEpochTimeInMillis = System.currentTimeMillis() + ONE_DAY_IN_MILLISECOND;
|
||||||
|
mAms.getAuthToken(
|
||||||
|
response, // response
|
||||||
|
AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS,
|
||||||
|
"authTokenType", // authTokenType
|
||||||
|
true, // notifyOnAuthFailure
|
||||||
|
false, // expectActivityLaunch
|
||||||
|
createGetAuthTokenOptionsWithExpiry(expiryEpochTimeInMillis));
|
||||||
|
waitForLatch(latch);
|
||||||
|
|
||||||
|
// Make call for cached token.
|
||||||
|
mAms.getAuthToken(
|
||||||
|
response, // response
|
||||||
|
AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS,
|
||||||
|
"authTokenType", // authTokenType
|
||||||
|
true, // notifyOnAuthFailure
|
||||||
|
false, // expectActivityLaunch
|
||||||
|
createGetAuthTokenOptionsWithExpiry(expiryEpochTimeInMillis + 10));
|
||||||
|
waitForLatch(latch);
|
||||||
|
|
||||||
|
verify(mMockAccountManagerResponse, times(2)).onResult(mBundleCaptor.capture());
|
||||||
|
List<Bundle> result = mBundleCaptor.getAllValues();
|
||||||
|
assertGetTokenResponse(result.get(0), expiryEpochTimeInMillis);
|
||||||
|
// cached token was returned with the same expiration time as first token.
|
||||||
|
assertGetTokenResponse(result.get(1), expiryEpochTimeInMillis);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertGetTokenResponse(Bundle result, long expiryEpochTimeInMillis) {
|
||||||
|
assertEquals(result.getString(AccountManager.KEY_AUTHTOKEN),
|
||||||
|
AccountManagerServiceTestFixtures.AUTH_TOKEN);
|
||||||
|
assertEquals(result.getString(AccountManager.KEY_ACCOUNT_NAME),
|
||||||
|
AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS);
|
||||||
|
assertEquals(result.getString(AccountManager.KEY_ACCOUNT_TYPE),
|
||||||
|
AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1);
|
||||||
|
assertEquals(result.getLong(AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY),
|
||||||
|
expiryEpochTimeInMillis);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SmallTest
|
@SmallTest
|
||||||
@@ -3241,11 +3296,16 @@ public class AccountManagerServiceTest extends AndroidTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Bundle createGetAuthTokenOptions() {
|
private Bundle createGetAuthTokenOptions() {
|
||||||
|
return createGetAuthTokenOptionsWithExpiry(
|
||||||
|
System.currentTimeMillis() + ONE_DAY_IN_MILLISECOND);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Bundle createGetAuthTokenOptionsWithExpiry(long expiryEpochTimeInMillis) {
|
||||||
Bundle options = new Bundle();
|
Bundle options = new Bundle();
|
||||||
options.putString(AccountManager.KEY_ANDROID_PACKAGE_NAME,
|
options.putString(AccountManager.KEY_ANDROID_PACKAGE_NAME,
|
||||||
AccountManagerServiceTestFixtures.CALLER_PACKAGE);
|
AccountManagerServiceTestFixtures.CALLER_PACKAGE);
|
||||||
options.putLong(AccountManagerServiceTestFixtures.KEY_TOKEN_EXPIRY,
|
options.putLong(AccountManagerServiceTestFixtures.KEY_TOKEN_EXPIRY,
|
||||||
System.currentTimeMillis() + ONE_DAY_IN_MILLISECOND);
|
expiryEpochTimeInMillis);
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user