Search in sources :

Example 1 with MsalOAuth2TokenCache

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

the class BrokerOAuth2TokenCacheTest method testGetAccountsAdal.

@Test
public void testGetAccountsAdal() 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());
}
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) Test(org.junit.Test)

Example 2 with MsalOAuth2TokenCache

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

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

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

the class BaseController method getCachedAccountRecord.

/**
 * Helper method to get a cached account
 *
 * @param parameters
 * @return
 */
protected AccountRecord getCachedAccountRecord(@NonNull final SilentTokenCommandParameters parameters) throws ClientException {
    if (parameters.getAccount() == null) {
        throw new ClientException(ErrorStrings.NO_ACCOUNT_FOUND, "No cached accounts found for the supplied homeAccountId and clientId");
    }
    final boolean isB2CAuthority = B2C.equalsIgnoreCase(parameters.getAuthority().getAuthorityTypeString());
    final String clientId = parameters.getClientId();
    final String homeAccountId = parameters.getAccount().getHomeAccountId();
    final String localAccountId = parameters.getAccount().getLocalAccountId();
    AccountRecord targetAccount;
    if (isB2CAuthority) {
        // Due to differences in the B2C service API relative to AAD, all IAccounts returned by
        // the B2C-STS have the same local_account_id irrespective of the policy used to load it.
        // 
        // Because the home_account_id is unique to policy and there is no concept of
        // multi-realm accounts relative to B2C, we'll conditionally use the home_account_id
        // in these cases
        targetAccount = parameters.getOAuth2TokenCache().getAccountByHomeAccountId(null, clientId, homeAccountId);
    } else {
        targetAccount = parameters.getOAuth2TokenCache().getAccountByLocalAccountId(null, clientId, localAccountId);
    }
    if (null == targetAccount && parameters.getOAuth2TokenCache() instanceof MsalOAuth2TokenCache) {
        targetAccount = getAccountWithFRTIfAvailable(parameters, (MsalOAuth2TokenCache) parameters.getOAuth2TokenCache());
    }
    if (null == targetAccount) {
        Logger.info(TAG, "No accounts found for clientId [" + clientId + ", " + "]", null);
        Logger.errorPII(TAG, "No accounts found for clientId, homeAccountId: [" + clientId + ", " + homeAccountId + "]", null);
        throw new ClientException(ErrorStrings.NO_ACCOUNT_FOUND, "No cached accounts found for the supplied homeAccountId");
    }
    return targetAccount;
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) ClientException(com.microsoft.identity.common.exception.ClientException) MsalOAuth2TokenCache(com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)

Aggregations

MsalOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)4 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)4 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)3 Test (org.junit.Test)3 Context (android.content.Context)2 BrokerApplicationMetadata (com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata)2 BrokerOAuth2TokenCache (com.microsoft.identity.common.internal.cache.BrokerOAuth2TokenCache)2 MicrosoftFamilyOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MicrosoftFamilyOAuth2TokenCache)2 OAuth2TokenCache (com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)2 ArrayList (java.util.ArrayList)2 StorageHelper (com.microsoft.identity.common.adal.internal.cache.StorageHelper)1 ClientException (com.microsoft.identity.common.exception.ClientException)1 BearerAuthenticationSchemeInternal (com.microsoft.identity.common.internal.authscheme.BearerAuthenticationSchemeInternal)1 CacheKeyValueDelegate (com.microsoft.identity.common.internal.cache.CacheKeyValueDelegate)1 IAccountCredentialCache (com.microsoft.identity.common.internal.cache.IAccountCredentialCache)1 MicrosoftStsAccountCredentialAdapter (com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter)1 SharedPreferencesAccountCredentialCache (com.microsoft.identity.common.internal.cache.SharedPreferencesAccountCredentialCache)1 SharedPreferencesBrokerApplicationMetadataCache (com.microsoft.identity.common.internal.cache.SharedPreferencesBrokerApplicationMetadataCache)1 SharedPreferencesFileManager (com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager)1 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)1