Search in sources :

Example 6 with SharedPreferencesFileManager

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

the class CacheUtils method clear.

public void clear(String sharedPrefName, boolean encrypted) {
    final SharedPreferencesFileManager sharedPref = encrypted ? TestUtils.getEncryptedSharedPreferences(sharedPrefName) : TestUtils.getSharedPreferences(sharedPrefName);
    sharedPref.clear();
}
Also used : SharedPreferencesFileManager(com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager)

Example 7 with SharedPreferencesFileManager

use of com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager 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)

Example 8 with SharedPreferencesFileManager

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

the class TestUtils method removeAccessTokenFromCache.

public static void removeAccessTokenFromCache(final String sharedPrefName) {
    SharedPreferencesFileManager sharedPreferences = getSharedPreferences(sharedPrefName);
    final Map<String, ?> cacheValues = sharedPreferences.getAll();
    final String keyToRemove = getCacheKeyForAccessToken(cacheValues);
    if (keyToRemove != null) {
        sharedPreferences.remove(keyToRemove);
    }
}
Also used : SharedPreferencesFileManager(com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager)

Aggregations

SharedPreferencesFileManager (com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager)8 StorageHelper (com.microsoft.identity.common.adal.internal.cache.StorageHelper)3 Context (android.content.Context)2 CacheKeyValueDelegate (com.microsoft.identity.common.internal.cache.CacheKeyValueDelegate)2 IAccountCredentialCache (com.microsoft.identity.common.internal.cache.IAccountCredentialCache)2 SharedPreferencesAccountCredentialCache (com.microsoft.identity.common.internal.cache.SharedPreferencesAccountCredentialCache)2 AccountRecord (com.microsoft.identity.common.internal.dto.AccountRecord)2 Test (org.junit.Test)2 UiThreadTest (androidx.test.annotation.UiThreadTest)1 BearerAuthenticationSchemeInternal (com.microsoft.identity.common.internal.authscheme.BearerAuthenticationSchemeInternal)1 ICacheRecord (com.microsoft.identity.common.internal.cache.ICacheRecord)1 MicrosoftStsAccountCredentialAdapter (com.microsoft.identity.common.internal.cache.MicrosoftStsAccountCredentialAdapter)1 MsalOAuth2TokenCache (com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache)1 Credential (com.microsoft.identity.common.internal.dto.Credential)1 IdTokenRecord (com.microsoft.identity.common.internal.dto.IdTokenRecord)1 RefreshTokenRecord (com.microsoft.identity.common.internal.dto.RefreshTokenRecord)1 ClientInfo (com.microsoft.identity.common.internal.providers.microsoft.azureactivedirectory.ClientInfo)1 Date (java.util.Date)1 Map (java.util.Map)1