Skip to content

Commit

Permalink
Merge pull request #27 from FINTLabs/FLA-339-add-retry-to-refresh-token
Browse files Browse the repository at this point in the history
added retry on refreshToken
  • Loading branch information
sondre81 authored May 14, 2024
2 parents 3da66e1 + a3bc8db commit 309e823
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/no/fintlabs/oidc/OidcService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import javax.annotation.PostConstruct;
import java.net.URI;
import java.security.interfaces.RSAPublicKey;
import java.sql.Time;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
Expand Down Expand Up @@ -58,6 +60,9 @@ public class OidcService {
@Setter
private Jwk jwk;

public static final Long RETRY_ATTEMPTS = 5L;
public static final Duration DELAY = Duration.ofSeconds((long) Math.pow(1L, 5L));

public OidcService(ApplicationConfiguration applicationConfiguration, WebClient webClient, SessionService sessionService, CookieService cookieService, OidcRequestFactory oidcRequestFactory) {
this.applicationConfiguration = applicationConfiguration;
this.webClient = webClient;
Expand Down Expand Up @@ -123,6 +128,11 @@ public void refreshToken(String state, Token token) {
)
.retrieve()
.bodyToMono(Token.class)
.retryWhen(Retry.fixedDelay(RETRY_ATTEMPTS, DELAY)
.doAfterRetry(retrySignal -> log.debug("Retried {} times.", retrySignal.totalRetries()))
.onRetryExhaustedThrow(((retryBackoffSpec, retrySignal) -> retrySignal.failure()))
)
.doOnSuccess(tokenResponse -> log.debug("Successfully refreshed token for: {}", tokenResponse.getAccessToken()))
.doOnError(WebClientResponseException.class, ex -> {
log.error("Error occured for clientId: {}", applicationConfiguration.getClientId());
log.error("WebClientResponseException occurred: {}", ex.getMessage());
Expand Down

0 comments on commit 309e823

Please sign in to comment.