Skip to content

Commit 4da0340

Browse files
Skip JwtToken validation for mobile app (#79)
* Skip the JwtToken validation if request coming from mobile * Added condition for Android,ios * CI properties change * Verified acceptance crietaria conditions * Indent and okhttp validated
1 parent 5314e79 commit 4da0340

2 files changed

Lines changed: 46 additions & 21 deletions

File tree

src/main/environment/common_ci.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,6 @@ logging.level.org.springframework=INFO
113113
logging.path=logs/
114114
logging.file.name=@env.FHIR_API_LOGGING_FILE_NAME@
115115
jwt.secret=@env.JWT_SECRET_KEY@
116+
117+
springdoc.api-docs.enabled=@env.SWAGGER_DOC_ENABLED@
118+
springdoc.swagger-ui.enabled=@env.SWAGGER_DOC_ENABLED@

src/main/java/com/wipro/fhir/utils/JwtUserIdValidationFilter.java

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,65 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
5353
logger.info("JWT token from header: ");
5454

5555
// Skip login and public endpoints
56-
if (path.equals(contextPath + "/user/userAuthenticate")
57-
|| path.equalsIgnoreCase(contextPath + "/user/logOutUserFromConcurrentSession")
58-
|| path.startsWith(contextPath + "/swagger-ui")
59-
|| path.startsWith(contextPath + "/v3/api-docs")
60-
|| path.startsWith(contextPath + "/public")) {
61-
logger.info("Skipping filter for path: " + path);
56+
if (shouldSkipPath(path, contextPath)) {
6257
filterChain.doFilter(servletRequest, servletResponse);
6358
return;
6459
}
6560

6661
try {
67-
// Retrieve JWT token from cookies
68-
String jwtTokenFromCookie = getJwtTokenFromCookies(request);
69-
logger.info("JWT token from cookie: ");
70-
71-
// Determine which token (cookie or header) to validate
72-
String jwtToken = jwtTokenFromCookie != null ? jwtTokenFromCookie : jwtTokenFromHeader;
73-
if (jwtToken == null) {
74-
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "JWT token not found in cookies or headers");
75-
return;
62+
String jwtFromCookie = getJwtTokenFromCookies(request);
63+
String jwtFromHeader = request.getHeader("JwtToken");
64+
String authHeader = request.getHeader("Authorization");
65+
66+
if (jwtFromCookie != null) {
67+
logger.info("Validating JWT token from cookie");
68+
if (jwtAuthenticationUtil.validateUserIdAndJwtToken(jwtFromCookie)) {
69+
filterChain.doFilter(servletRequest, servletResponse);
70+
return;
71+
}
7672
}
7773

78-
// Validate JWT token and userId
79-
boolean isValid = jwtAuthenticationUtil.validateUserIdAndJwtToken(jwtToken);
74+
if (jwtFromHeader != null) {
75+
logger.info("Validating JWT token from header");
76+
if (jwtAuthenticationUtil.validateUserIdAndJwtToken(jwtFromHeader)) {
77+
filterChain.doFilter(servletRequest, servletResponse);
78+
return;
79+
}
80+
}
81+
String userAgent = request.getHeader("User-Agent");
82+
logger.info("User-Agent: " + userAgent);
8083

81-
if (isValid) {
82-
// If token is valid, allow the request to proceed
84+
if (userAgent != null && isMobileClient(userAgent) && authHeader != null) {
8385
filterChain.doFilter(servletRequest, servletResponse);
84-
} else {
85-
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid JWT token");
86+
return;
8687
}
88+
89+
logger.warn("No valid authentication token found");
90+
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized: Invalid or missing token");
91+
8792
} catch (Exception e) {
8893
logger.error("Authorization error: ", e);
8994
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authorization error: " + e.getMessage());
9095
}
9196
}
9297

98+
private boolean isMobileClient(String userAgent) {
99+
if (userAgent == null)
100+
return false;
101+
102+
userAgent = userAgent.toLowerCase();
103+
104+
return userAgent.contains("okhttp"); // iOS (custom clients)
105+
}
106+
107+
private boolean shouldSkipPath(String path, String contextPath) {
108+
return path.equals(contextPath + "/user/userAuthenticate")
109+
|| path.equalsIgnoreCase(contextPath + "/user/logOutUserFromConcurrentSession")
110+
|| path.startsWith(contextPath + "/swagger-ui")
111+
|| path.startsWith(contextPath + "/v3/api-docs")
112+
|| path.startsWith(contextPath + "/public");
113+
}
114+
93115
private String getJwtTokenFromCookies(HttpServletRequest request) {
94116
Cookie[] cookies = request.getCookies();
95117
if (cookies != null) {

0 commit comments

Comments
 (0)