Search in sources :

Example 11 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 malformedCacheValueForIdToken.

@Test
public void malformedCacheValueForIdToken() {
    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);
    assertNull(mSharedPreferencesFileManager.getString(cacheKey));
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) Test(org.junit.Test)

Example 12 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 saveTokensWithAggregationV1MultiEntry.

@Test
public void saveTokensWithAggregationV1MultiEntry() throws ClientException {
    // Load additional creds into the cache to simulate a guest account...
    // at, id, account
    final AccessTokenRecord at = new AccessTokenRecord();
    at.setRealm(REALM2);
    at.setCachedAt(CACHED_AT);
    at.setExpiresOn(EXPIRES_ON);
    at.setSecret(SECRET);
    at.setHomeAccountId(HOME_ACCOUNT_ID);
    at.setEnvironment(ENVIRONMENT);
    at.setCredentialType(AccessToken.name());
    at.setClientId(CLIENT_ID);
    at.setTarget(TARGET);
    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/");
    final AccountRecord acct = new AccountRecord();
    acct.setAuthorityType(AUTHORITY_TYPE);
    acct.setLocalAccountId(UUID.randomUUID().toString());
    acct.setUsername(USERNAME);
    acct.setHomeAccountId(HOME_ACCOUNT_ID);
    acct.setEnvironment(ENVIRONMENT);
    acct.setRealm(REALM2);
    accountCredentialCache.saveAccount(acct);
    accountCredentialCache.saveCredential(at);
    accountCredentialCache.saveCredential(id);
    final List<ICacheRecord> result = loadTestBundleIntoCacheWithAggregation(defaultTestBundleV1);
    assertEquals(2, result.size());
    final ICacheRecord entry1 = result.get(0);
    assertNotNull(entry1.getAccount());
    assertNotNull(entry1.getV1IdToken());
    assertNotNull(entry1.getAccessToken());
    assertNotNull(entry1.getRefreshToken());
    final ICacheRecord entry2 = result.get(1);
    assertNotNull(entry2.getAccount());
    assertNotNull(entry2.getIdToken());
    assertNull(entry2.getAccessToken());
    assertNull(entry2.getRefreshToken());
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 13 with IdTokenRecord

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

the class TokenCacheAccessorTests method testMsalCacheIsUpdated.

/**
 * This test asserts that the MSAL cache is updated by writes to the ADAL cache.
 * The ADAL class {@link TokenCacheAccessor} receives an instance of the cache supplied by the host
 * app. If the caller has set an instance of {@link DefaultTokenCacheStore}, then ADAL should write a
 * matching ID, AT, and Account to the MSAL cache for migration/SSO purposes.
 */
@Test
public void testMsalCacheIsUpdated() throws ServiceException, MalformedURLException {
    // Assert our cache is configured for WW
    assertEquals(WORLDWIDE_AUTHORITY, mTokenCacheAccessor.getAuthorityUrlWithPreferredCache());
    // Create a request to WW
    final AuthenticationRequest request = new AuthenticationRequest(WORLDWIDE_AUTHORITY, RESOURCE, CLIENT, REDIRECT, "", PromptBehavior.Auto, "", UUID.randomUUID(), false, null);
    final AuthenticationResult result = new AuthenticationResult(MOCK_AT, MOCK_RT, new Date(System.currentTimeMillis() + (3600 * 1000)), false, new UserInfo(USERID_1, GIVEN_NAME, FAMILY_NAME, IDENTITY, USERID_1), TID, MOCK_ID_TOKEN_WITH_CLAIMS, null, CLIENT);
    result.setAuthority(WORLDWIDE_AUTHORITY);
    result.setClientInfo(new ClientInfo(MOCK_CLIENT_INFO));
    result.setResponseReceived(System.currentTimeMillis());
    result.setExpiresIn(TimeUnit.HOURS.toSeconds(1));
    // Save this to the cache
    mTokenCacheAccessor.updateTokenCache(request, result);
    assertEquals(WORLDWIDE_AUTHORITY, mTokenCacheAccessor.getAuthorityUrlWithPreferredCache());
    // Assert the MSAL replicated cache now contains the account & RT
    final IAccountCredentialCache accountCredentialCache = new SharedPreferencesAccountCredentialCache(new CacheKeyValueDelegate(), new SharedPreferencesFileManager(mContext, DEFAULT_ACCOUNT_CREDENTIAL_SHARED_PREFERENCES, new StorageHelper(mContext)));
    final MsalOAuth2TokenCache msalCache = new MsalOAuth2TokenCache(mContext, accountCredentialCache, new MicrosoftStsAccountCredentialAdapter());
    // Assert the presence of the account
    final AccountRecord accountRecord = msalCache.getAccount(LOGIN_WINDOWS_NET, CLIENT, MOCK_UID + "." + MOCK_UTID, MOCK_UTID);
    Assert.assertNotNull(accountRecord);
    // The RT
    final ICacheRecord cacheRecord = msalCache.load(CLIENT, null, accountRecord, new BearerAuthenticationSchemeInternal());
    final IdTokenRecord idToken = cacheRecord.getIdToken();
    final RefreshTokenRecord refreshToken = cacheRecord.getRefreshToken();
    Assert.assertEquals(MOCK_UTID, idToken.getRealm());
    Assert.assertEquals(CLIENT, idToken.getClientId());
    Assert.assertEquals(accountRecord.getHomeAccountId(), idToken.getHomeAccountId());
    Assert.assertEquals(LOGIN_WINDOWS_NET, refreshToken.getEnvironment());
    Assert.assertEquals(CLIENT, refreshToken.getClientId());
    Assert.assertEquals(accountRecord.getHomeAccountId(), refreshToken.getHomeAccountId());
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) BearerAuthenticationSchemeInternal(com.microsoft.identity.common.internal.authscheme.BearerAuthenticationSchemeInternal) SharedPreferencesFileManager(com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) MsalOAuth2TokenCache(com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache) IAccountCredentialCache(com.microsoft.identity.common.internal.cache.IAccountCredentialCache) Date(java.util.Date) CacheKeyValueDelegate(com.microsoft.identity.common.internal.cache.CacheKeyValueDelegate) MicrosoftStsAccountCredentialAdapter(com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter) SharedPreferencesAccountCredentialCache(com.microsoft.identity.common.internal.cache.SharedPreferencesAccountCredentialCache) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) StorageHelper(com.microsoft.identity.common.adal.internal.cache.StorageHelper) ClientInfo(com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo) Test(org.junit.Test)

Example 14 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 testGetFamilyRefreshTokenForHomeAccountIdNoAccountWithHomeAccountId.

@Test
public void testGetFamilyRefreshTokenForHomeAccountIdNoAccountWithHomeAccountId() {
    // 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(// different home account id
    "26685724-1f8e-4b97-a0ca-1863e33b9fb1");
    assertNull(refreshTokenRecord);
}
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 15 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 saveTokensWithAggregationMultiEntry.

@Test
public void saveTokensWithAggregationMultiEntry() throws ClientException {
    // Load additional creds into the cache to simulate a guest account...
    // at, id, account
    final AccessTokenRecord at = new AccessTokenRecord();
    at.setRealm(REALM2);
    at.setCachedAt(CACHED_AT);
    at.setExpiresOn(EXPIRES_ON);
    at.setSecret(SECRET);
    at.setHomeAccountId(HOME_ACCOUNT_ID);
    at.setEnvironment(ENVIRONMENT);
    at.setCredentialType(AccessToken.name());
    at.setClientId(CLIENT_ID);
    at.setTarget(TARGET);
    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/");
    final AccountRecord acct = new AccountRecord();
    acct.setAuthorityType(AUTHORITY_TYPE);
    acct.setLocalAccountId(UUID.randomUUID().toString());
    acct.setUsername(USERNAME);
    acct.setHomeAccountId(HOME_ACCOUNT_ID);
    acct.setEnvironment(ENVIRONMENT);
    acct.setRealm(REALM2);
    accountCredentialCache.saveAccount(acct);
    accountCredentialCache.saveCredential(at);
    accountCredentialCache.saveCredential(id);
    final List<ICacheRecord> result = loadTestBundleIntoCacheWithAggregation(defaultTestBundleV2);
    assertEquals(2, result.size());
    final ICacheRecord entry1 = result.get(0);
    assertNotNull(entry1.getAccount());
    assertNotNull(entry1.getIdToken());
    assertNotNull(entry1.getAccessToken());
    assertNotNull(entry1.getRefreshToken());
    final ICacheRecord entry2 = result.get(1);
    assertNotNull(entry2.getAccount());
    assertNotNull(entry2.getIdToken());
    assertNull(entry2.getAccessToken());
    assertNull(entry2.getRefreshToken());
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) 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