use of com.microsoft.identity.common.internal.dto.AccountRecord in project microsoft-authentication-library-common-for-android by AzureAD.
the class MsalOAuth2TokenCacheTest method saveTokensV1Compat.
@Test
public void saveTokensV1Compat() throws ClientException {
// This test asserts that if an IdToken is returned in the v1 format (broker cases),
// it is saved property.
loadTestBundleIntoCache(defaultTestBundleV1);
final List<AccountRecord> accounts = accountCredentialCache.getAccounts();
assertEquals(1, accounts.size());
assertEquals(defaultTestBundleV1.mGeneratedAccount, accounts.get(0));
final List<Credential> credentials = accountCredentialCache.getCredentials();
assertEquals(3, credentials.size());
final List<Credential> rts = new ArrayList<>();
final List<Credential> ats = new ArrayList<>();
final List<Credential> ids = new ArrayList<>();
for (final Credential credential : credentials) {
switch(CredentialType.fromString(credential.getCredentialType())) {
case AccessToken:
ats.add(credential);
break;
case RefreshToken:
rts.add(credential);
break;
case V1IdToken:
ids.add(credential);
break;
default:
fail("Unexpected value: " + credential.getCredentialType());
}
}
assertEquals(defaultTestBundleV1.mGeneratedAccessToken, ats.get(0));
assertEquals(defaultTestBundleV1.mGeneratedRefreshToken, rts.get(0));
assertEquals(defaultTestBundleV1.mGeneratedIdToken, ids.get(0));
}
use of com.microsoft.identity.common.internal.dto.AccountRecord in project microsoft-authentication-library-common-for-android by AzureAD.
the class MsalOAuth2TokenCacheTest method getAccountCacheEmpty.
@Test
public void getAccountCacheEmpty() {
final AccountRecord account = mOauth2TokenCache.getAccount(ENVIRONMENT, CLIENT_ID, HOME_ACCOUNT_ID, REALM);
assertNull(account);
}
use of com.microsoft.identity.common.internal.dto.AccountRecord in project microsoft-authentication-library-common-for-android by AzureAD.
the class MsalOAuth2TokenCacheTest method saveTokensWithAggregationV1MultiEntry.
@Test
public void saveTokensWithAggregationV1MultiEntry() throws ClientException {
// Load additional creds into the cache to simulate a guest account...
// at, id, account
final AccessTokenRecord at = new AccessTokenRecord();
at.setRealm(REALM2);
at.setCachedAt(CACHED_AT);
at.setExpiresOn(EXPIRES_ON);
at.setSecret(SECRET);
at.setHomeAccountId(HOME_ACCOUNT_ID);
at.setEnvironment(ENVIRONMENT);
at.setCredentialType(AccessToken.name());
at.setClientId(CLIENT_ID);
at.setTarget(TARGET);
final IdTokenRecord id = new IdTokenRecord();
id.setHomeAccountId(HOME_ACCOUNT_ID);
id.setEnvironment(ENVIRONMENT);
id.setRealm(REALM2);
id.setCredentialType(IdToken.name());
id.setClientId(CLIENT_ID);
id.setSecret(MOCK_ID_TOKEN_WITH_CLAIMS);
id.setAuthority("https://sts.windows.net/0287f963-2d72-4363-9e3a-5705c5b0f031/");
final AccountRecord acct = new AccountRecord();
acct.setAuthorityType(AUTHORITY_TYPE);
acct.setLocalAccountId(UUID.randomUUID().toString());
acct.setUsername(USERNAME);
acct.setHomeAccountId(HOME_ACCOUNT_ID);
acct.setEnvironment(ENVIRONMENT);
acct.setRealm(REALM2);
accountCredentialCache.saveAccount(acct);
accountCredentialCache.saveCredential(at);
accountCredentialCache.saveCredential(id);
final List<ICacheRecord> result = loadTestBundleIntoCacheWithAggregation(defaultTestBundleV1);
assertEquals(2, result.size());
final ICacheRecord entry1 = result.get(0);
assertNotNull(entry1.getAccount());
assertNotNull(entry1.getV1IdToken());
assertNotNull(entry1.getAccessToken());
assertNotNull(entry1.getRefreshToken());
final ICacheRecord entry2 = result.get(1);
assertNotNull(entry2.getAccount());
assertNotNull(entry2.getIdToken());
assertNull(entry2.getAccessToken());
assertNull(entry2.getRefreshToken());
}
use of com.microsoft.identity.common.internal.dto.AccountRecord 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());
}
use of com.microsoft.identity.common.internal.dto.AccountRecord 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());
}
Aggregations