From 8f68847280f036a3a167d673f8e9895e416f64bc Mon Sep 17 00:00:00 2001 From: MOHANKUMAR T Date: Fri, 2 Jun 2023 00:05:24 +0530 Subject: [PATCH] Fix. OpenMRSLoginAuthenticator to read JESSIONID from cookie --- .../client/openmrs/OpenMRSLoginAuthenticator.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/bahmni/src/main/java/org/avni_integration_service/bahmni/client/openmrs/OpenMRSLoginAuthenticator.java b/bahmni/src/main/java/org/avni_integration_service/bahmni/client/openmrs/OpenMRSLoginAuthenticator.java index 3990cfb9..c3a8e671 100644 --- a/bahmni/src/main/java/org/avni_integration_service/bahmni/client/openmrs/OpenMRSLoginAuthenticator.java +++ b/bahmni/src/main/java/org/avni_integration_service/bahmni/client/openmrs/OpenMRSLoginAuthenticator.java @@ -23,6 +23,8 @@ import java.io.InputStream; import java.net.URI; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class OpenMRSLoginAuthenticator implements Authenticator { private static final Logger logger = Logger.getLogger(OpenMRSLoginAuthenticator.class); @@ -76,7 +78,7 @@ public HttpRequestDetails refreshRequestDetails(URI uri) { confirmAuthenticated(openMRSResponse); ClientCookies clientCookies = new ClientCookies(); - clientCookies.put(SESSION_ID_KEY, openMRSResponse.getSessionId()); + clientCookies.put(SESSION_ID_KEY, ExtractStringUsingRegex(response.getHeaders("Set-Cookie")[0].getValue())); previousSuccessfulRequest = new HttpRequestDetails(uri, clientCookies, new HttpHeaders()); return previousSuccessfulRequest; @@ -88,6 +90,14 @@ public HttpRequestDetails refreshRequestDetails(URI uri) { } } + private String ExtractStringUsingRegex(String Cookie){ + if (Cookie == null) return null; + Pattern pattern = Pattern.compile("\\bJSESSIONID=([A-Z0-9]{32})"); + Matcher matcher = pattern.matcher(Cookie); + if (matcher.find()) return matcher.group(1); + throw new WebClientsException("No Matching SessionID in the Response Cookie"); + } + protected void setCredentials(HttpGet httpGet) throws AuthenticationException { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(authenticationDetails.getUserId(), authenticationDetails.getPassword()); BasicScheme scheme = new BasicScheme();