Search in sources :

Example 21 with AccountRecord

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

the class BrokerOAuth2TokenCache method getAccounts.

/**
 * Broker-only API. Fetches AccountRecords from all provided caches - makes NO GUARANTEES
 * as to whether or not an AT/RT pair exists for these Accounts.
 *
 * @return A List of AccountRecords, may be empty but is never null.
 */
public List<AccountRecord> getAccounts() {
    final String methodName = ":getAccounts";
    final Set<AccountRecord> allAccounts = new HashSet<>();
    final List<BrokerApplicationMetadata> allMetadata = mApplicationMetadataCache.getAll();
    // TODO - Everything inside this loop can be parallelized... should it be?
    for (final BrokerApplicationMetadata metadata : allMetadata) {
        final OAuth2TokenCache candidateCache = getTokenCacheForClient(metadata);
        if (null != candidateCache) {
            allAccounts.addAll(((MsalOAuth2TokenCache) candidateCache).getAccountCredentialCache().getAccounts());
        }
    }
    // Hit the FOCI cache
    allAccounts.addAll(mFociCache.getAccountCredentialCache().getAccounts());
    final List<AccountRecord> allAccountsResult = new ArrayList<>(allAccounts);
    Logger.verbose(TAG + methodName, "Found [" + allAccountsResult.size() + "] accounts.");
    return allAccountsResult;
}
Also used : OAuth2TokenCache(com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 22 with AccountRecord

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

the class BrokerOAuth2TokenCache method getAccountByHomeAccountId.

@Override
public AccountRecord getAccountByHomeAccountId(@Nullable final String environment, @NonNull final String clientId, @NonNull final String homeAccountId) {
    final String methodName = "getAccountByHomeAccountId";
    Logger.verbose(TAG + methodName, "Loading account by home account id.");
    if (null != environment) {
        OAuth2TokenCache targetCache = getTokenCacheForClient(clientId, environment, mCallingProcessUid);
        Logger.info(TAG + methodName, "Loading from FOCI cache? [" + (targetCache == null) + "]");
        if (null != targetCache) {
            return targetCache.getAccountByHomeAccountId(environment, clientId, homeAccountId);
        } else {
            return mFociCache.getAccountByHomeAccountId(environment, clientId, homeAccountId);
        }
    } else {
        AccountRecord result = null;
        final List<OAuth2TokenCache> cachesToInspect = getTokenCachesForClientId(clientId);
        final Iterator<OAuth2TokenCache> cacheIterator = cachesToInspect.iterator();
        while (null == result && cacheIterator.hasNext()) {
            result = cacheIterator.next().getAccountByHomeAccountId(environment, clientId, homeAccountId);
        }
        return result;
    }
}
Also used : OAuth2TokenCache(com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord)

Example 23 with AccountRecord

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

the class BrokerOAuth2TokenCache method getAccountByLocalAccountId.

@Override
@Nullable
public AccountRecord getAccountByLocalAccountId(@Nullable final String environment, @NonNull final String clientId, @NonNull final String localAccountId) {
    final String methodName = ":getAccountByLocalAccountId";
    Logger.verbose(TAG + methodName, "Loading account by local account id.");
    if (null != environment) {
        OAuth2TokenCache targetCache = getTokenCacheForClient(clientId, environment, mCallingProcessUid);
        Logger.info(TAG + methodName, "Loading from FOCI cache? [" + (targetCache == null) + "]");
        if (null != targetCache) {
            return targetCache.getAccountByLocalAccountId(environment, clientId, localAccountId);
        } else {
            return mFociCache.getAccountByLocalAccountId(environment, clientId, localAccountId);
        }
    } else {
        AccountRecord result = null;
        final List<OAuth2TokenCache> cachesToInspect = getTokenCachesForClientId(clientId);
        final Iterator<OAuth2TokenCache> cacheIterator = cachesToInspect.iterator();
        while (null == result && cacheIterator.hasNext()) {
            result = cacheIterator.next().getAccountByLocalAccountId(environment, clientId, localAccountId);
        }
        return result;
    }
}
Also used : OAuth2TokenCache(com.microsoft.identity.common.internal.providers.oauth2.OAuth2TokenCache) AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) Nullable(androidx.annotation.Nullable)

Example 24 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 getAccountByHomeAccountId.

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

Example 25 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 removeAccount.

/**
 * Removes the specified Account or Accounts from the cache.
 * <p>
 * Note: if realm is passed as null, all tokens and AccountRecords associated to the
 * provided homeAccountId will be deleted. If a realm is provided, then the deletion is
 * restricted to only those AccountRecords and Credentials in that realm (tenant).
 * <p>
 * clientId, and home_account_id are nullable parameters. However, it should be
 * noted that if these params are null, this method will have no effect.
 *
 * @param environment   The environment to which the targeted Account is associated.
 * @param clientId      The clientId of this current app.
 * @param homeAccountId The homeAccountId of the Account targeted for deletion.
 * @param realm         The tenant id of the targeted Account (if applicable).
 * @param typesToRemove The CredentialTypes to delete for the targeted Account.
 * @return An {@link AccountDeletionRecord}, containing the deleted {@link AccountDeletionRecord}s.
 */
@Override
public AccountDeletionRecord removeAccount(@Nullable final String environment, @Nullable final String clientId, @Nullable final String homeAccountId, @Nullable final String realm, @Nullable final CredentialType... typesToRemove) {
    final String methodName = ":removeAccount";
    Logger.verbosePII(TAG + methodName, "Environment: [" + environment + "]" + "\n" + "ClientId: [" + clientId + "]" + "\n" + "HomeAccountId: [" + homeAccountId + "]" + "\n" + "Realm: [" + realm + "]" + "\n" + "CredentialTypes to delete: [" + Arrays.toString(typesToRemove) + "]");
    final AccountRecord targetAccount;
    if (null == clientId || null == homeAccountId || null == (targetAccount = getAccount(environment, clientId, homeAccountId, realm))) {
        Logger.warn(TAG + methodName, "Insufficient filtering provided for account removal - preserving Account.");
        return new AccountDeletionRecord(null);
    }
    // If no realm is provided, remove the Account/Credentials from all realms.
    final boolean isRealmAgnostic = (null == realm);
    Logger.verbose(TAG + methodName, "IsRealmAgnostic? " + isRealmAgnostic);
    if (null != typesToRemove && typesToRemove.length > 0) {
        for (final CredentialType type : typesToRemove) {
            // A count of the deleted creds...
            int deletedCredentialsOfTypeCount = removeCredentialsOfTypeForAccount(environment, clientId, type, targetAccount, isRealmAgnostic);
            com.microsoft.identity.common.internal.logging.Logger.info(TAG + methodName, "Removed " + deletedCredentialsOfTypeCount + " credentials of type: " + type);
        }
    } else {
        com.microsoft.identity.common.internal.logging.Logger.warn(TAG + methodName, "removeAccount called, but no CredentialTypes to remove specified");
    }
    final List<AccountRecord> deletedAccounts = new ArrayList<>();
    if (isRealmAgnostic) {
        // Remove all Accounts associated with this home_account_id...
        final List<AccountRecord> accountsToRemove = mAccountCredentialCache.getAccountsFilteredBy(homeAccountId, environment, // wildcard (*) realm
        null);
        for (final AccountRecord accountToRemove : accountsToRemove) {
            if (mAccountCredentialCache.removeAccount(accountToRemove)) {
                deletedAccounts.add(accountToRemove);
            }
        }
    } else {
        // Remove only the target Account
        if (mAccountCredentialCache.removeAccount(targetAccount)) {
            deletedAccounts.add(targetAccount);
        }
    }
    return new AccountDeletionRecord(deletedAccounts);
}
Also used : AccountRecord(com.microsoft.identity.common.internal.dto.AccountRecord) ArrayList(java.util.ArrayList) CredentialType(com.microsoft.identity.common.internal.dto.CredentialType)

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