Search in sources :

Example 56 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 removeNonexistentAccountTest.

@Test
public void removeNonexistentAccountTest() throws ClientException {
    final AccountRecord generatedAccount = mTestBundle.mGeneratedAccount;
    final AccountDeletionRecord deletionRecord = mCppCache.removeAccount(generatedAccount.getHomeAccountId(), generatedAccount.getEnvironment(), generatedAccount.getRealm());
    Assert.assertTrue(deletionRecord.isEmpty());
}
Also used : AccountDeletionRecord(com.microsoft.identity.common.internal.cache.AccountDeletionRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 57 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 removeNoMatchingAccount.

@Test
public void removeNoMatchingAccount() 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 with a different realm
    final AccountDeletionRecord deletionRecord = mCppCache.removeAccount(generatedAccount.getHomeAccountId(), generatedAccount.getEnvironment(), REALM2);
    // Check the receipt, should remove nothing
    Assert.assertEquals(0, deletionRecord.size());
}
Also used : AccountDeletionRecord(com.microsoft.identity.common.internal.cache.AccountDeletionRecord) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 58 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 getAllAccountsTest.

@Test
public void getAllAccountsTest() {
    final AccountRecord generatedAccount = mTestBundle.mGeneratedAccount;
    mCppCache.saveAccountRecord(generatedAccount);
    final List<AccountRecord> accounts = mCppCache.getAllAccounts();
    final AccountRecord restoredAccount = accounts.get(0);
    Assert.assertEquals(generatedAccount, restoredAccount);
    Assert.assertEquals(1, accounts.size());
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Test(org.junit.Test)

Example 59 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 removeAccountTest.

@Test
public void removeAccountTest() 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(), generatedAccount.getEnvironment(), generatedAccount.getRealm());
    // 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)

Example 60 with AccountRecord

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

the class BaseController method getCachedAccountRecord.

/**
 * Helper method to get a cached account
 *
 * @param parameters
 * @return
 */
protected AccountRecord getCachedAccountRecord(@NonNull final SilentTokenCommandParameters parameters) throws ClientException {
    if (parameters.getAccount() == null) {
        throw new ClientException(ErrorStrings.NO_ACCOUNT_FOUND, "No cached accounts found for the supplied homeAccountId and clientId");
    }
    final boolean isB2CAuthority = B2C.equalsIgnoreCase(parameters.getAuthority().getAuthorityTypeString());
    final String clientId = parameters.getClientId();
    final String homeAccountId = parameters.getAccount().getHomeAccountId();
    final String localAccountId = parameters.getAccount().getLocalAccountId();
    AccountRecord targetAccount;
    if (isB2CAuthority) {
        // Due to differences in the B2C service API relative to AAD, all IAccounts returned by
        // the B2C-STS have the same local_account_id irrespective of the policy used to load it.
        // 
        // Because the home_account_id is unique to policy and there is no concept of
        // multi-realm accounts relative to B2C, we'll conditionally use the home_account_id
        // in these cases
        targetAccount = parameters.getOAuth2TokenCache().getAccountByHomeAccountId(null, clientId, homeAccountId);
    } else {
        targetAccount = parameters.getOAuth2TokenCache().getAccountByLocalAccountId(null, clientId, localAccountId);
    }
    if (null == targetAccount && parameters.getOAuth2TokenCache() instanceof MsalOAuth2TokenCache) {
        targetAccount = getAccountWithFRTIfAvailable(parameters, (MsalOAuth2TokenCache) parameters.getOAuth2TokenCache());
    }
    if (null == targetAccount) {
        Logger.info(TAG, "No accounts found for clientId [" + clientId + ", " + "]", null);
        Logger.errorPII(TAG, "No accounts found for clientId, homeAccountId: [" + clientId + ", " + homeAccountId + "]", null);
        throw new ClientException(ErrorStrings.NO_ACCOUNT_FOUND, "No cached accounts found for the supplied homeAccountId");
    }
    return targetAccount;
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) ClientException(com.microsoft.identity.common.exception.ClientException) MsalOAuth2TokenCache(com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)

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