Skip to content
Prev Previous commit
Next Next commit
test
  • Loading branch information
moarychan committed Dec 31, 2024
commit 7bc13905d57a2a612b0a4bbdacb53da2e6574e93
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package com.azure.identity.extensions.implementation.cache;

import com.azure.core.credential.TokenCredential;
import com.azure.identity.extensions.implementation.credential.TokenCredentialProviderOptions;

import java.util.concurrent.ConcurrentHashMap;

public class InMemoryTokenCredentialCache implements TokenCredentialCache {

private static final ConcurrentHashMap<String, TokenCredential> cache = new ConcurrentHashMap<>();

@Override
public void put(TokenCredentialProviderOptions options, TokenCredential value) {
cache.putIfAbsent(getKey(options), value);
public void put(String key, TokenCredential value) {
cache.putIfAbsent(key, value);
}

@Override
public TokenCredential get(TokenCredentialProviderOptions options) {
return cache.get(getKey(options));
public TokenCredential get(String key) {
return cache.get(key);
}

@Override
public void remove(TokenCredentialProviderOptions options) {
cache.remove(getKey(options));
public void remove(String key) {
cache.remove(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@
import com.azure.core.credential.TokenCredential;
import com.azure.identity.extensions.implementation.credential.TokenCredentialProviderOptions;

import java.util.Arrays;
import java.util.stream.Collectors;

public interface TokenCredentialCache {

void put(TokenCredentialProviderOptions options, TokenCredential value);
void put(String key, TokenCredential value);

TokenCredential get(TokenCredentialProviderOptions options);
TokenCredential get(String key);

void remove(TokenCredentialProviderOptions options);
void remove(String key);

default String getKey(TokenCredentialProviderOptions options) {
// todo implement the key generation
return "";
return joinOptions(options.getTenantId(), options.getClientId(), options.getClientCertificatePath(),
options.getUsername(), String.valueOf(options.isManagedIdentityEnabled()),
options.getTokenCredentialProviderClassName(), options.getTokenCredentialBeanName(),
options.getTokenCredentialCacheClassName());
}

static String joinOptions(String... options) {
return Arrays.stream(options).map(TokenCredentialCache::nonNullOption).collect(Collectors.joining(","));
}

static String nonNullOption(String option) {
return option == null ? "" : option;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public static TokenCredentialCache createInstance(String cacheClassName) {
return new InMemoryTokenCredentialCache();
}

Class<? extends TokenCredentialCache> clazz
= ClassUtil.getClass(cacheClassName, TokenCredentialCache.class);
Class<? extends TokenCredentialCache> clazz = ClassUtil.getClass(cacheClassName, TokenCredentialCache.class);
if (clazz == null) {
clazz = defaultCacheClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public class DefaultTokenCredentialProvider implements TokenCredentialProvider {
this.tokenCredential = get(this.options);

if (this.options.isTokenCredentialCacheEnabled()) {
tokenCredentialCache = TokenCredentialCacheHelper.createInstance(this.options.getTokenCredentialCacheClassName());
tokenCredentialCache
= TokenCredentialCacheHelper.createInstance(this.options.getTokenCredentialCacheClassName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,20 @@ default TokenCredentialProviderOptions getTokenCredentialProviderOptions() {
}

default TokenCredential getFromCache() {
TokenCredentialCache tokenCredentialCache = getTokenCredentialCache();
TokenCredentialProviderOptions providerOptions = getTokenCredentialProviderOptions();
if (providerOptions != null && tokenCredentialCache != null && providerOptions.isTokenCredentialCacheEnabled()) {
TokenCredential cachedTokenCredential = tokenCredentialCache.get(providerOptions);
TokenCredentialCache cache = getTokenCredentialCache();
TokenCredentialProviderOptions options = getTokenCredentialProviderOptions();
if (options != null && cache != null && options.isTokenCredentialCacheEnabled()) {
TokenCredential cachedTokenCredential = cache.get(cache.getKey(options));
if (cachedTokenCredential != null) {
LOGGER.verbose("Returning TokenCredential from cache.");
LOGGER.verbose("Returning token credential from cache.");
return cachedTokenCredential;
}
}

TokenCredential tokenCredential = get();
if (providerOptions != null && tokenCredentialCache != null && providerOptions.isTokenCredentialCacheEnabled()) {
tokenCredentialCache.put(providerOptions, tokenCredential);
if (options != null && cache != null && options.isTokenCredentialCacheEnabled()) {
cache.put(cache.getKey(options), tokenCredential);
LOGGER.verbose("The token credential cached.");
}
return tokenCredential;
}
Expand Down