-
Notifications
You must be signed in to change notification settings - Fork 31
fix: ensure jwt is not in deny list before further authentication #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.iemr.mmu.utils; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @Component | ||
| public class TokenDenylist { | ||
| private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); | ||
|
|
||
| private static final String PREFIX = "denied_"; | ||
|
|
||
| @Autowired | ||
| private RedisTemplate<String, Object> redisTemplate; | ||
|
|
||
| private String getKey(String jti) { | ||
| return PREFIX + jti; | ||
| } | ||
|
|
||
| // Add a token's jti to the denylist with expiration time | ||
| public void addTokenToDenylist(String jti, Long expirationTime) { | ||
| if (jti == null || jti.trim().isEmpty()) { | ||
| return; | ||
| } | ||
| if (expirationTime == null || expirationTime <= 0) { | ||
| throw new IllegalArgumentException("Expiration time must be positive"); | ||
| } | ||
|
|
||
| try { | ||
| String key = getKey(jti); // Use helper method to get the key | ||
| redisTemplate.opsForValue().set(key, " ", expirationTime, TimeUnit.MILLISECONDS); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to denylist token", e); | ||
| } | ||
| } | ||
|
Comment on lines
+24
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π οΈ Refactor suggestion Consider consistent error handling strategy across methods. The method throws a Consider adopting a uniform approach:
Apply this diff to handle Redis failures gracefully: public void addTokenToDenylist(String jti, Long expirationTime) {
if (jti == null || jti.trim().isEmpty()) {
return;
}
if (expirationTime == null || expirationTime <= 0) {
throw new IllegalArgumentException("Expiration time must be positive");
}
try {
String key = getKey(jti); // Use helper method to get the key
redisTemplate.opsForValue().set(key, " ", expirationTime, TimeUnit.MILLISECONDS);
} catch (Exception e) {
- throw new RuntimeException("Failed to denylist token", e);
+ logger.error("Failed to add token to denylist for jti: " + jti, e);
+ // Optionally, you could track this failure for monitoring
}
}Also, consider using a more meaningful value instead of a space character: - redisTemplate.opsForValue().set(key, " ", expirationTime, TimeUnit.MILLISECONDS);
+ redisTemplate.opsForValue().set(key, "true", expirationTime, TimeUnit.MILLISECONDS);π€ Prompt for AI Agents |
||
|
|
||
| // Check if a token's jti is in the denylist | ||
| public boolean isTokenDenylisted(String jti) { | ||
| if (jti == null || jti.trim().isEmpty()) { | ||
| return false; | ||
| } | ||
| try { | ||
| String key = getKey(jti); // Use helper method to get the key | ||
| return Boolean.TRUE.equals(redisTemplate.hasKey(key)); | ||
| } catch (Exception e) { | ||
| logger.error("Failed to check denylist status for jti: " + jti, e); | ||
| // In case of Redis failure, consider the token as not denylisted to avoid blocking all requests | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add exception handling for consistency with validateToken.
The
extractAllClaimsmethod doesn't handle exceptions, which could cause runtime errors when called fromextractClaimorextractUsername. This is inconsistent withvalidateTokenwhich catches all exceptions.Apply this diff to handle exceptions consistently:
private Claims extractAllClaims(String token) { + try { return Jwts.parser() .verifyWith(getSigningKey()) .build() .parseSignedClaims(token) .getPayload(); + } catch (Exception e) { + return null; // Consistent with validateToken behavior + } }π Committable suggestion
π€ Prompt for AI Agents