Skip to content
Prev Previous commit
Next Next commit
Resolve comments
  • Loading branch information
moarychan committed Jan 2, 2025
commit f9ee905d5273ef0c7fa6d1daa145b38f04910696

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class TokenCredentialProviderOptions {
private String tokenCredentialProviderClassName;
private String tokenCredentialBeanName;
private String accessTokenTimeoutInSeconds;
private String tokenCredentialCacheClassName;
private String accessTokenCacheClassName;

public TokenCredentialProviderOptions() {

Expand All @@ -44,8 +42,6 @@ public TokenCredentialProviderOptions(Properties properties) {
this.managedIdentityEnabled = Boolean.TRUE.equals(AuthProperty.MANAGED_IDENTITY_ENABLED.getBoolean(properties));
this.tokenCredentialProviderClassName = AuthProperty.TOKEN_CREDENTIAL_PROVIDER_CLASS_NAME.get(properties);
this.tokenCredentialBeanName = AuthProperty.TOKEN_CREDENTIAL_BEAN_NAME.get(properties);
this.tokenCredentialCacheClassName = AuthProperty.TOKEN_CREDENTIAL_CACHE_CLASS_NAME.get(properties);
this.accessTokenCacheClassName = AuthProperty.ACCESS_TOKEN_CACHE_CLASS_NAME.get(properties);
this.accessTokenTimeoutInSeconds = AuthProperty.GET_TOKEN_TIMEOUT.get(properties);
this.authorityHost = AuthProperty.AUTHORITY_HOST.get(properties);
}
Expand Down Expand Up @@ -145,20 +141,4 @@ public String getAccessTokenTimeoutInSeconds() {
public void setAccessTokenTimeoutInSeconds(String accessTokenTimeoutInSeconds) {
this.accessTokenTimeoutInSeconds = accessTokenTimeoutInSeconds;
}

public String getTokenCredentialCacheClassName() {
return tokenCredentialCacheClassName;
}

public void setTokenCredentialCacheClassName(String tokenCredentialCacheClassName) {
this.tokenCredentialCacheClassName = tokenCredentialCacheClassName;
}

public String getAccessTokenCacheClassName() {
return accessTokenCacheClassName;
}

public void setAccessTokenCacheClassName(String accessTokenCacheClassName) {
this.accessTokenCacheClassName = accessTokenCacheClassName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import com.azure.core.credential.TokenCredential;
import com.azure.core.util.logging.ClientLogger;
import com.azure.identity.extensions.implementation.cache.IdentityCache;
import com.azure.identity.extensions.implementation.cache.IdentityCacheHelper;
import com.azure.identity.extensions.implementation.credential.TokenCredentialProviderOptions;

import java.util.concurrent.ConcurrentHashMap;

import static com.azure.identity.extensions.implementation.utils.StringUtils.getTokenCredentialCacheKey;

/**
Expand All @@ -18,9 +18,9 @@ public class DefaultCacheTokenCredentialProvider implements TokenCredentialProvi

private static final ClientLogger LOGGER = new ClientLogger(DefaultCacheTokenCredentialProvider.class);

private final TokenCredentialProviderOptions options;
private static final ConcurrentHashMap<String, TokenCredential> CACHE = new ConcurrentHashMap<>();

private final IdentityCache<String, TokenCredential> tokenCredentialCache;
private final TokenCredentialProviderOptions options;

private final DefaultTokenCredentialProvider defaultProvider;

Expand All @@ -29,30 +29,21 @@ public class DefaultCacheTokenCredentialProvider implements TokenCredentialProvi
}

DefaultCacheTokenCredentialProvider(TokenCredentialProviderOptions options) {
this(options, null);
}

DefaultCacheTokenCredentialProvider(TokenCredentialProviderOptions options, IdentityCache<String, TokenCredential> tokenCredentialCache) {
this.options = options;
if (tokenCredentialCache == null) {
this.tokenCredentialCache = IdentityCacheHelper.createTokenCredentialCacheInstance(options.getTokenCredentialCacheClassName());
} else {
this.tokenCredentialCache = tokenCredentialCache;
}
this.defaultProvider = new DefaultTokenCredentialProvider(this.options);
}

@Override
public TokenCredential get() {
String tokenCredentialCacheKey = getTokenCredentialCacheKey(options);
TokenCredential cachedTokenCredential = tokenCredentialCache.get(tokenCredentialCacheKey);
TokenCredential cachedTokenCredential = CACHE.get(tokenCredentialCacheKey);
if (cachedTokenCredential != null) {
LOGGER.verbose("Returning token credential from cache.");
return cachedTokenCredential;
}

TokenCredential tokenCredential = defaultProvider.get();
tokenCredentialCache.put(tokenCredentialCacheKey, tokenCredential);
CACHE.put(tokenCredentialCacheKey, tokenCredential);
LOGGER.verbose("The token credential cached.");
return tokenCredential;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ public enum AuthProperty {
*/
TOKEN_CREDENTIAL_BEAN_NAME("azure.tokenCredentialBeanName", "springCloudAzureDefaultCredential",
"The given bean name of a TokenCredential bean in the Spring context.", false),
TOKEN_CREDENTIAL_CACHE_CLASS_NAME("azure.tokenCredentialCacheClassName",
"The given class name of a TokenCredential cache.", false),
/**
* Whether to enable access token cache.
*/
ACCESS_TOKEN_CACHE_ENABLED("azure.accessTokenCacheEnabled", "true",
"Whether to enable the token cache.", false),
ACCESS_TOKEN_CACHE_CLASS_NAME("azure.accessTokenCacheClassName",
"The given class name of a AccessToken cache.", false);
"Whether to enable the token cache.", false);

String propertyKey;
String defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@

import com.azure.core.credential.AccessToken;
import com.azure.core.util.logging.ClientLogger;
import com.azure.identity.extensions.implementation.cache.IdentityCache;
import com.azure.identity.extensions.implementation.credential.TokenCredentialProviderOptions;
import com.azure.identity.extensions.implementation.credential.provider.TokenCredentialProvider;
import com.azure.identity.extensions.implementation.credential.TokenCredentialProviderOptions;
import com.azure.identity.extensions.implementation.enums.AuthProperty;
import com.azure.identity.extensions.implementation.token.AccessTokenResolver;
import com.azure.identity.extensions.implementation.token.AccessTokenResolverOptions;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.azure.identity.extensions.implementation.cache.IdentityCacheHelper.createAccessTokenCacheInstance;
import reactor.core.publisher.Mono;
import static com.azure.identity.extensions.implementation.enums.AuthProperty.GET_TOKEN_TIMEOUT;
import static com.azure.identity.extensions.implementation.utils.StringUtils.getAccessTokenCacheKey;

/**
* Template class can be extended to get password from access token.
Expand All @@ -34,10 +29,6 @@ public class AzureAuthenticationTemplate {

private AccessTokenResolver accessTokenResolver;

private IdentityCache<String, AccessToken> accessTokenCache;

private AccessTokenResolverOptions resolverOptions;

private long accessTokenTimeoutInSeconds;

/**
Expand Down Expand Up @@ -69,20 +60,20 @@ public void init(Properties properties) {
if (isInitialized.compareAndSet(false, true)) {
LOGGER.verbose("Initializing AzureAuthenticationTemplate.");

TokenCredentialProviderOptions providerOptions = new TokenCredentialProviderOptions(properties);
if (getTokenCredentialProvider() == null) {
this.tokenCredentialProvider
= TokenCredentialProvider.createDefault(providerOptions);
= TokenCredentialProvider.createDefault(new TokenCredentialProviderOptions(properties));
}

this.resolverOptions = new AccessTokenResolverOptions(properties);
if (getAccessTokenResolver() == null) {
this.accessTokenResolver
= AccessTokenResolver.createDefault(resolverOptions);
}

if (AuthProperty.ACCESS_TOKEN_CACHE_ENABLED.getBoolean(properties)) {
this.accessTokenCache = createAccessTokenCacheInstance(providerOptions.getAccessTokenCacheClassName());
Boolean accessTokenCacheEnabled = AuthProperty.ACCESS_TOKEN_CACHE_ENABLED.getBoolean(properties);
if (accessTokenCacheEnabled) {
this.accessTokenResolver
= AccessTokenResolver.createDefaultCache(new AccessTokenResolverOptions(properties));
} else {
this.accessTokenResolver
= AccessTokenResolver.createDefault(new AccessTokenResolverOptions(properties));
}
}

if (properties.containsKey(GET_TOKEN_TIMEOUT.getPropertyKey())) {
Expand All @@ -106,31 +97,10 @@ public Mono<String> getTokenAsPasswordAsync() {
if (!isInitialized.get()) {
throw LOGGER.logExceptionAsError(new IllegalStateException("must call init() first"));
}

if (accessTokenCache != null) {
String accessTokenCacheKey = getAccessTokenCacheKey(this.resolverOptions);
AccessToken accessToken = accessTokenCache.get(accessTokenCacheKey);
if (accessToken != null) {
if (!accessToken.isExpired()) {
LOGGER.verbose("Returning access token from cache.");
return Mono.just(accessToken.getToken());
} else {
accessTokenCache.remove(accessTokenCacheKey);
}

}
}

return Mono.fromSupplier(getTokenCredentialProvider())
.flatMap(getAccessTokenResolver())
.doOnSuccess(accessToken -> {
if (accessTokenCache != null) {
accessTokenCache.put(getAccessTokenCacheKey(this.resolverOptions), accessToken);
LOGGER.verbose("The access token cached.");
}
})
.filter(token -> !token.isExpired())
.map(AccessToken::getToken);
.flatMap(getAccessTokenResolver())
.filter(token -> !token.isExpired())
.map(AccessToken::getToken);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ static AccessTokenResolver createDefault(AccessTokenResolverOptions options) {
return new AccessTokenResolverImpl(options);
}

static AccessTokenResolver createDefaultCache(AccessTokenResolverOptions options) {
return new AccessTokenResolverCacheImpl(options);
}
}
Loading
Loading