Skip to content
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

Fix. OpenMRSLoginAuthenticator to read JESESSIONID from cookie #69

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down