forked from cytomine/Cytomine-core-spring
-
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.
- Loading branch information
1 parent
fcd7c77
commit d5f5fc5
Showing
5 changed files
with
98 additions
and
6 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
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
53 changes: 53 additions & 0 deletions
53
src/main/java/be/cytomine/security/jwt/JwtTokenGenerator.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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* This file is used to generate a JWT token with a payload, secret key, algorithm, and expiry time. | ||
* | ||
* The main purpose is generate temporary access keys for the IMS through a shared secret key. | ||
*/ | ||
|
||
package be.cytomine.security.jwt; | ||
|
||
import be.cytomine.config.properties.ApplicationProperties; | ||
|
||
import java.util.Map; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import io.jsonwebtoken.security.Keys; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.Key; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
|
||
|
||
public class JwtTokenGenerator { | ||
|
||
public static Map<String, Object> generateJwtToken(ApplicationProperties applicationProperties, | ||
Map<String, Object> payload, | ||
SignatureAlgorithm algorithm, long expiryMinutes) { | ||
|
||
// Get the current time in UTC | ||
Instant nowUtc = Instant.now(); | ||
String secret = applicationProperties.getAuthentication().getJwt().getSecret(); | ||
// Calculate the expiry time in UTC | ||
Instant expiryUtc = nowUtc.plus(expiryMinutes, ChronoUnit.MINUTES); | ||
byte[] keyBytes = secret.getBytes(StandardCharsets.UTF_8); | ||
Key key = Keys.hmacShaKeyFor(keyBytes); | ||
// Generate the JWT token | ||
String token = Jwts.builder() | ||
.setClaims(payload) | ||
.setExpiration(Date.from(expiryUtc)) | ||
.signWith(key, algorithm) | ||
.compact(); | ||
|
||
Map<String, Object> result = new HashMap<>(); | ||
result.put("token", token); | ||
result.put("expiryTime", expiryUtc); | ||
return result; | ||
} | ||
|
||
public static Map<String, Object> generateJwtToken(ApplicationProperties applicationProperties, Map<String, Object> payload) { | ||
return generateJwtToken(applicationProperties, payload, SignatureAlgorithm.HS256, 30); | ||
} | ||
} |
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
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