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;
}
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;
}
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;
}
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));
}
}
}
Aggregations