Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 32
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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"); | ||
| 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); | ||
| } | ||
| } |
| 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(); | ||
| } | ||
| } |
| 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); | ||
| } | ||
| } | ||
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.createRequestEntitymethod, 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