Search in sources :

Example 21 with ICacheRecord

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

the class MsalOAuth2TokenCacheTest method saveTokensWithAggregationSingleEntryWithMalformedDataInCache.

@Test
public void saveTokensWithAggregationSingleEntryWithMalformedDataInCache() throws ClientException {
    // Prepopulate the cache with unparseable, junk data
    mSharedPreferencesFileManager.putString(JUNK_KEY, JUNK_VALUE);
    final List<ICacheRecord> result = loadTestBundleIntoCacheWithAggregation(defaultTestBundleV2);
    assertEquals(1, result.size());
    final ICacheRecord entry = result.get(0);
    assertEquals(defaultTestBundleV2.mGeneratedAccount, entry.getAccount());
    assertEquals(defaultTestBundleV2.mGeneratedIdToken, entry.getIdToken());
    assertEquals(defaultTestBundleV2.mGeneratedAccessToken, entry.getAccessToken());
    assertEquals(defaultTestBundleV2.mGeneratedRefreshToken, entry.getRefreshToken());
    // Verify that our junk data still exists
    assertEquals(JUNK_VALUE, mSharedPreferencesFileManager.getString(JUNK_KEY));
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) Test(org.junit.Test)

Example 22 with ICacheRecord

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

the class MsalOAuth2TokenCacheTest method saveAccountDirect.

private void saveAccountDirect(@NonNull final AccountCredentialTestBundle testBundle) {
    mOauth2TokenCache.save(testBundle.mGeneratedAccount, testBundle.mGeneratedIdToken);
    final AccountRecord account = mOauth2TokenCache.getAccount(ENVIRONMENT, CLIENT_ID, HOME_ACCOUNT_ID, REALM);
    final ICacheRecord cacheRecord = mOauth2TokenCache.load(CLIENT_ID, TARGET, account, BEARER_AUTHENTICATION_SCHEME);
    assertNotNull(cacheRecord);
    assertNotNull(cacheRecord.getAccount());
    if (testBundle == defaultTestBundleV2) {
        assertNotNull(cacheRecord.getIdToken());
        assertNull(cacheRecord.getV1IdToken());
        assertEquals(testBundle.mGeneratedIdToken, cacheRecord.getIdToken());
    } else {
        assertNotNull(cacheRecord.getV1IdToken());
        assertNull(cacheRecord.getIdToken());
        assertEquals(testBundle.mGeneratedIdToken, cacheRecord.getV1IdToken());
    }
    assertNull(cacheRecord.getAccessToken());
    assertNull(cacheRecord.getRefreshToken());
    assertEquals(testBundle.mGeneratedAccount, cacheRecord.getAccount());
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord)

Example 23 with ICacheRecord

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

the class MsalOAuth2TokenCacheTest method getAccountsWithAggregatedAccountData.

@Test
public void getAccountsWithAggregatedAccountData() throws ClientException {
    loadTestBundleIntoCache(defaultTestBundleV2);
    final List<ICacheRecord> cacheRecords = mOauth2TokenCache.getAccountsWithAggregatedAccountData(ENVIRONMENT, CLIENT_ID, HOME_ACCOUNT_ID);
    assertEquals(1, cacheRecords.size());
    final ICacheRecord cacheRecord = cacheRecords.get(0);
    assertNotNull(cacheRecord.getAccount());
    assertNotNull(cacheRecord.getIdToken());
    assertNull(cacheRecord.getAccessToken());
    assertNull(cacheRecord.getRefreshToken());
    assertNull(cacheRecord.getV1IdToken());
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) Test(org.junit.Test)

Example 24 with ICacheRecord

use of com.microsoft.identity.common.internal.cache.ICacheRecord 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 25 with ICacheRecord

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

the class MsalOAuth2TokenCacheTest method loadTokens.

@Test
public void loadTokens() throws ClientException {
    // Save an Account into the cache
    final ICacheRecord result = mOauth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    assertEquals(defaultTestBundleV2.mGeneratedAccount, result.getAccount());
    assertEquals(defaultTestBundleV2.mGeneratedAccessToken, result.getAccessToken());
    assertEquals(defaultTestBundleV2.mGeneratedRefreshToken, result.getRefreshToken());
    assertEquals(defaultTestBundleV2.mGeneratedIdToken, result.getIdToken());
    final ICacheRecord secondaryLoad = mOauth2TokenCache.load(CLIENT_ID, TARGET, defaultTestBundleV2.mGeneratedAccount, BEARER_AUTHENTICATION_SCHEME);
    assertEquals(result, secondaryLoad);
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) Test(org.junit.Test)

Aggregations

ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)51 Test (org.junit.Test)40 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)11 ArrayList (java.util.ArrayList)5 MsalOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)4 OAuth2TokenCache (com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)4 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)4 LocalAuthenticationResult (com.microsoft.identity.common.internal.result.LocalAuthenticationResult)4 ApiEndEvent (com.microsoft.identity.common.internal.telemetry.events.ApiEndEvent)4 ApiStartEvent (com.microsoft.identity.common.internal.telemetry.events.ApiStartEvent)4 ClientException (com.microsoft.identity.common.exception.ClientException)3 BearerAuthenticationSchemeInternal (com.microsoft.identity.common.internal.authscheme.BearerAuthenticationSchemeInternal)3 BrokerApplicationMetadata (com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata)3 BrokerOAuth2TokenCache (com.microsoft.identity.common.internal.cache.BrokerOAuth2TokenCache)3 MicrosoftFamilyOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MicrosoftFamilyOAuth2TokenCache)3 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)3 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)3 OAuth2Strategy (com.microsoft.identity.common.internal.providers.oauth2.OAuth2Strategy)3 OAuth2StrategyParameters (com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters)3 TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)3