Search in sources :

Example 61 with Credential

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

the class SharedPreferencesAccountCredentialCache method removeCredential.

@Override
public boolean removeCredential(@NonNull final Credential credentialToRemove) {
    Logger.info(TAG, "Removing Credential...");
    if (null == credentialToRemove) {
        throw new IllegalArgumentException("Param [credentialToRemove] cannot be null.");
    }
    final Map<String, Credential> credentials = getCredentialsWithKeys();
    boolean credentialRemoved = false;
    for (final Map.Entry<String, Credential> entry : credentials.entrySet()) {
        Logger.verbosePII(TAG, "Inspecting: [" + entry.getKey() + "]");
        final Credential currentCredential = entry.getValue();
        if (currentCredential.equals(credentialToRemove)) {
            mSharedPreferencesFileManager.remove(entry.getKey());
            credentialRemoved = true;
            break;
        }
    }
    Logger.info(TAG, "Credential was removed? [" + credentialRemoved + "]");
    return credentialRemoved;
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) HashMap(java.util.HashMap) Map(java.util.Map)

Example 62 with Credential

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

the class SharedPreferencesAccountCredentialCache method getCredential.

@Override
@Nullable
public synchronized Credential getCredential(@NonNull final String cacheKey) {
    // TODO add support for more Credential types...
    Logger.verbose(TAG, "getCredential()");
    Logger.verbosePII(TAG, "Using cache key: [" + cacheKey + "]");
    final CredentialType type = getCredentialTypeForCredentialCacheKey(cacheKey);
    Class<? extends Credential> clazz = null;
    if (null != type) {
        clazz = getTargetClassForCredentialType(cacheKey, type);
    }
    Credential credential = null;
    if (null != clazz) {
        credential = mCacheValueDelegate.fromCacheValue(mSharedPreferencesFileManager.getString(cacheKey), clazz);
    }
    if (null == credential) {
        // We could not deserialize the target Credential...
        // Maybe it was encrypted for another application?
        Logger.warn(TAG, CREDENTIAL_DESERIALIZATION_FAILED);
    } else if ((AccessTokenRecord.class == clazz && EMPTY_AT.equals(credential)) || (RefreshTokenRecord.class == clazz && EMPTY_RT.equals(credential)) || (IdTokenRecord.class == clazz) && EMPTY_ID.equals(credential)) {
        // The returned credential came back uninitialized...
        // Remove the entry and return null...
        Logger.warn(TAG, "The returned Credential was uninitialized. Removing...");
        mSharedPreferencesFileManager.remove(cacheKey);
        credential = null;
    }
    return credential;
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) CredentialType(com.microsoft.identity.common.internal.dto.CredentialType) RefreshTokenRecord(com.microsoft.identity.common.internal.dto.RefreshTokenRecord) AccessTokenRecord(com.microsoft.identity.common.internal.dto.AccessTokenRecord) Nullable(androidx.annotation.Nullable)

Example 63 with Credential

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

the class MsalOAuth2TokenCache method removeCredentialsOfTypeForAccountExcept.

/**
 * Removes Credentials of the supplied type for the supplied Account; skipping any record
 * specified as exempt.
 *
 * @param environment          Entity which issued the token represented as a host.
 * @param clientId             The clientId of the target app.
 * @param credentialType       The type of Credential to remove.
 * @param targetAccount        The target Account whose Credentials should be removed.
 * @param realmAgnostic        True if the specified action should be completed irrespective of realm.
 * @param deletionExemptRecord A record which explicitly must not be removed.
 * @return The number of Credentials removed.
 */
private int removeCredentialsOfTypeForAccountExcept(@NonNull final String environment, @Nullable final String clientId, @NonNull final CredentialType credentialType, @NonNull final AccountRecord targetAccount, final boolean realmAgnostic, @NonNull final Credential deletionExemptRecord) {
    int credentialsRemoved = 0;
    // Query it for Credentials matching the supplied targetAccount
    final List<Credential> credentialsToRemove = mAccountCredentialCache.getCredentialsFilteredBy(targetAccount.getHomeAccountId(), environment, credentialType, clientId, realmAgnostic ? // wildcard (*) realm
    null : targetAccount.getRealm(), // wildcard (*) target,
    null, null);
    for (final Credential credentialToRemove : credentialsToRemove) {
        // Do not delete the record, if it is the supplied exempted Credential.
        if (!deletionExemptRecord.equals(credentialToRemove) && mAccountCredentialCache.removeCredential(credentialToRemove)) {
            credentialsRemoved++;
        }
    }
    return credentialsRemoved;
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential)

Example 64 with Credential

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

the class CacheUtils method editAllTokenInCache.

/**
 * This method will edit all the token specified by the predicate using the  editor
 * in the shared preference.
 *
 * @param sharedPrefName Name of the shared preference where token has been stored.
 * @param predicate      Generic functional interface representing function that returns true
 *                       or false depending on token type.
 * @param editor         Functional interface to have any number of token editing method.
 */
public void editAllTokenInCache(@NonNull final String sharedPrefName, @NonNull final Predicate<String> predicate, @NonNull Function<String, Class<? extends Credential>> classFunction, @NonNull final Function<String, String> editor, final boolean encrypted) {
    final SharedPreferencesFileManager sharedPref = encrypted ? TestUtils.getEncryptedSharedPreferences(sharedPrefName) : TestUtils.getSharedPreferences(sharedPrefName);
    final Map<String, ?> cacheEntries = sharedPref.getAll();
    // get all the key from the cache entry, verify and edit it.
    for (final Map.Entry<String, ?> cacheEntry : cacheEntries.entrySet()) {
        final String keyToEdit = cacheEntry.getKey();
        if (predicate.test(keyToEdit)) {
            final String cacheValue = (String) cacheEntries.get(keyToEdit);
            final Class<? extends Credential> credClass = classFunction.apply(keyToEdit);
            if (credClass == null) {
                continue;
            }
            final Credential credential = CACHE_KEY_VALUE_DELEGATE.fromCacheValue(cacheValue, credClass);
            if (credential == null) {
                Logger.warn("CacheUtils:editAllTokenInCache", "Value did not deserialize");
                continue;
            }
            credential.setSecret(editor.apply(credential.getSecret()));
            sharedPref.putString(keyToEdit, CACHE_KEY_VALUE_DELEGATE.generateCacheValue(credential));
        }
    }
}
Also used : Credential(com.microsoft.identity.common.internal.dto.Credential) SharedPreferencesFileManager(com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager) Map(java.util.Map)

Aggregations

Credential (com.microsoft.identity.common.internal.dto.Credential)64 Test (org.junit.Test)45 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)33 PrimaryRefreshTokenRecord (com.microsoft.identity.common.internal.dto.PrimaryRefreshTokenRecord)31 AccessTokenRecord (com.microsoft.identity.common.internal.dto.AccessTokenRecord)30 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 JsonElement (com.google.gson.JsonElement)7 JsonPrimitive (com.google.gson.JsonPrimitive)7 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)7 CredentialType (com.microsoft.identity.common.internal.dto.CredentialType)3 Map (java.util.Map)3 Nullable (androidx.annotation.Nullable)2 HashSet (java.util.HashSet)2 NonNull (androidx.annotation.NonNull)1 ClientException (com.microsoft.identity.common.exception.ClientException)1 SharedPreferencesFileManager (com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager)1 CacheEndEvent (com.microsoft.identity.common.internal.telemetry.events.CacheEndEvent)1 CacheStartEvent (com.microsoft.identity.common.internal.telemetry.events.CacheStartEvent)1