Skip to content

Commit

Permalink
Changed behaviour to not throw an exception immediately if a key is m…
Browse files Browse the repository at this point in the history
…issing. Update cache before and try again before fail
  • Loading branch information
Mme-adorsys committed Mar 12, 2024
1 parent 73d41b7 commit b9a050e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions sts-token-auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>2.0.5</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import lombok.Setter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;

import java.net.URL;
import java.security.Key;
Expand Down Expand Up @@ -68,6 +70,8 @@ private void updateJwkCache() throws JsonWebKeyRetrievalException {
log.debug("Thread leaving updateJwkCache: " + Thread.currentThread().getId());
}

@Retryable(maxAttempts = 2, backoff = @Backoff(delay = 1000, multiplier = 2), retryFor =
{JsonWebKeyNotFoundException.class}, noRetryFor = {JsonWebKeyRetrievalException.class})
public Key getJWK(String keyID) throws JsonWebKeyRetrievalException {
log.debug("Thread entering getJWK: {}", Thread.currentThread().getId());

Expand All @@ -84,7 +88,9 @@ public Key getJWK(String keyID) throws JsonWebKeyRetrievalException {
JWK jwk = jwkCache.get(keyID);
if (jwk == null) {
log.error("Key with ID {} not found in cache", keyID);
throw new JsonWebKeyRetrievalException("Key with ID " + keyID + " not found in cache");
//Update cache and try again
updateJwkCache();
throw new JsonWebKeyNotFoundException("Key with ID " + keyID + " not found in cache");
}

log.debug("JWK for key ID {} found in cache", keyID);
Expand Down Expand Up @@ -119,4 +125,10 @@ public JsonWebKeyRetrievalException(String message) {
super(message);
}
}

protected static class JsonWebKeyNotFoundException extends RuntimeException {
public JsonWebKeyNotFoundException(String message) {
super(message);
}
}
}

0 comments on commit b9a050e

Please sign in to comment.