-
Notifications
You must be signed in to change notification settings - Fork 45
Force logout #207
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
Force logout #207
Changes from all commits
29f9d13
9921859
aa74de0
04b3d99
05065b8
a0ede18
d7d7fba
7eb2a89
7ad12c1
1d20ed1
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 |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -83,6 +86,8 @@ | |
private CookieUtil cookieUtil; | ||
@Autowired | ||
private RedisTemplate<String, Object> redisTemplate; | ||
@Value("${jwt.blacklist.expiration}") | ||
private static long BLACK_LIST_EXPIRATION_TIME; | ||
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. Each token will have it's own expiration time. Why would this be a constant? |
||
|
||
private AESUtil aesUtil; | ||
|
||
|
@@ -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); | ||
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. 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
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. If this method is not going to be used, let's remove it from code. |
||
// Set the response message | ||
outputResponse.setResponse("Success"); | ||
|
@@ -944,7 +954,17 @@ | |
} | ||
return outputResponse.toString(); | ||
} | ||
|
||
private String getJwtTokenFromCookies(HttpServletRequest request) { | ||
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. 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(); | ||
|
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 { | ||
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. Please use "Deny" instead of Black |
||
|
||
|
||
// Store blacklisted tokens (in-memory) | ||
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. 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) { | ||
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. This looks like application memory. |
||
blacklistedTokens.remove(token); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} |
Check notice
Code scanning / SonarCloud
Injecting data into static fields is not supported by Spring