Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed behaviour to not throw an exception immediately if a key is m… #185

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}