-
Notifications
You must be signed in to change notification settings - Fork 30
JwtToken,User-Agent validation changes #77
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.iemr.admin.utils.http.AuthorizationHeaderRequestWrapper; | ||
|
||
import jakarta.servlet.Filter; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
|
@@ -72,25 +74,35 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo | |
if (jwtFromCookie != null) { | ||
logger.info("Validating JWT token from cookie"); | ||
if (jwtAuthenticationUtil.validateUserIdAndJwtToken(jwtFromCookie)) { | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
AuthorizationHeaderRequestWrapper authorizationHeaderRequestWrapper = new AuthorizationHeaderRequestWrapper( | ||
request, ""); | ||
filterChain.doFilter(authorizationHeaderRequestWrapper, servletResponse); | ||
return; | ||
} | ||
} | ||
|
||
if (jwtFromHeader != null) { | ||
} else if (jwtFromHeader != null) { | ||
logger.info("Validating JWT token from header"); | ||
if (jwtAuthenticationUtil.validateUserIdAndJwtToken(jwtFromHeader)) { | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
AuthorizationHeaderRequestWrapper authorizationHeaderRequestWrapper = new AuthorizationHeaderRequestWrapper( | ||
request, ""); | ||
filterChain.doFilter(authorizationHeaderRequestWrapper, servletResponse); | ||
return; | ||
} | ||
} else { | ||
String userAgent = request.getHeader("User-Agent"); | ||
logger.info("User-Agent: " + userAgent); | ||
if (userAgent != null && isMobileClient(userAgent) && authHeader != null) { | ||
try { | ||
UserAgentContext.setUserAgent(userAgent); | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
} finally { | ||
UserAgentContext.clear(); | ||
} | ||
return; | ||
} | ||
} | ||
String userAgent = request.getHeader("User-Agent"); | ||
logger.info("User-Agent: " + userAgent); | ||
|
||
if (userAgent != null && isMobileClient(userAgent) && authHeader != null) { | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
return; | ||
} | ||
logger.warn("No valid authentication token found"); | ||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized: Invalid or missing token"); | ||
Comment on lines
+104
to
+105
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. Duplicate error message There's a duplicate error message at lines 104-105 and 107-108. One of these blocks should be removed to avoid redundant logging and response. - logger.warn("No valid authentication token found");
- response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized: Invalid or missing token");
π€ Prompt for AI Agents
|
||
|
||
logger.warn("No valid authentication token found"); | ||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized: Invalid or missing token"); | ||
|
This file contains hidden or 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,50 @@ | ||
package com.iemr.admin.utils; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
public class RestTemplateUtil { | ||
private final static Logger logger = LoggerFactory.getLogger(RestTemplateUtil.class); | ||
|
||
public static HttpEntity<Object> createRequestEntity(Object body, String authorization) { | ||
|
||
ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()); | ||
if (servletRequestAttributes == null) { | ||
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); | ||
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8"); | ||
headers.add(HttpHeaders.AUTHORIZATION, authorization); | ||
return new HttpEntity<>(body, headers); | ||
} | ||
HttpServletRequest requestHeader = servletRequestAttributes.getRequest(); | ||
String jwtTokenFromCookie = null; | ||
try { | ||
jwtTokenFromCookie = CookieUtil.getJwtTokenFromCookie(requestHeader); | ||
|
||
} catch (Exception e) { | ||
logger.error("Error while getting jwtToken from Cookie" + e.getMessage() ); | ||
} | ||
|
||
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); | ||
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8"); | ||
if(null != UserAgentContext.getUserAgent()) { | ||
headers.add(HttpHeaders.USER_AGENT, UserAgentContext.getUserAgent()); | ||
} | ||
headers.add(HttpHeaders.AUTHORIZATION, authorization); | ||
headers.add("JwtToken",requestHeader.getHeader("JwtToken")); | ||
if(null != jwtTokenFromCookie) { | ||
headers.add(HttpHeaders.COOKIE, "Jwttoken=" + jwtTokenFromCookie); | ||
} | ||
|
||
return new HttpEntity<>(body, headers); | ||
} | ||
|
||
} |
This file contains hidden or 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,18 @@ | ||
package com.iemr.admin.utils; | ||
|
||
public class UserAgentContext { | ||
private static final ThreadLocal<String> userAgentHolder = new ThreadLocal<>(); | ||
|
||
public static void setUserAgent(String userAgent) { | ||
userAgentHolder.set(userAgent); | ||
} | ||
|
||
public static String getUserAgent() { | ||
return userAgentHolder.get(); | ||
} | ||
|
||
public static void clear() { | ||
userAgentHolder.remove(); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/iemr/admin/utils/http/AuthorizationHeaderRequestWrapper.java
This file contains hidden or 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,43 @@ | ||
package com.iemr.admin.utils.http; | ||
|
||
import java.util.Collections; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletRequestWrapper; | ||
|
||
public class AuthorizationHeaderRequestWrapper extends HttpServletRequestWrapper{ | ||
private final String Authorization; | ||
|
||
public AuthorizationHeaderRequestWrapper(HttpServletRequest request, String authHeaderValue) { | ||
super(request); | ||
this.Authorization = authHeaderValue; | ||
} | ||
|
||
@Override | ||
public String getHeader(String name) { | ||
if ("Authorization".equalsIgnoreCase(name)) { | ||
return Authorization; | ||
} | ||
return super.getHeader(name); | ||
} | ||
|
||
@Override | ||
public Enumeration<String> getHeaders(String name) { | ||
if ("Authorization".equalsIgnoreCase(name)) { | ||
return Collections.enumeration(Collections.singletonList(Authorization)); | ||
} | ||
return super.getHeaders(name); | ||
} | ||
|
||
@Override | ||
public Enumeration<String> getHeaderNames() { | ||
List<String> names = Collections.list(super.getHeaderNames()); | ||
if (!names.contains("Authorization")) { | ||
names.add("Authorization"); | ||
} | ||
return Collections.enumeration(names); | ||
} | ||
} | ||
|
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
π‘ Verification agent
π§© Analysis chain
Good refactoring using centralized HTTP request entity creation
The refactoring simplifies the code by leveraging the new
RestTemplateUtil.createRequestEntity
method, which centralizes HTTP request construction and improves consistency by ensuring all necessary headers are included. This change removes duplicate code and makes the implementation more maintainable.However, the URL being used appears to be for creating feedback but is used in a method for getting CTI campaign roles. Verify that this is the correct endpoint:
π Script executed:
Length of output: 383
π Script executed:
Length of output: 4265
Fix endpoint property key in getCTICampaignRoles
The code is using the undefined
"create-feedback"
property for fetching campaign roles. Use the"campaign-roles-url"
key defined in application.properties instead.π Committable suggestion
π€ Prompt for AI Agents