Search in sources :

Example 1 with OAuth2TokenCache

use of com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache 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 OAuth2TokenCache

use of com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache in project microsoft-authentication-library-common-for-android by AzureAD.

the class BrokerOAuth2TokenCacheTest method testRemoveAccountFromDevice.

@Test
public void testRemoveAccountFromDevice() 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 = mBrokerOAuth2TokenCache.getAccounts();
    // Deleting one of these AccountRecords should remove all of them...
    final AccountDeletionRecord deletionRecord = mBrokerOAuth2TokenCache.removeAccountFromDevice(xAppAccounts.get(0));
    assertEquals(xAppAccounts.size(), deletionRecord.size());
    assertEquals(0, mBrokerOAuth2TokenCache.getAccounts().size());
}
Also used : 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) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) AccountDeletionRecord(com.microsoft.identity.common.internal.cache.AccountDeletionRecord) BrokerApplicationMetadata(com.microsoft.identity.common.internal.cache.BrokerApplicationMetadata) ArrayList(java.util.ArrayList) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 3 with OAuth2TokenCache

use of com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache in project microsoft-authentication-library-common-for-android by AzureAD.

the class BrokerOAuth2TokenCache method load.

/**
 * {@inheritDoc}
 * <p>
 * The caller of this function should inspect the result carefully.
 * <p>
 * If the result contains an AccountRecord, IdTokenRecord, AccessTokenRecord, and
 * RefreshTokenRecord then the result is OK to use. The caller should still check the expiry of
 * the AccessTokenRecord before returning the result to the caller, refreshing as necessary...
 * <p>
 * If the result contains only an AccountRecord then we had no tokens in the cache and the
 * library should do some equivalent of AUTH_REFRESH_FAILED_PROMPT_NOT_ALLOWED
 * <p>
 * If the result contains only an AccountRecord and RefreshTokenRecord then the caller should attempt to refresh
 * the access token. If it works, call BrokerOAuth2TokenCache#save() with the result. If it
 * fails, throw some equivalent of AUTH_REFRESH_FAILED_PROMPT_NOT_ALLOWED
 *
 * @param clientId The ClientId of the current app.
 * @param target   The 'target' (scopes) the requested token should contain.
 * @param account  The Account whose Credentials should be loaded.
 * @return
 */
@Override
public ICacheRecord load(@NonNull final String clientId, @Nullable final String target, @NonNull final AccountRecord account, @NonNull final AbstractAuthenticationScheme authScheme) {
    final String methodName = ":load";
    Logger.verbose(TAG + methodName, "Performing lookup in app-specific cache.");
    final BrokerApplicationMetadata appMetadata = mApplicationMetadataCache.getMetadata(clientId, account.getEnvironment(), mCallingProcessUid);
    boolean isKnownFoci = false;
    if (null != appMetadata) {
        isKnownFoci = null != appMetadata.getFoci();
        Logger.info(TAG + methodName, "App is known foci? " + isKnownFoci);
    }
    final OAuth2TokenCache targetCache = getTokenCacheForClient(clientId, account.getEnvironment(), mCallingProcessUid);
    final boolean shouldUseFociCache = null == targetCache || isKnownFoci;
    Logger.info(TAG + methodName, "Loading from FOCI cache? [" + shouldUseFociCache + "]");
    ICacheRecord resultRecord;
    if (shouldUseFociCache) {
        resultRecord = mFociCache.loadByFamilyId(clientId, target, account, authScheme);
    } else {
        resultRecord = targetCache.load(clientId, target, account, authScheme);
    }
    final boolean resultFound = null != resultRecord.getRefreshToken();
    Logger.verbose(TAG + methodName, "Result found? [" + resultFound + "]");
    return resultRecord;
}
Also used : OAuth2TokenCache(com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)

Example 4 with OAuth2TokenCache

use of com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache in project microsoft-authentication-library-common-for-android by AzureAD.

the class BrokerOAuth2TokenCache method getAllTenantAccountsForAccountByClientId.

@Override
public List<AccountRecord> getAllTenantAccountsForAccountByClientId(@NonNull final String clientId, @NonNull final AccountRecord accountRecord) {
    final OAuth2TokenCache cache = getTokenCacheForClient(clientId, accountRecord.getEnvironment(), mCallingProcessUid);
    // Suppressing unchecked warnings due to casting List to List<AccountRecord> as the generic type for cache was not provided
    @SuppressWarnings(WarningType.unchecked_warning) List<AccountRecord> tenantAccountsForAccountByClientId = cache.getAllTenantAccountsForAccountByClientId(clientId, accountRecord);
    return tenantAccountsForAccountByClientId;
}
Also used : OAuth2TokenCache(com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord)

Example 5 with OAuth2TokenCache

use of com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache in project microsoft-authentication-library-common-for-android by AzureAD.

the class BrokerOAuth2TokenCache method getIdTokensForAccountRecord.

@Override
public List<IdTokenRecord> getIdTokensForAccountRecord(@NonNull final String clientId, @NonNull final AccountRecord accountRecord) {
    final List<IdTokenRecord> result;
    final String accountEnv = accountRecord.getEnvironment();
    if (null == clientId) {
        // this feature...
        throw new UnsupportedOperationException("Aggregating IdTokens across ClientIds is not supported - do you have a feature request?");
    } else {
        final OAuth2TokenCache cache = getTokenCacheForClient(clientId, accountEnv, mCallingProcessUid);
        // Suppressing unchecked warning as the generic type was not provided for cache
        @SuppressWarnings(WarningType.unchecked_warning) List<IdTokenRecord> cacheIdTokensForAccountRecord = cache.getIdTokensForAccountRecord(clientId, accountRecord);
        result = cacheIdTokensForAccountRecord;
    }
    return result;
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) OAuth2TokenCache(com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)

Aggregations

OAuth2TokenCache (com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)20 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)11 ArrayList (java.util.ArrayList)6 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)5 Context (android.content.Context)3 Nullable (androidx.annotation.Nullable)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 MsalOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)3 Test (org.junit.Test)3 OAuth2StrategyParameters (com.microsoft.identity.common.internal.providers.oauth2.OAuth2StrategyParameters)2 TokenResult (com.microsoft.identity.common.internal.providers.oauth2.TokenResult)2 AcquireTokenResult (com.microsoft.identity.common.internal.result.AcquireTokenResult)2 LocalAuthenticationResult (com.microsoft.identity.common.internal.result.LocalAuthenticationResult)2 Uri (android.net.Uri)1 ClientException (com.microsoft.identity.common.exception.ClientException)1 AbstractAuthenticationScheme (com.microsoft.identity.common.internal.authscheme.AbstractAuthenticationScheme)1 IPoPAuthenticationSchemeParams (com.microsoft.identity.common.internal.authscheme.IPoPAuthenticationSchemeParams)1 AccountDeletionRecord (com.microsoft.identity.common.internal.cache.AccountDeletionRecord)1