-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from LeComptoirDesPharmacies/features/fix-jwt-d…
…uration Fix problem using expired token because Instant.now is computed after…
- Loading branch information
Showing
5 changed files
with
33 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 17 additions & 18 deletions
35
src/main/java/fr/lecomptoirdespharmacies/offisante/esignature/client/entity/Token.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
...va/fr/lecomptoirdespharmacies/offisante/esignature/client/repository/TokenRepository.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters