Search in sources :

Example 46 with RefreshTokenRecord

use of com.microsoft.identity.common.internal.dto.RefreshTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method noValueForCacheKeyRefreshToken.

@Test
public void noValueForCacheKeyRefreshToken() {
    assertEquals(0, mSharedPreferencesAccountCredentialCache.getCredentials().size());
    final RefreshTokenRecord refreshToken = (RefreshTokenRecord) mSharedPreferencesAccountCredentialCache.getCredential(CACHE_VALUE_SEPARATOR + CredentialType.RefreshToken.name().toLowerCase() + CACHE_VALUE_SEPARATOR);
    assertNull(refreshToken);
}
Also used : PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) Test(org.junit.Test)

Example 47 with RefreshTokenRecord

use of com.microsoft.identity.common.internal.dto.RefreshTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method getCredentialsNoHomeAccountIdNoRealm.

@Test
public void getCredentialsNoHomeAccountIdNoRealm() {
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setSecret(SECRET);
    refreshToken.setHomeAccountId("Foo");
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setCredentialType(CredentialType.RefreshToken.name());
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setTarget(TARGET);
    final AccessTokenRecord accessToken = new AccessTokenRecord();
    accessToken.setCachedAt(CACHED_AT);
    accessToken.setExpiresOn(EXPIRES_ON);
    accessToken.setSecret(SECRET);
    accessToken.setHomeAccountId("Bar");
    accessToken.setRealm(REALM);
    accessToken.setEnvironment(ENVIRONMENT);
    accessToken.setCredentialType(CredentialType.AccessToken.name());
    accessToken.setClientId(CLIENT_ID);
    accessToken.setTarget(TARGET);
    final AccessTokenRecord accessToken2 = new AccessTokenRecord();
    accessToken2.setCachedAt(CACHED_AT);
    accessToken2.setExpiresOn(EXPIRES_ON);
    accessToken2.setSecret(SECRET);
    accessToken2.setHomeAccountId("Baz");
    accessToken2.setRealm(REALM);
    accessToken2.setEnvironment(ENVIRONMENT);
    accessToken2.setCredentialType(CredentialType.AccessToken.name());
    accessToken2.setClientId(CLIENT_ID);
    accessToken2.setTarget(TARGET);
    // Save the Credentials
    mSharedPreferencesAccountCredentialCache.saveCredential(refreshToken);
    mSharedPreferencesAccountCredentialCache.saveCredential(accessToken);
    mSharedPreferencesAccountCredentialCache.saveCredential(accessToken2);
    List<Credential> credentials = mSharedPreferencesAccountCredentialCache.getCredentialsFilteredBy(null, ENVIRONMENT, CredentialType.AccessToken, CLIENT_ID, null, TARGET, BEARER_AUTHENTICATION_SCHEME.getName());
    assertEquals(2, credentials.size());
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 48 with RefreshTokenRecord

use of com.microsoft.identity.common.internal.dto.RefreshTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method clearAll.

@Test
public void clearAll() {
    // Save an Account into the cache
    final AccountRecord account = new AccountRecord();
    account.setHomeAccountId(HOME_ACCOUNT_ID);
    account.setEnvironment(ENVIRONMENT);
    account.setRealm(REALM);
    account.setLocalAccountId(LOCAL_ACCOUNT_ID);
    account.setUsername(USERNAME);
    account.setAuthorityType(AUTHORITY_TYPE);
    mSharedPreferencesAccountCredentialCache.saveAccount(account);
    // Save an AccessToken into the cache
    final AccessTokenRecord accessToken = new AccessTokenRecord();
    accessToken.setCredentialType(CredentialType.AccessToken.name());
    accessToken.setHomeAccountId(HOME_ACCOUNT_ID);
    accessToken.setRealm("Foo");
    accessToken.setEnvironment(ENVIRONMENT);
    accessToken.setClientId(CLIENT_ID);
    accessToken.setTarget(TARGET);
    accessToken.setCachedAt(CACHED_AT);
    accessToken.setExpiresOn(EXPIRES_ON);
    accessToken.setSecret(SECRET);
    mSharedPreferencesAccountCredentialCache.saveCredential(accessToken);
    // Save a RefreshToken into the cache
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setCredentialType(CredentialType.RefreshToken.name());
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setSecret(SECRET);
    refreshToken.setTarget(TARGET);
    mSharedPreferencesAccountCredentialCache.saveCredential(refreshToken);
    // Call clearAll()
    mSharedPreferencesAccountCredentialCache.clearAll();
    // Verify getAccounts() returns zero items
    assertTrue(mSharedPreferencesAccountCredentialCache.getAccounts().isEmpty());
    // Verify getCredentials() returns zero items
    assertTrue(mSharedPreferencesAccountCredentialCache.getCredentials().isEmpty());
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 49 with RefreshTokenRecord

use of com.microsoft.identity.common.internal.dto.RefreshTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method persistAndRestoreExtraClaimsRefreshToken.

@Test
public void persistAndRestoreExtraClaimsRefreshToken() {
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setCredentialType(CredentialType.RefreshToken.name());
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setSecret(SECRET);
    refreshToken.setTarget(TARGET);
    // Create and set some additional field data...
    final String additionalKey = "extra-prop-1";
    final String additionalValue = "extra-value-1";
    final JsonElement additionalValueElement = new JsonPrimitive(additionalValue);
    final Map<String, JsonElement> additionalFields = new HashMap<>();
    additionalFields.put(additionalKey, additionalValueElement);
    refreshToken.setAdditionalFields(additionalFields);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveCredential(refreshToken);
    // Synthesize a cache key for it
    final String credentialCacheKey = mDelegate.generateCacheKey(refreshToken);
    // Resurrect the Credential
    final Credential restoredRefreshToken = mSharedPreferencesAccountCredentialCache.getCredential(credentialCacheKey);
    assertTrue(refreshToken.equals(restoredRefreshToken));
    assertEquals(additionalValue, restoredRefreshToken.getAdditionalFields().get(additionalKey).getAsString());
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) JsonPrimitive(com.google.gson.JsonPrimitive) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) Test(org.junit.Test)

Example 50 with RefreshTokenRecord

use of com.microsoft.identity.common.internal.dto.RefreshTokenRecord in project microsoft-authentication-library-common-for-android by AzureAD.

the class SharedPreferencesAccountCredentialCacheTest method testRefreshTokenMerge.

@Test
public void testRefreshTokenMerge() {
    final RefreshTokenRecord refreshTokenFirst = new RefreshTokenRecord();
    refreshTokenFirst.setCredentialType(CredentialType.RefreshToken.name());
    refreshTokenFirst.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshTokenFirst.setEnvironment(ENVIRONMENT);
    refreshTokenFirst.setClientId(CLIENT_ID);
    refreshTokenFirst.setCachedAt(CACHED_AT);
    refreshTokenFirst.setSecret(SECRET);
    // Create and set some additional field data...
    final String additionalKey = "extra-prop-1";
    final String additionalValue = "extra-value-1";
    final JsonElement additionalValueElement = new JsonPrimitive(additionalValue);
    final Map<String, JsonElement> additionalFields = new HashMap<>();
    additionalFields.put(additionalKey, additionalValueElement);
    refreshTokenFirst.setAdditionalFields(additionalFields);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveCredential(refreshTokenFirst);
    final RefreshTokenRecord refreshTokenSecond = new RefreshTokenRecord();
    refreshTokenSecond.setCredentialType(CredentialType.RefreshToken.name());
    refreshTokenSecond.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshTokenSecond.setEnvironment(ENVIRONMENT);
    refreshTokenSecond.setClientId(CLIENT_ID);
    refreshTokenSecond.setCachedAt(CACHED_AT);
    refreshTokenSecond.setSecret(SECRET);
    // Create and set some additional field data...
    final String additionalKey2 = "extra-prop-2";
    final String additionalValue2 = "extra-value-2";
    final JsonElement additionalValueElement2 = new JsonPrimitive(additionalValue2);
    final Map<String, JsonElement> additionalFields2 = new HashMap<>();
    additionalFields2.put(additionalKey2, additionalValueElement2);
    refreshTokenSecond.setAdditionalFields(additionalFields2);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveCredential(refreshTokenSecond);
    // Synthesize a cache key for it
    final String credentialCacheKey = mDelegate.generateCacheKey(refreshTokenFirst);
    // Resurrect the Credential
    final Credential restoredIdToken = mSharedPreferencesAccountCredentialCache.getCredential(credentialCacheKey);
    assertTrue(refreshTokenFirst.equals(restoredIdToken));
    assertEquals(additionalValue, restoredIdToken.getAdditionalFields().get(additionalKey).getAsString());
    assertEquals(additionalValue2, restoredIdToken.getAdditionalFields().get(additionalKey2).getAsString());
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) JsonPrimitive(com.google.gson.JsonPrimitive) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) Test(org.junit.Test)

Aggregations

RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)63 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)51 Test (org.junit.Test)51 Credential (com.microsoft.identity.common.internal.dto.Credential)32 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)29 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)10 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)9 JsonElement (com.google.gson.JsonElement)4 JsonPrimitive (com.google.gson.JsonPrimitive)4 HashMap (java.util.HashMap)4 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)3 JSONObject (org.json.JSONObject)3 Nullable (androidx.annotation.Nullable)2 ClientException (com.microsoft.identity.common.exception.ClientException)2 ClientInfo (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)2 CacheEndEvent (com.microsoft.identity.common.internal.telemetry.events.CacheEndEvent)2 JSONArray (org.json.JSONArray)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 StorageHelper (com.microsoft.identity.common.adal.internal.cache.StorageHelper)1