Search in sources :

Example 41 with AccountRecord

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));
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 42 with AccountRecord

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);
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 43 with AccountRecord

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());
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Test(org.junit.Test)

Example 44 with AccountRecord

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());
}
Also used : ICacheRecord(com.microsoft.identity.common.internal.cache.ICacheRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord)

Example 45 with AccountRecord

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

Aggregations

AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)92 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)20 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)11 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)11 OAuth2TokenCache (com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache)11 Credential (com.microsoft.identity.common.internal.dto.Credential)10 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)10 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)10 AccountDeletionRecord (com.microsoft.identity.common.internal.cache.AccountDeletionRecord)9 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)7 Nullable (androidx.annotation.Nullable)6 MsalOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)5 HashMap (java.util.HashMap)5 IAccountRecord (com.microsoft.identity.common.internal.dto.IAccountRecord)4 Context (android.content.Context)3 NonNull (androidx.annotation.NonNull)3 JsonElement (com.google.gson.JsonElement)3 JsonPrimitive (com.google.gson.JsonPrimitive)3 BearerAuthenticationSchemeInternal (com.microsoft.identity.common.internal.authscheme.BearerAuthenticationSchemeInternal)3