-
Notifications
You must be signed in to change notification settings - Fork 31
AMM-1456 : JWT validation #84
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
9 commits
Select commit
Hold shift + click to select a range
cb59cfe
Skip the JwtToken validation if request coming from mobile
ravishanigarapu 54d149f
Added condition for Android,ios
ravishanigarapu d926226
CI properties change
ravishanigarapu 48ed61d
Verified acceptance crietaria conditions
ravishanigarapu 8b203a4
Indent and okhttp validated
ravishanigarapu 06015ae
Handled Authorization in Header
ravishanigarapu b6dae7d
Jwttoken and user-agent validation
ravishanigarapu db54cfb
null check
ravishanigarapu fb8f028
Merge branch 'develop' into develop
ravishanigarapu 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
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.wipro.fhir.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.wipro.fhir.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(); | ||
| } | ||
|
|
||
| } | ||
41 changes: 41 additions & 0 deletions
41
src/main/java/com/wipro/fhir/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,41 @@ | ||
| package com.wipro.fhir.utils.http; | ||
|
|
||
|
|
||
| import java.util.*; | ||
|
|
||
| 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.
π οΈ Refactor suggestion
The ThreadLocal implementation looks good but needs documentation
The implementation correctly uses ThreadLocal for thread-safe user agent storage, which is essential in a multi-threaded web application environment. The class provides appropriate methods for setting, getting, and clearing the user agent value.
Add JavaDoc comments to explain the purpose of this class and its methods, especially highlighting the importance of calling
clear()to prevent memory leaks in thread pools:π Committable suggestion
π€ Prompt for AI Agents