Search in sources :

Example 51 with AccountRecord

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

the class BrokerOAuth2TokenCacheTest method testCanSaveIntoFociCache.

@Test
public void testCanSaveIntoFociCache() throws ClientException {
    configureMocksForFoci();
    mBrokerOAuth2TokenCache.save(mockStrategy, mockRequest, mockResponse);
    final List<AccountRecord> accounts = mFociCredentialCache.getAccounts();
    assertEquals(1, accounts.size());
    assertEquals(mDefaultFociTestBundle.mGeneratedAccount, accounts.get(0));
    final List<Credential> credentials = mFociCredentialCache.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) {
        if (credential.getCredentialType().equalsIgnoreCase(CredentialType.AccessToken.name())) {
            ats.add(credential);
        } else if (credential.getCredentialType().equalsIgnoreCase(CredentialType.RefreshToken.name())) {
            rts.add(credential);
        } else if (credential.getCredentialType().equalsIgnoreCase(CredentialType.IdToken.name())) {
            ids.add(credential);
        } else {
            fail();
        }
    }
    assertEquals(mDefaultFociTestBundle.mGeneratedAccessToken, ats.get(0));
    assertEquals(mDefaultFociTestBundle.mGeneratedRefreshToken, rts.get(0));
    assertEquals(mDefaultFociTestBundle.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 52 with AccountRecord

use of com.microsoft.identity.common.internal.dto.AccountRecord 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 53 with AccountRecord

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

the class MicrosoftStsAccountCredentialAdapterTest method createAccount.

@Test
public void createAccount() {
    // This test is now basically a copy-constructor test
    final AccountRecord account = mAccountCredentialAdapter.createAccount(mockStrategy, mockRequest, mockResponse);
    assertNotNull(account);
    assertEquals(MOCK_UID + "." + MOCK_UTID, account.getHomeAccountId());
    assertEquals(MOCK_ENVIRONMENT, account.getEnvironment());
    assertEquals(MOCK_CLIENT_INFO, account.getClientInfo());
    assertEquals(MOCK_TID, account.getRealm());
    assertEquals(MOCK_OID, account.getLocalAccountId());
    assertEquals(MOCK_PREFERRED_USERNAME, account.getUsername());
    assertEquals("MSSTS", account.getAuthorityType());
    assertEquals(MOCK_GIVEN_NAME, account.getFirstName());
    assertEquals(MOCK_FAMILY_NAME, account.getFamilyName());
    assertEquals(MOCK_MIDDLE_NAME, account.getMiddleName());
    assertEquals(MOCK_NAME, account.getName());
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 54 with AccountRecord

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

the class MsalCppOAuth2TokenCacheTest method forceRemoveAccountTest.

@Test
public void forceRemoveAccountTest() throws ClientException {
    // Get the generated account
    final AccountRecord generatedAccount = mTestBundle.mGeneratedAccount;
    // Save it to the cache
    mCppCache.saveAccountRecord(generatedAccount);
    // Do not save any credentials for this account...
    final AccountDeletionRecord deletionRecord = mCppCache.forceRemoveAccount(generatedAccount.getHomeAccountId(), generatedAccount.getEnvironment(), generatedAccount.getRealm());
    Assert.assertEquals(1, deletionRecord.size());
    // Try to restore it
    final AccountRecord restoredAccount = mCppCache.getAccount(generatedAccount.getHomeAccountId(), generatedAccount.getEnvironment(), generatedAccount.getRealm());
    // Make sure it doesn't exist....
    Assert.assertNull(restoredAccount);
}
Also used : AccountDeletionRecord(com.microsoft.identity.common.internal.cache.AccountDeletionRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 55 with AccountRecord

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

the class MsalCppOAuth2TokenCacheTest method removeAccountWithHomeAccountIdTest.

@Test
public void removeAccountWithHomeAccountIdTest() throws ClientException {
    // Get the generated account
    final AccountRecord generatedAccount = mTestBundle.mGeneratedAccount;
    // Save it to the cache
    mCppCache.saveAccountRecord(generatedAccount);
    mCppCache.saveCredentials(null, mTestBundle.mGeneratedRefreshToken);
    // Call remove
    final AccountDeletionRecord deletionRecord = mCppCache.removeAccount(generatedAccount.getHomeAccountId(), "", "");
    // Check the receipt
    Assert.assertEquals(generatedAccount, deletionRecord.get(0));
    // Try to restore it
    final AccountRecord restoredAccount = mCppCache.getAccount(generatedAccount.getHomeAccountId(), generatedAccount.getEnvironment(), generatedAccount.getRealm());
    // Make sure it doesn't exist....
    Assert.assertNull(restoredAccount);
}
Also used : AccountDeletionRecord(com.microsoft.identity.common.internal.cache.AccountDeletionRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) 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