Skip to content

Commit c6b12ba

Browse files
committed
Refactored out Servlet dependencies from core and toolkit
- Introduced `servlet-jakarta` and `servlet-javax` - Teased apart HTTP request and HTTP response objects along a common seam - Bumped version to 3.0.0
1 parent 5a58410 commit c6b12ba

File tree

32 files changed

+1401
-1270
lines changed

32 files changed

+1401
-1270
lines changed

core/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<groupId>com.onelogin</groupId>
55
<artifactId>java-saml-toolkit</artifactId>
6-
<version>2.9.1-SNAPSHOT</version>
6+
<version>3.0.0-SNAPSHOT</version>
77
</parent>
88

99
<packaging>jar</packaging>

core/src/main/java/com/onelogin/saml2/authn/SamlResponse.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import javax.xml.parsers.ParserConfigurationException;
1616
import javax.xml.xpath.XPathExpressionException;
1717

18+
import com.onelogin.saml2.http.HttpRequest;
1819
import com.onelogin.saml2.model.hsm.HSM;
1920

2021
import org.apache.commons.lang3.StringUtils;
@@ -28,7 +29,6 @@
2829
import org.xml.sax.SAXException;
2930
import com.onelogin.saml2.exception.SettingsException;
3031
import com.onelogin.saml2.exception.ValidationError;
31-
import com.onelogin.saml2.http.HttpRequest;
3232
import com.onelogin.saml2.model.SamlResponseStatus;
3333
import com.onelogin.saml2.model.SubjectConfirmationIssue;
3434
import com.onelogin.saml2.settings.Saml2Settings;
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,56 @@
11
package com.onelogin.saml2.http;
22

3-
import static com.onelogin.saml2.util.Preconditions.checkNotNull;
4-
import static java.util.Collections.unmodifiableList;
5-
import static java.util.Collections.unmodifiableMap;
3+
import com.onelogin.saml2.util.Util;
64

7-
import java.util.ArrayList;
8-
import java.util.Collections;
5+
import java.util.Arrays;
96
import java.util.HashMap;
107
import java.util.List;
118
import java.util.Map;
12-
import java.util.Objects;
139
import java.util.regex.Matcher;
1410
import java.util.regex.Pattern;
1511

16-
import org.apache.commons.lang3.StringUtils;
17-
18-
import com.onelogin.saml2.util.Util;
19-
2012
/**
21-
* Framework-agnostic representation of an HTTP request.
13+
* Framework-agnostic definition of an HTTP request with a very minimal set of
14+
* methods needed to support the SAML handshake.
2215
*
23-
* @since 2.0.0
16+
* @since 3.0.0
2417
*/
25-
public final class HttpRequest {
18+
public interface HttpRequest {
2619

27-
public static final Map<String, List<String>> EMPTY_PARAMETERS = Collections.<String, List<String>>emptyMap();
20+
int getServerPort();
2821

29-
private final String requestURL;
30-
private final Map<String, List<String>> parameters;
31-
private final String queryString;
22+
String getScheme();
3223

33-
/**
34-
* Creates a new HttpRequest.
35-
*
36-
* @param requestURL the request URL (up to but not including query parameters)
37-
* @throws NullPointerException if requestURL is null
38-
* @deprecated Not providing a queryString can cause HTTP Redirect binding to fail.
39-
*/
40-
@Deprecated
41-
public HttpRequest(String requestURL) {
42-
this(requestURL, EMPTY_PARAMETERS);
43-
}
24+
String getServerName();
4425

45-
/**
46-
* Creates a new HttpRequest.
47-
*
48-
* @param requestURL the request URL (up to but not including query parameters)
49-
* @param queryString string that is contained in the request URL after the path
50-
*/
51-
public HttpRequest(String requestURL, String queryString) {
52-
this(requestURL, EMPTY_PARAMETERS, queryString);
53-
}
26+
String getRequestURL();
5427

55-
/**
56-
* Creates a new HttpRequest.
57-
*
58-
* @param requestURL the request URL (up to but not including query parameters)
59-
* @param parameters the request query parameters
60-
* @throws NullPointerException if any of the parameters is null
61-
* @deprecated Not providing a queryString can cause HTTP Redirect binding to fail.
62-
*/
63-
@Deprecated
64-
public HttpRequest(String requestURL, Map<String, List<String>> parameters) {
65-
this(requestURL, parameters, null);
66-
}
28+
String getRequestURI();
6729

68-
/**
69-
* Creates a new HttpRequest.
70-
*
71-
* @param requestURL the request URL (up to but not including query parameters)
72-
* @param parameters the request query parameters
73-
* @param queryString string that is contained in the request URL after the path
74-
* @throws NullPointerException if any of the parameters is null
75-
*/
76-
public HttpRequest(String requestURL, Map<String, List<String>> parameters, String queryString) {
77-
this.requestURL = checkNotNull(requestURL, "requestURL");
78-
this.parameters = unmodifiableCopyOf(checkNotNull(parameters, "queryParams"));
79-
this.queryString = StringUtils.trimToEmpty(queryString);
80-
}
81-
82-
/**
83-
* @param name the query parameter name
84-
* @param value the query parameter value
85-
* @return a new HttpRequest with the given query parameter added
86-
* @throws NullPointerException if any of the parameters is null
87-
*/
88-
public HttpRequest addParameter(String name, String value) {
89-
checkNotNull(name, "name");
90-
checkNotNull(value, "value");
30+
String getQueryString();
9131

92-
final List<String> oldValues = parameters.containsKey(name) ? parameters.get(name) : new ArrayList<String>();
93-
final List<String> newValues = new ArrayList<>(oldValues);
94-
newValues.add(value);
95-
final Map<String, List<String>> params = new HashMap<>(parameters);
96-
params.put(name, newValues);
97-
98-
return new HttpRequest(requestURL, params, queryString);
99-
}
32+
void invalidateSession();
10033

101-
/**
102-
* @param name the query parameter name
103-
* @return a new HttpRequest with the given query parameter removed
104-
* @throws NullPointerException if any of the parameters is null
105-
*/
106-
public HttpRequest removeParameter(String name) {
107-
checkNotNull(name, "name");
34+
Map<String, String[]> getParameterMap();
10835

109-
final Map<String, List<String>> params = new HashMap<>(parameters);
110-
params.remove(name);
36+
default List<String> getParameters(String name) {
37+
final Map<String, String[]> paramsAsArray = getParameterMap();
38+
final Map<String, List<String>> paramsAsList = new HashMap<>();
39+
for (Map.Entry<String, String[]> param : paramsAsArray.entrySet()) {
40+
paramsAsList.put(param.getKey(), Arrays.asList(param.getValue()));
41+
}
11142

112-
return new HttpRequest(requestURL, params, queryString);
113-
}
114-
115-
/**
116-
* The URL the client used to make the request. Includes a protocol, server name, port number, and server path, but
117-
* not the query string parameters.
118-
*
119-
* @return the request URL
120-
*/
121-
public String getRequestURL() {
122-
return requestURL;
43+
return paramsAsList.get(name);
12344
}
12445

125-
/**
126-
* @param name the query parameter name
127-
* @return the first value for the parameter, or null
128-
*/
129-
public String getParameter(String name) {
46+
default String getParameter(String name) {
13047
List<String> values = getParameters(name);
13148
return values.isEmpty() ? null : values.get(0);
13249
}
13350

134-
/**
135-
* @param name the query parameter name
136-
* @return a List containing all values for the parameter
137-
*/
138-
public List<String> getParameters(String name) {
139-
List<String> values = parameters.get(name);
140-
return values != null ? values : Collections.<String>emptyList();
141-
}
142-
143-
/**
144-
* @return a map of all query parameters
145-
*/
146-
public Map<String, List<String>> getParameters() {
147-
return parameters;
51+
default String getEncodedParameter(String name, String defaultValue) {
52+
String value = getEncodedParameter(name);
53+
return (value != null) ? value : Util.urlEncoder(defaultValue);
14854
}
14955

15056
/**
@@ -155,7 +61,8 @@ public Map<String, List<String>> getParameters() {
15561
* @param name
15662
* @return the first value for the parameter, or null
15763
*/
158-
public String getEncodedParameter(String name) {
64+
default String getEncodedParameter(String name) {
65+
String queryString = getQueryString();
15966
Matcher matcher = Pattern.compile(Pattern.quote(name) + "=([^&#]+)").matcher(queryString);
16067
if (matcher.find()) {
16168
return matcher.group(1);
@@ -164,58 +71,4 @@ public String getEncodedParameter(String name) {
16471
}
16572
}
16673

167-
/**
168-
* Return an url encoded get parameter value
169-
* Prefer to extract the original encoded value directly from queryString since url
170-
* encoding is not canonical.
171-
*
172-
* @param name
173-
* @param defaultValue
174-
* @return the first value for the parameter, or url encoded default value
175-
*/
176-
public String getEncodedParameter(String name, String defaultValue) {
177-
String value = getEncodedParameter(name);
178-
return (value != null ? value : Util.urlEncoder(defaultValue));
179-
}
180-
181-
@Override
182-
public boolean equals(Object o) {
183-
if (this == o) {
184-
return true;
185-
}
186-
187-
if (o == null || getClass() != o.getClass()) {
188-
return false;
189-
}
190-
191-
HttpRequest that = (HttpRequest) o;
192-
return Objects.equals(requestURL, that.requestURL) &&
193-
Objects.equals(parameters, that.parameters) &&
194-
Objects.equals(queryString, that.queryString);
195-
}
196-
197-
@Override
198-
public int hashCode() {
199-
return Objects.hash(requestURL, parameters, queryString);
200-
}
201-
202-
@Override
203-
public String toString() {
204-
return "HttpRequest{" +
205-
"requestURL='" + requestURL + '\'' +
206-
", parameters=" + parameters +
207-
", queryString=" + queryString +
208-
'}';
209-
}
210-
211-
private static Map<String, List<String>> unmodifiableCopyOf(Map<String, List<String>> orig) {
212-
Map<String, List<String>> copy = new HashMap<>();
213-
for (Map.Entry<String, List<String>> entry : orig.entrySet()) {
214-
copy.put(entry.getKey(), unmodifiableList(new ArrayList<>(entry.getValue())));
215-
}
216-
217-
return unmodifiableMap(copy);
218-
}
219-
220-
22174
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.onelogin.saml2.http;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
public class HttpRequestUtils {
6+
7+
private HttpRequestUtils() {
8+
}
9+
10+
/**
11+
* Returns the protocol + the current host + the port (if different than
12+
* common ports).
13+
*
14+
* @param request
15+
* HttpServletRequest object to be processed
16+
*
17+
* @return the HOST URL
18+
*/
19+
public static String getSelfURLhost(HttpRequest request) {
20+
String hostUrl = StringUtils.EMPTY;
21+
final int serverPort = request.getServerPort();
22+
if ((serverPort == 80) || (serverPort == 443) || serverPort == 0) {
23+
hostUrl = String.format("%s://%s", request.getScheme(), request.getServerName());
24+
} else {
25+
hostUrl = String.format("%s://%s:%s", request.getScheme(), request.getServerName(), serverPort);
26+
}
27+
return hostUrl;
28+
}
29+
30+
/**
31+
* Returns the URL of the current context + current view + query
32+
*
33+
* @param request
34+
* HttpServletRequest object to be processed
35+
*
36+
* @return current context + current view + query
37+
*/
38+
public static String getSelfURL(HttpRequest request) {
39+
String url = getSelfURLhost(request);
40+
41+
String requestUri = request.getRequestURI();
42+
String queryString = request.getQueryString();
43+
44+
if (null != requestUri && !requestUri.isEmpty()) {
45+
url += requestUri;
46+
}
47+
48+
if (null != queryString && !queryString.isEmpty()) {
49+
url += '?' + queryString;
50+
}
51+
return url;
52+
}
53+
54+
/**
55+
* Returns the URL of the current host + current view.
56+
*
57+
* @param request
58+
* HttpServletRequest object to be processed
59+
*
60+
* @return current host + current view
61+
*/
62+
public static String getSelfURLNoQuery(HttpRequest request) {
63+
return request.getRequestURL();
64+
}
65+
66+
/**
67+
* Returns the routed URL of the current host + current view.
68+
*
69+
* @param request
70+
* HttpServletRequest object to be processed
71+
*
72+
* @return the current routed url
73+
*/
74+
public static String getSelfRoutedURLNoQuery(HttpRequest request) {
75+
String url = getSelfURLhost(request);
76+
String requestUri = request.getRequestURI();
77+
if (null != requestUri && !requestUri.isEmpty()) {
78+
url += requestUri;
79+
}
80+
return url;
81+
}
82+
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.onelogin.saml2.http;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* Framework-agnostic definition of an HTTP response with a very minimal set of
7+
* methods needed to support the SAML handshake.
8+
*
9+
* @since 3.0.0
10+
*/
11+
public interface HttpResponse {
12+
13+
void sendRedirect(String location) throws IOException;
14+
15+
}

0 commit comments

Comments
 (0)