fix(api): add refresh token nbf to prevent immediate usage (#2245)
This commit is contained in:
@@ -43,6 +43,8 @@ public class JwtUtils {
|
||||
@Getter
|
||||
public static final long refreshTokenExpirationMs = 1000L * 60 * 60 * 24 * 30; // 30 days
|
||||
|
||||
private static final long refreshTokenNotBeforeMs = 1000L * 60 * 2; // 2 minutes
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
validateSecret();
|
||||
@@ -79,15 +81,22 @@ public class JwtUtils {
|
||||
try {
|
||||
JWSSigner signer = new MACSigner(getSecretBytes());
|
||||
|
||||
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
|
||||
var builder = new JWTClaimsSet.Builder()
|
||||
.issuer(JWT_ISSUER)
|
||||
.jwtID(UUID.randomUUID().toString())
|
||||
.subject(user.getUsername())
|
||||
.claim("userId", user.getId())
|
||||
.claim("isDefaultPassword", user.isDefaultPassword())
|
||||
.issueTime(Date.from(now))
|
||||
.expirationTime(Date.from(now.plusMillis(expirationTime)))
|
||||
.build();
|
||||
.expirationTime(Date.from(now.plusMillis(expirationTime)));
|
||||
|
||||
if (isRefreshToken) {
|
||||
// Prevent refresh tokens from being used until at least
|
||||
// some `refreshTokenNotBeforeMs` milliseconds from now.
|
||||
builder.notBeforeTime(Date.from(now.plusMillis(refreshTokenNotBeforeMs)));
|
||||
}
|
||||
|
||||
JWTClaimsSet claimsSet = builder.build();
|
||||
|
||||
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet);
|
||||
signedJWT.sign(signer);
|
||||
|
||||
Reference in New Issue
Block a user