Search in sources :

Example 36 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 removeV1IdToken.

@Test
public void removeV1IdToken() throws ClientException {
    final ICacheRecord result = loadTestBundleIntoCache(defaultTestBundleV1);
    mOauth2TokenCache.removeCredential(result.getV1IdToken());
    final ICacheRecord secondaryLoad = mOauth2TokenCache.load(CLIENT_ID, TARGET, defaultTestBundleV2.mGeneratedAccount, BEARER_AUTHENTICATION_SCHEME);
    assertEquals(defaultTestBundleV1.mGeneratedAccount, secondaryLoad.getAccount());
    assertEquals(defaultTestBundleV1.mGeneratedAccessToken, secondaryLoad.getAccessToken());
    assertEquals(defaultTestBundleV1.mGeneratedRefreshToken, secondaryLoad.getRefreshToken());
    assertNull(secondaryLoad.getIdToken());
    assertNull(secondaryLoad.getV1IdToken());
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) Test(org.junit.Test)

Example 37 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 saveTokensWithAggregationV1SingleEntry.

@Test
public void saveTokensWithAggregationV1SingleEntry() throws ClientException {
    final List<ICacheRecord> result = loadTestBundleIntoCacheWithAggregation(defaultTestBundleV1);
    assertEquals(1, result.size());
    final ICacheRecord entry = result.get(0);
    assertNotNull(entry.getAccount());
    assertNotNull(entry.getV1IdToken());
    assertNotNull(entry.getAccessToken());
    assertNotNull(entry.getRefreshToken());
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) Test(org.junit.Test)

Example 38 with ICacheRecord

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

the class BrokerOAuth2TokenCacheTest method testGetAccountsMsal.

@Test
public void testGetAccountsMsal() throws ClientException {
    // Load up the 'other caches' which a bunch of test credentials, see if we can get them out...
    int ii = 0;
    for (final OAuth2TokenCache cache : mOtherAppTokenCaches) {
        configureMocks(mOtherCacheTestBundles.get(ii));
        final ICacheRecord cacheRecord = cache.save(mockStrategy, mockRequest, mockResponse);
        final BrokerApplicationMetadata applicationMetadata = new BrokerApplicationMetadata();
        applicationMetadata.setClientId(cacheRecord.getIdToken().getClientId());
        applicationMetadata.setEnvironment(cacheRecord.getIdToken().getEnvironment());
        applicationMetadata.setFoci(cacheRecord.getRefreshToken().getFamilyId());
        applicationMetadata.setUid(testAppUids[ii++]);
        mApplicationMetadataCache.insert(applicationMetadata);
    }
    final List<String> clientIds = new ArrayList<>();
    for (final MsalOAuth2TokenCacheTest.AccountCredentialTestBundle testBundle : mOtherCacheTestBundles) {
        clientIds.add(testBundle.mGeneratedRefreshToken.getClientId());
    }
    final List<AccountRecord> xAppAccounts = new ArrayList<>();
    for (final int testUid : testAppUids) {
        // Create the cache to query...
        mBrokerOAuth2TokenCache = new BrokerOAuth2TokenCache(InstrumentationRegistry.getContext(), testUid, mApplicationMetadataCache, new BrokerOAuth2TokenCache.ProcessUidCacheFactory() {

            @Override
            public MsalOAuth2TokenCache getTokenCache(Context context, int bindingProcessUid) {
                return initAppUidCache(context, bindingProcessUid);
            }
        }, mFociCache);
        for (final String clientId : clientIds) {
            final List<AccountRecord> accountsInCache = mBrokerOAuth2TokenCache.getAccounts(ENVIRONMENT, clientId);
            xAppAccounts.addAll(accountsInCache);
        }
    }
    assertEquals(clientIds.size(), xAppAccounts.size());
    final List<AccountRecord> xAppAccountsNoParam = new ArrayList<>(mBrokerOAuth2TokenCache.getAccounts());
    assertEquals(xAppAccounts.size(), xAppAccountsNoParam.size());
    final Context context = InstrumentationRegistry.getContext();
    final BrokerOAuth2TokenCache brokerOAuth2TokenCache = new BrokerOAuth2TokenCache(context, TEST_APP_UID, new SharedPreferencesBrokerApplicationMetadataCache(context));
    assertEquals(0, brokerOAuth2TokenCache.getAccounts(ENVIRONMENT, CLIENT_ID).size());
    final BrokerOAuth2TokenCache brokerOAuth2TokenCache2 = new BrokerOAuth2TokenCache(context, TEST_APP_UID, new SharedPreferencesBrokerApplicationMetadataCache(context));
    assertEquals(xAppAccounts.size(), brokerOAuth2TokenCache2.getAccounts().size());
}
Also used : Context(android.content.Context) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) BrokerApplicationMetadata(com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata) ArrayList(java.util.ArrayList) OAuth2TokenCache(com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache) MicrosoftFamilyOAuth2TokenCache(com.microsoft.identity.common.internal.cache.MicrosoftFamilyOAuth2TokenCache) MsalOAuth2TokenCache(com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache) BrokerOAuth2TokenCache(com.microsoft.identity.common.internal.cache.BrokerOAuth2TokenCache) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) BrokerOAuth2TokenCache(com.microsoft.identity.common.internal.cache.BrokerOAuth2TokenCache) SharedPreferencesBrokerApplicationMetadataCache(com.microsoft.identity.common.internal.cache.SharedPreferencesBrokerApplicationMetadataCache) Test(org.junit.Test)

Example 39 with ICacheRecord

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

the class MicrosoftFamilyOAuth2TokenCacheTest method testDualStackIdTokenRetrieval.

@Test
public void testDualStackIdTokenRetrieval() throws ClientException {
    final String randomHomeAccountId = UUID.randomUUID().toString();
    final String randomRealm = UUID.randomUUID().toString();
    final AccountCredentialTestBundle frtTestBundleV2 = new AccountCredentialTestBundle(MicrosoftAccount.AUTHORITY_TYPE_V1_V2, UUID.randomUUID().toString(), "test.user@tenant.onmicrosoft.com", randomHomeAccountId, ENVIRONMENT, randomRealm, TARGET, CACHED_AT, EXPIRES_ON, SECRET, CLIENT_ID, SECRET, MicrosoftStsAccountCredentialAdapterTest.MOCK_ID_TOKEN_WITH_CLAIMS, "1", SESSION_KEY, CredentialType.IdToken);
    when(mockCredentialAdapter.createAccount(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundleV2.mGeneratedAccount);
    when(mockCredentialAdapter.createAccessToken(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundleV2.mGeneratedAccessToken);
    when(mockCredentialAdapter.createRefreshToken(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundleV2.mGeneratedRefreshToken);
    when(mockCredentialAdapter.createIdToken(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundleV2.mGeneratedIdToken);
    // Save the family token data
    mOauth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    final AccountCredentialTestBundle frtTestBundleV1 = new AccountCredentialTestBundle(MicrosoftAccount.AUTHORITY_TYPE_V1_V2, UUID.randomUUID().toString(), "test.user@tenant.onmicrosoft.com", randomHomeAccountId, ENVIRONMENT, randomRealm, TARGET, CACHED_AT, EXPIRES_ON, SECRET, CLIENT_ID, SECRET, MicrosoftStsAccountCredentialAdapterTest.MOCK_ID_TOKEN_WITH_CLAIMS, "1", SESSION_KEY, CredentialType.V1IdToken);
    when(mockCredentialAdapter.createAccount(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundleV1.mGeneratedAccount);
    when(mockCredentialAdapter.createAccessToken(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundleV1.mGeneratedAccessToken);
    when(mockCredentialAdapter.createRefreshToken(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundleV1.mGeneratedRefreshToken);
    when(mockCredentialAdapter.createIdToken(mockStrategy, mockRequest, mockResponse)).thenReturn(frtTestBundleV1.mGeneratedIdToken);
    // Save the family token data
    mOauth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    final ICacheRecord familyCacheRecord = mOauth2TokenCache.loadByFamilyId(CLIENT_ID, TARGET, frtTestBundleV1.mGeneratedAccount, BEARER_SCHEME);
    assertNotNull(familyCacheRecord);
    assertNotNull(familyCacheRecord.getAccount());
    assertNotNull(familyCacheRecord.getRefreshToken());
    assertNotNull(familyCacheRecord.getIdToken());
    assertNotNull(familyCacheRecord.getV1IdToken());
    assertNotNull(familyCacheRecord.getAccessToken());
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) Test(org.junit.Test)

Example 40 with ICacheRecord

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

the class BrokerOAuth2TokenCacheTest method testCacheMiss.

@Test
public void testCacheMiss() {
    final ICacheRecord cacheRecord = mBrokerOAuth2TokenCache.load(CLIENT_ID, TARGET, mDefaultAppUidTestBundle.mGeneratedAccount, BEARER_AUTHENTICATION_SCHEME);
    assertNotNull(cacheRecord);
    assertNotNull(cacheRecord.getAccount());
    assertNull(cacheRecord.getAccessToken());
    assertNull(cacheRecord.getRefreshToken());
    assertNull(cacheRecord.getIdToken());
}
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