Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/iemr/common/constant/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class Constants {
public static final String HOLD = "Hold";
public static final String NOT_READY = "Not Ready";
public static final String AUX = "Aux";
public static final String JWT_TOKEN = "Jwttoken";

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -44,6 +45,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.iemr.common.config.encryption.SecurePassword;
import com.iemr.common.constant.Constants;
import com.iemr.common.data.users.LoginSecurityQuestions;
import com.iemr.common.data.users.M_Role;
import com.iemr.common.data.users.ServiceRoleScreenMapping;
Expand All @@ -56,6 +58,7 @@
import com.iemr.common.service.users.IEMRAdminUserService;
import com.iemr.common.utils.CookieUtil;
import com.iemr.common.utils.JwtUtil;
import com.iemr.common.utils.TokenBlacklist;
import com.iemr.common.utils.encryption.AESUtil;
import com.iemr.common.utils.exception.IEMRException;
import com.iemr.common.utils.mapper.InputMapper;
Expand Down Expand Up @@ -83,6 +86,8 @@
private CookieUtil cookieUtil;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Value("${jwt.blacklist.expiration}")

Check notice

Code scanning / SonarCloud

Injecting data into static fields is not supported by Spring

<!--SONAR_ISSUE_KEY:AZcTCO8gnd2xMJsJ5hH3-->Remove this injection annotation targeting the static field. <p>See more on <a href="https://sonarcloud.io/project/issues?id=PSMRI_Common-API&issues=AZcTCO8gnd2xMJsJ5hH3&open=AZcTCO8gnd2xMJsJ5hH3&pullRequest=207">SonarQube Cloud</a></p>
private static long BLACK_LIST_EXPIRATION_TIME;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each token will have it's own expiration time. Why would this be a constant?


private AESUtil aesUtil;

Expand Down Expand Up @@ -933,9 +938,14 @@
try {
// Perform the force logout logic
iemrAdminUserServiceImpl.forceLogout(request);

String token = null;
token = getJwtTokenFromCookies(httpRequest);
if(null == token) {
token = httpRequest.getHeader(Constants.JWT_TOKEN);
}
TokenBlacklist.blacklistToken(token,BLACK_LIST_EXPIRATION_TIME);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after comma

// Extract and invalidate JWT token cookie dynamically from the request
invalidateJwtCookie(httpRequest, response);
// invalidateJwtCookie(httpRequest, response);

Comment on lines +948 to 949
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this method is not going to be used, let's remove it from code.
Setting cookie to null with Set-cookie header is still valid though.

// Set the response message
outputResponse.setResponse("Success");
Expand All @@ -944,7 +954,17 @@
}
return outputResponse.toString();
}

private String getJwtTokenFromCookies(HttpServletRequest request) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look up for current token seems to be getting repeated in so many places. Can we make this a reusable method?

Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equalsIgnoreCase("Jwttoken")) {
return cookie.getValue();
}
}
}
return null;
}
private void invalidateJwtCookie(HttpServletRequest request, HttpServletResponse response) {
// Get the cookies from the incoming request
Cookie[] cookies = request.getCookies();
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/iemr/common/utils/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ private String buildToken(String username, String userId, String tokenType, long
}

public Claims validateToken(String token) {
if (TokenBlacklist.isTokenBlacklisted(token)) {
return null;
}
try {
return Jwts.parser()
.verifyWith(getSigningKey())
.build()
.parseSignedClaims(token)
.getPayload();
return Jwts.parser().verifyWith(getSigningKey()).build().parseSignedClaims(token).getPayload();

} catch (ExpiredJwtException ex) {
// Handle expired token specifically if needed
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/iemr/common/utils/TokenBlacklist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.iemr.common.utils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Value;

public class TokenBlacklist {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use "Deny" instead of Black



// Store blacklisted tokens (in-memory)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does in memory mean? We should store this in Redis.

private static final Map<String, Long> blacklistedTokens = new ConcurrentHashMap<>();


// Add a token to the blacklist
public static void blacklistToken(String token ,Long blackListExpirationTime) {
if(token == null || token.trim().isEmpty()) {
return;
}
blacklistedTokens.put(token, System.currentTimeMillis()+ blackListExpirationTime);
}

// Check if a token is blacklisted

public static boolean isTokenBlacklisted(String token) {
if(token == null || token.trim().isEmpty()) {
return false;
}
Long expiry = blacklistedTokens.get(token);
if (expiry == null) return false;
// If token is expired, remove it from blacklist and treat as not blacklisted
if (System.currentTimeMillis() > expiry) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like application memory.
Not the best place to keep.
Also it looks like token gets removed only in subsequent request.

blacklistedTokens.remove(token);
return false;
}
return true;
}

}
Loading