Skip to content

Commit

Permalink
Fix startup if no oidc issuer is defined
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 98d5e29800d5b53e153c74714fe39b0f6329b986
  • Loading branch information
pdesgarets authored and Gitlab-CI committed Dec 4, 2024
1 parent 3ea3a08 commit 29c64ca
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -24,6 +25,7 @@ public class OIDCConfig {
private static final int OIDC_WELL_KNOWN_CACHE_EXPIRATION_HOURS = 24;
public static final int JWKS_CACHE_SIZE = 10;
public static final int JWKS_CACHE_EXPIRATION_HOURS = 24;
public static final String JWK_BEAN_NAME = "jwkProvider";

@Bean
public Cache<String, String> wellKnownCache() {
Expand All @@ -34,12 +36,16 @@ public Cache<String, String> wellKnownCache() {
.build();
}

@Bean
@Bean(name = OIDCConfig.JWK_BEAN_NAME)
public JwkProvider jwkProvider(
@Value("${oidc.issuer}") String issuer,
Cache<String, String> wellKnownCache,
RestTemplate restTemplate
) throws MalformedURLException, ExecutionException {
if (StringUtils.isBlank(issuer)) {
logger.debug("Skipping OIDC Provider initialization");
return null;
}
logger.debug("Initializing OIDC Provider");
String jwksUriValue = wellKnownCache
.get(issuer, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class OIDCService {
private final ActivityLogger activityLogger;
private final ApplicationContext applicationContext;
private final Cache<String, String> wellKnownCache;
private final JwkProvider jwkProvider;

@Value("${oidc.audience}")
private String audience;
Expand Down Expand Up @@ -90,7 +89,7 @@ private void verifyToken(DecodedJWT jwt, int retries) throws Exception {
);
DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) applicationContext
.getAutowireCapableBeanFactory();
registry.destroySingleton("jwkProvider");
registry.destroySingleton(OIDCConfig.JWK_BEAN_NAME);
this.wellKnownCache.invalidateAll();
this.verifyToken(jwt, retries + 1);
} else {
Expand Down

0 comments on commit 29c64ca

Please sign in to comment.