Skip to content

Commit

Permalink
Merge pull request #4 from LeComptoirDesPharmacies/features/fix-jwt-d…
Browse files Browse the repository at this point in the history
…uration

Fix problem using expired token because Instant.now is computed after…
  • Loading branch information
AntoineDuComptoirDesPharmacies authored May 3, 2024
2 parents 21f0e66 + 64a673a commit c0f82f8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 53 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<jakarta-annotation-version>1.3.5</jakarta-annotation-version>
<junit-version>5.10.0</junit-version>
<scribejava-version>8.3.3</scribejava-version>
<java-jwt-version>4.4.0</java-jwt-version>
</properties>


Expand Down Expand Up @@ -161,6 +162,12 @@
<version>2.35.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>${java-jwt-version}</version>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
package fr.lecomptoirdespharmacies.offisante.esignature.client.entity;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;

public class Token {
private final String token;
private final LocalDateTime createdAt;
private final Duration duration;

public Token(String token, Duration duration) {
this.token = token;
this.createdAt = LocalDateTime.now();
this.duration = duration;

private static Duration VALIDITY_MINIMUM_DELAY = Duration.ofSeconds(30);

private final DecodedJWT decodedJWT;

public Token(String token) {
this.decodedJWT = JWT.decode(token);
}

public String getToken() {
return token;
return decodedJWT.getToken();
}

public boolean isExpired() {
return LocalDateTime.now().isAfter(createdAt.plus(duration));
return decodedJWT
.getExpiresAtAsInstant()
.isBefore(
Instant.now().minus(VALIDITY_MINIMUM_DELAY)
);
}

@Override
public String toString() {
return "Token{" +
"token='" + token.substring(0, 32) + "..." +
", createdAt=" + createdAt +
", duration=" + duration +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public ApiClientRequestInterceptor(LoginService loginService) {

@Override
public void apply(feign.RequestTemplate template) {
template.header("x-access-token", loginService.getValidToken());
template.header("x-access-token", loginService.getValidAccessToken());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@
import fr.lecomptoirdespharmacies.offisante.esignature.model.LoginRequest;
import fr.lecomptoirdespharmacies.offisante.esignature.model.ValidTokenResponse;
import fr.lecomptoirdespharmacies.offisante.esignature.client.entity.Token;
import fr.lecomptoirdespharmacies.offisante.esignature.client.repository.TokenRepository;

import java.util.Objects;

public class LoginService {
private final LoginRequest loginRequest;
private final ApiClient apiClient;
private final TokenRepository tokenRepository;

private Token currentToken;

public LoginService(LoginRequest loginRequest, ApiClient apiClient) {
this.loginRequest = loginRequest;
this.apiClient = apiClient;
this.tokenRepository = new TokenRepository();
}

/**
Expand All @@ -27,15 +26,13 @@ public LoginService(LoginRequest loginRequest, ApiClient apiClient) {
*
* @return a valid token
*/
public synchronized String getValidToken(){
Token token = tokenRepository.findToken();

public synchronized String getValidAccessToken(){
// If token is null or expired, create a new token
if(Objects.isNull(token) || token.isExpired()) {
token = login();
if(Objects.isNull(currentToken) || currentToken.isExpired()) {
currentToken = login();
}

return token.getToken();
return currentToken.getToken();
}

/**
Expand All @@ -48,19 +45,16 @@ private Token login(){
ApiResponse<ValidTokenResponse> response = authApi.loginWithHttpInfo(loginRequest);

Token newToken = new Token(
response.getData().getToken(),
TokenRepository.DEFAULT_TOKEN_DURATION
response.getData().getToken()
);

tokenRepository.save(newToken);

return newToken;
}

/**
* Reset a token
*/
public synchronized void resetToken() {
tokenRepository.save(null);
this.currentToken = null;
}
}

0 comments on commit c0f82f8

Please sign in to comment.