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());
}
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());
}
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;
}
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;
}
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;
}
Aggregations