Search in sources :

Example 81 with AccountRecord

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

the class MsalOAuth2TokenCache method mergeCacheRecordWithOtherTenantCacheRecords.

@NonNull
private List<ICacheRecord> mergeCacheRecordWithOtherTenantCacheRecords(@NonNull final ICacheRecord savedCacheRecord) {
    final List<ICacheRecord> result = new ArrayList<>();
    // Whatever ICacheRecord you provide will _always_ be the first element in the result List.
    result.add(savedCacheRecord);
    final List<AccountRecord> accountsInOtherTenants = new ArrayList<>(getAllTenantAccountsForAccountByClientId(savedCacheRecord.getRefreshToken().getClientId(), savedCacheRecord.getAccount()));
    if (!accountsInOtherTenants.isEmpty()) {
        // Remove the first element from the List since it is already contained in the result List
        accountsInOtherTenants.remove(0);
        // Iterate over the rest of the Accounts to build up the final result
        for (final AccountRecord acct : accountsInOtherTenants) {
            result.add(getSparseCacheRecordForAccount(savedCacheRecord.getRefreshToken().getClientId(), acct));
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) NonNull(androidx.annotation.NonNull)

Example 82 with AccountRecord

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

the class MsalOAuth2TokenCache method getAccountsWithAggregatedAccountData.

@Override
public List<ICacheRecord> getAccountsWithAggregatedAccountData(@Nullable final String environment, @NonNull final String clientId) {
    final String methodName = ":getAccountsWithAggregatedAccountData";
    final List<ICacheRecord> result = new ArrayList<>();
    final List<AccountRecord> allMatchingAccounts = getAccounts(environment, clientId);
    for (final AccountRecord accountRecord : allMatchingAccounts) {
        final List<IdTokenRecord> idTokensForAccount = getIdTokensForAccountRecord(clientId, accountRecord);
        if (idTokensForAccount == null || idTokensForAccount.size() == 0) {
            // Skip returning account record if there is no corresponding idToken record in the cache for the given clientId
            continue;
        }
        // Construct the cache record....
        final CacheRecord.CacheRecordBuilder cacheRecordBuilder = CacheRecord.builder();
        cacheRecordBuilder.account(accountRecord);
        // Set the IdTokens...
        for (IdTokenRecord idTokenRecord : idTokensForAccount) {
            setToCacheRecord(cacheRecordBuilder, idTokenRecord);
        }
        result.add(cacheRecordBuilder.build());
    }
    Logger.verbose(TAG + methodName, "Found " + result.size() + " accounts with IdTokens");
    return Collections.unmodifiableList(result);
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) ArrayList(java.util.ArrayList) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord)

Example 83 with AccountRecord

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

the class MicrosoftFamilyOAuth2TokenCache method loadByFamilyIdWithAggregatedAccountData.

public List<ICacheRecord> loadByFamilyIdWithAggregatedAccountData(@NonNull final String clientId, @Nullable final String target, @NonNull final AccountRecord account, @Nullable final AbstractAuthenticationScheme authenticationScheme) {
    final String methodName = ":loadByFamilyIdWithAggregatedAccountData";
    final List<ICacheRecord> result = new ArrayList<>();
    // First, load our primary record...
    result.add(loadByFamilyId(clientId, target, account, authenticationScheme));
    // We also want to add accounts from different realms...
    final List<AccountRecord> accountsInOtherTenants = new ArrayList<>(getAllTenantAccountsForAccountByClientId(clientId, account));
    Logger.info(TAG + methodName, "Found " + (accountsInOtherTenants.size() - 1) + " profiles for this account");
    // Ignore the first element, as it will contain the same result as loadByFamilyId()....
    accountsInOtherTenants.remove(0);
    if (!accountsInOtherTenants.isEmpty()) {
        for (final AccountRecord accountRecord : accountsInOtherTenants) {
            // Declare our container
            final CacheRecord.CacheRecordBuilder cacheRecord = CacheRecord.builder();
            cacheRecord.mAccount(accountRecord);
            cacheRecord.refreshToken(result.get(0).getRefreshToken());
            // Load all of the IdTokens and set as appropriate...
            final List<IdTokenRecord> idTokensForAccount = getIdTokensForAccountRecord(clientId, accountRecord);
            for (final IdTokenRecord idTokenRecord : idTokensForAccount) {
                if (CredentialType.V1IdToken.name().equalsIgnoreCase(idTokenRecord.getCredentialType())) {
                    cacheRecord.v1IdToken(idTokenRecord);
                } else {
                    cacheRecord.idToken(idTokenRecord);
                }
            }
            // We can ignore the A/T, since this account isn't being authorized...
            result.add(cacheRecord.build());
        }
    }
    return result;
}
Also used : IdTokenRecord(com.microsoft.identity.common.internal.dto.IdTokenRecord) ArrayList(java.util.ArrayList) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord)

Example 84 with AccountRecord

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

the class MsalOAuth2TokenCache method getAccount.

@Override
@Nullable
public AccountRecord getAccount(@Nullable final String environment, @NonNull final String clientId, @NonNull final String homeAccountId, @Nullable final String realm) {
    final String methodName = ":getAccount";
    Logger.verbosePII(TAG + methodName, "Environment: [" + environment + "]" + "\n" + "ClientId: [" + clientId + "]" + "\n" + "HomeAccountId: [" + homeAccountId + "]" + "\n" + "Realm: [" + realm + "]");
    final List<AccountRecord> allAccounts = getAccounts(environment, clientId);
    Logger.info(TAG + methodName, "Found " + allAccounts.size() + " accounts");
    // Return the sought Account matching the supplied homeAccountId and realm, if applicable
    for (final AccountRecord account : allAccounts) {
        if (homeAccountId.equals(account.getHomeAccountId()) && (null == realm || realm.equals(account.getRealm()))) {
            return account;
        }
    }
    Logger.warn(TAG + methodName, "No matching account found.");
    return null;
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Nullable(androidx.annotation.Nullable)

Example 85 with AccountRecord

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

the class MsalOAuth2TokenCache method getAccountByLocalAccountId.

@Override
@Nullable
public AccountRecord getAccountByLocalAccountId(@Nullable final String environment, @NonNull final String clientId, @NonNull final String localAccountId) {
    final String methodName = ":getAccountByLocalAccountId";
    final List<AccountRecord> accounts = getAccounts(environment, clientId);
    Logger.verbosePII(TAG + methodName, "LocalAccountId: [" + localAccountId + "]");
    for (final AccountRecord account : accounts) {
        if (localAccountId.equals(account.getLocalAccountId())) {
            return account;
        }
    }
    return null;
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Nullable(androidx.annotation.Nullable)

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