Search in sources :

Example 16 with IdTokenRecord

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

the class MsalOAuth2TokenCacheTest method testGetFamilyRefreshTokenForHomeAccountIdValidCase.

@Test
public void testGetFamilyRefreshTokenForHomeAccountIdValidCase() {
    // 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);
    accountCredentialCache.saveAccount(account);
    // Save an AccessToken into the cache
    final AccessTokenRecord accessToken = new AccessTokenRecord();
    accessToken.setCredentialType(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);
    accountCredentialCache.saveCredential(accessToken);
    // Save a Family RefreshToken into the cache
    final RefreshTokenRecord refreshToken = new RefreshTokenRecord();
    refreshToken.setCredentialType(RefreshToken.name());
    refreshToken.setEnvironment(ENVIRONMENT);
    refreshToken.setHomeAccountId(HOME_ACCOUNT_ID);
    refreshToken.setClientId(CLIENT_ID);
    refreshToken.setFamilyId("1");
    refreshToken.setSecret(SECRET);
    refreshToken.setTarget(TARGET);
    accountCredentialCache.saveCredential(refreshToken);
    final IdTokenRecord id = new IdTokenRecord();
    id.setHomeAccountId(HOME_ACCOUNT_ID);
    id.setEnvironment(ENVIRONMENT);
    id.setRealm(REALM2);
    id.setCredentialType(IdToken.name());
    id.setClientId(CLIENT_ID);
    id.setSecret(MOCK_ID_TOKEN_WITH_CLAIMS);
    id.setAuthority("https://sts.windows.net/0287f963-2d72-4363-9e3a-5705c5b0f031/");
    accountCredentialCache.saveCredential(id);
    final RefreshTokenRecord refreshTokenRecord = mOauth2TokenCache.getFamilyRefreshTokenForHomeAccountId(HOME_ACCOUNT_ID);
    assertNotNull(refreshTokenRecord);
    assertEquals(refreshTokenRecord.getSecret(), SECRET);
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) PrimaryRefreshTokenRecord(com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 17 with IdTokenRecord

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

the class SharedPreferencesAccountCredentialCacheTest method persistAndRestoreExtraClaimsIdToken.

@Test
public void persistAndRestoreExtraClaimsIdToken() {
    final IdTokenRecord idToken = new IdTokenRecord();
    idToken.setHomeAccountId(HOME_ACCOUNT_ID);
    idToken.setEnvironment(ENVIRONMENT);
    idToken.setRealm(REALM);
    idToken.setCredentialType(CredentialType.IdToken.name());
    idToken.setClientId(CLIENT_ID);
    idToken.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);
    idToken.setAdditionalFields(additionalFields);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveCredential(idToken);
    // Synthesize a cache key for it
    final String credentialCacheKey = mDelegate.generateCacheKey(idToken);
    // Resurrect the Credential
    final Credential restoredIdToken = mSharedPreferencesAccountCredentialCache.getCredential(credentialCacheKey);
    assertTrue(idToken.equals(restoredIdToken));
    assertEquals(additionalValue, restoredIdToken.getAdditionalFields().get(additionalKey).getAsString());
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) Credential(com.microsoft.identity.common.internal.dto.Credential) JsonPrimitive(com.google.gson.JsonPrimitive) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) Test(org.junit.Test)

Example 18 with IdTokenRecord

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

the class SharedPreferencesAccountCredentialCacheTest method saveIdToken.

@Test
public void saveIdToken() {
    final IdTokenRecord idToken = new IdTokenRecord();
    idToken.setHomeAccountId(HOME_ACCOUNT_ID);
    idToken.setEnvironment(ENVIRONMENT);
    idToken.setRealm(REALM);
    idToken.setCredentialType(CredentialType.IdToken.name());
    idToken.setClientId(CLIENT_ID);
    idToken.setSecret(SECRET);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveCredential(idToken);
    // Synthesize a cache key for it
    final String credentialCacheKey = mDelegate.generateCacheKey(idToken);
    // Resurrect the Credential
    final Credential restoredIdToken = mSharedPreferencesAccountCredentialCache.getCredential(credentialCacheKey);
    assertEquals(idToken.getHomeAccountId(), restoredIdToken.getHomeAccountId());
    assertEquals(idToken.getEnvironment(), restoredIdToken.getEnvironment());
    assertEquals(idToken.getCredentialType(), restoredIdToken.getCredentialType());
    assertEquals(idToken.getClientId(), restoredIdToken.getClientId());
    assertTrue(idToken.equals(restoredIdToken));
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) Credential(com.microsoft.identity.common.internal.dto.Credential) Test(org.junit.Test)

Example 19 with IdTokenRecord

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

the class SharedPreferencesAccountCredentialCacheTest method malformedJsonCacheValueForIdToken.

@Test
public void malformedJsonCacheValueForIdToken() {
    final IdTokenRecord idToken = new IdTokenRecord();
    idToken.setHomeAccountId(HOME_ACCOUNT_ID);
    idToken.setEnvironment(ENVIRONMENT);
    idToken.setCredentialType(CredentialType.IdToken.name());
    idToken.setClientId(CLIENT_ID);
    // Generate a cache key
    final String cacheKey = mDelegate.generateCacheKey(idToken);
    mSharedPreferencesFileManager.putString(cacheKey, "{\"thing\"  \"not an idToken\"}");
    final IdTokenRecord restoredIdToken = (IdTokenRecord) mSharedPreferencesAccountCredentialCache.getCredential(cacheKey);
    assertNull(restoredIdToken);
    assertNotNull(mSharedPreferencesFileManager.getString(cacheKey));
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) Test(org.junit.Test)

Example 20 with IdTokenRecord

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

the class SharedPreferencesAccountCredentialCacheTest method testIdTokenMerge.

@Test
public void testIdTokenMerge() {
    final IdTokenRecord idTokenFirst = new IdTokenRecord();
    idTokenFirst.setCredentialType(CredentialType.IdToken.name());
    idTokenFirst.setHomeAccountId(HOME_ACCOUNT_ID);
    idTokenFirst.setRealm(REALM);
    idTokenFirst.setEnvironment(ENVIRONMENT);
    idTokenFirst.setClientId(CLIENT_ID);
    idTokenFirst.setCachedAt(CACHED_AT);
    idTokenFirst.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);
    idTokenFirst.setAdditionalFields(additionalFields);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveCredential(idTokenFirst);
    final IdTokenRecord idTokenSecond = new IdTokenRecord();
    idTokenSecond.setCredentialType(CredentialType.IdToken.name());
    idTokenSecond.setHomeAccountId(HOME_ACCOUNT_ID);
    idTokenSecond.setRealm(REALM);
    idTokenSecond.setEnvironment(ENVIRONMENT);
    idTokenSecond.setClientId(CLIENT_ID);
    idTokenSecond.setCachedAt(CACHED_AT);
    idTokenSecond.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);
    idTokenSecond.setAdditionalFields(additionalFields2);
    // Save the Credential
    mSharedPreferencesAccountCredentialCache.saveCredential(idTokenSecond);
    // Synthesize a cache key for it
    final String credentialCacheKey = mDelegate.generateCacheKey(idTokenFirst);
    // Resurrect the Credential
    final Credential restoredIdToken = mSharedPreferencesAccountCredentialCache.getCredential(credentialCacheKey);
    assertTrue(idTokenFirst.equals(restoredIdToken));
    assertEquals(additionalValue, restoredIdToken.getAdditionalFields().get(additionalKey).getAsString());
    assertEquals(additionalValue2, restoredIdToken.getAdditionalFields().get(additionalKey2).getAsString());
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) Credential(com.microsoft.identity.common.internal.dto.Credential) JsonPrimitive(com.google.gson.JsonPrimitive) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) Test(org.junit.Test)

Aggregations

IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)31 Test (org.junit.Test)17 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)10 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)9 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)9 Credential (com.microsoft.identity.common.internal.dto.Credential)7 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)4 ArrayList (java.util.ArrayList)4 JsonElement (com.google.gson.JsonElement)3 JsonPrimitive (com.google.gson.JsonPrimitive)3 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)3 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3 ClientInfo (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)2 JSONArray (org.json.JSONArray)2 Nullable (androidx.annotation.Nullable)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 StorageHelper (com.microsoft.identity.common.adal.internal.cache.StorageHelper)1 ServiceException (com.microsoft.identity.common.exception.ServiceException)1