diff --git a/pom.xml b/pom.xml
index 4ecf8ae..d238b54 100644
--- a/pom.xml
+++ b/pom.xml
@@ -53,6 +53,7 @@
1.3.5
5.10.0
8.3.3
+ 4.4.0
@@ -161,6 +162,12 @@
2.35.1
test
+
+
+ com.auth0
+ java-jwt
+ ${java-jwt-version}
+
diff --git a/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/entity/Token.java b/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/entity/Token.java
index 535c36e..fcdf9f7 100644
--- a/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/entity/Token.java
+++ b/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/entity/Token.java
@@ -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 +
- '}';
- }
}
diff --git a/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/interceptor/ApiClientRequestInterceptor.java b/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/interceptor/ApiClientRequestInterceptor.java
index be98192..077b463 100644
--- a/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/interceptor/ApiClientRequestInterceptor.java
+++ b/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/interceptor/ApiClientRequestInterceptor.java
@@ -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());
}
}
diff --git a/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/repository/TokenRepository.java b/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/repository/TokenRepository.java
deleted file mode 100644
index 8c0e3f0..0000000
--- a/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/repository/TokenRepository.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package fr.lecomptoirdespharmacies.offisante.esignature.client.repository;
-
-import fr.lecomptoirdespharmacies.offisante.esignature.client.entity.Token;
-import java.time.Duration;
-
-public class TokenRepository {
-
- // On offisante, token duration is 86400 secondes (24 hours)
- public static final Duration DEFAULT_TOKEN_DURATION = Duration.ofHours(24);
-
- private Token token;
-
- public synchronized Token findToken() {
- return token;
- }
-
- public synchronized void save(Token token) {
- this.token = token;
- }
-}
diff --git a/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/service/LoginService.java b/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/service/LoginService.java
index c15c503..16bc53c 100644
--- a/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/service/LoginService.java
+++ b/src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/service/LoginService.java
@@ -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();
}
/**
@@ -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();
}
/**
@@ -48,12 +45,9 @@ private Token login(){
ApiResponse response = authApi.loginWithHttpInfo(loginRequest);
Token newToken = new Token(
- response.getData().getToken(),
- TokenRepository.DEFAULT_TOKEN_DURATION
+ response.getData().getToken()
);
- tokenRepository.save(newToken);
-
return newToken;
}
@@ -61,6 +55,6 @@ private Token login(){
* Reset a token
*/
public synchronized void resetToken() {
- tokenRepository.save(null);
+ this.currentToken = null;
}
}