Skip to content

Commit 5d5fbb3

Browse files
authored
Merge pull request #77 from onelogin/servletless
Merge Servletless branch
2 parents 96a2213 + 0e69c67 commit 5d5fbb3

27 files changed

Lines changed: 1349 additions & 989 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ jdk:
44
- oraclejdk7
55
install: true
66
after_success:
7-
- mvn clean test jacoco:report coveralls:report
7+
- mvn clean test org.jacoco:jacoco-maven-plugin:report org.eluder.coveralls:coveralls-maven-plugin:report

core/.gitignore

Lines changed: 0 additions & 6 deletions
This file was deleted.

core/pom.xml

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,42 @@
88

99
<packaging>jar</packaging>
1010
<name>OneLogin java-saml Toolkit Core</name>
11-
<artifactId>java-saml</artifactId>
11+
<artifactId>java-saml-core</artifactId>
1212

1313
<dependencies>
1414
<!-- for test -->
1515
<dependency>
1616
<groupId>org.hamcrest</groupId>
1717
<artifactId>hamcrest-core</artifactId>
18-
<version>1.3</version>
1918
<scope>test</scope>
2019
</dependency>
2120
<dependency>
2221
<groupId>org.hamcrest</groupId>
2322
<artifactId>hamcrest-library</artifactId>
24-
<version>1.3</version>
2523
<scope>test</scope>
2624
</dependency>
2725
<dependency>
2826
<groupId>junit</groupId>
2927
<artifactId>junit</artifactId>
30-
<version>${junitVersion}</version>
3128
<scope>test</scope>
3229
</dependency>
3330
<dependency>
3431
<groupId>org.mockito</groupId>
3532
<artifactId>mockito-core</artifactId>
36-
<version>1.10.19</version>
3733
<scope>test</scope>
3834
</dependency>
3935

4036
<!-- for log -->
4137
<dependency>
4238
<groupId>org.slf4j</groupId>
4339
<artifactId>slf4j-api</artifactId>
44-
<version>${slf4jVersion}</version>
4540
</dependency>
4641
<dependency>
4742
<groupId>ch.qos.logback</groupId>
4843
<artifactId>logback-classic</artifactId>
49-
<version>${logbackVersion}</version>
5044
<scope>test</scope>
5145
</dependency>
5246

53-
<!-- httprequest and httpresponse -->
54-
<dependency>
55-
<groupId>javax.servlet</groupId>
56-
<artifactId>servlet-api</artifactId>
57-
<version>2.5</version>
58-
<scope>provided</scope>
59-
</dependency>
60-
6147
<!-- date and time library for Java -->
6248
<dependency>
6349
<groupId>joda-time</groupId>
@@ -101,6 +87,17 @@
10187
</execution>
10288
</executions>
10389
</plugin>
90+
<plugin>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-jar-plugin</artifactId>
93+
<executions>
94+
<execution>
95+
<goals>
96+
<goal>test-jar</goal>
97+
</goals>
98+
</execution>
99+
</executions>
100+
</plugin>
104101
<plugin>
105102
<groupId>org.apache.maven.plugins</groupId>
106103
<artifactId>maven-surefire-plugin</artifactId>
@@ -111,8 +108,9 @@
111108
</plugin>
112109
</plugins>
113110
</build>
114-
111+
<!--
115112
<properties>
116113
<jacoco.agent.argLine />
117114
</properties>
115+
-->
118116
</project>

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
import java.util.List;
88
import java.util.Map;
99

10-
import javax.servlet.http.HttpServletRequest;
1110
import javax.xml.xpath.XPathExpressionException;
1211

13-
import com.onelogin.saml2.model.SubjectConfirmationIssue;
1412
import org.apache.commons.lang3.ObjectUtils;
1513
import org.joda.time.DateTime;
1614
import org.joda.time.Instant;
@@ -22,8 +20,10 @@
2220
import org.w3c.dom.Node;
2321
import org.w3c.dom.NodeList;
2422

25-
import com.onelogin.saml2.settings.Saml2Settings;
23+
import com.onelogin.saml2.http.HttpRequest;
2624
import com.onelogin.saml2.model.SamlResponseStatus;
25+
import com.onelogin.saml2.model.SubjectConfirmationIssue;
26+
import com.onelogin.saml2.settings.Saml2Settings;
2727
import com.onelogin.saml2.util.Constants;
2828
import com.onelogin.saml2.util.SchemaFactory;
2929
import com.onelogin.saml2.util.Util;
@@ -82,15 +82,15 @@ public class SamlResponse {
8282
* @param settings
8383
* Saml2Settings object. Setting data
8484
* @param request
85-
* HttpServletRequest object to be processed (Contains GET and POST parameters, session, ...).
85+
* the HttpRequest object to be processed (Contains GET and POST parameters, request URL, ...).
8686
*
87-
* @throws Exception
87+
* @throws Exception
8888
*/
89-
public SamlResponse(Saml2Settings settings, HttpServletRequest request) throws Exception {
89+
public SamlResponse(Saml2Settings settings, HttpRequest request) throws Exception {
9090
this.settings = settings;
9191

92-
if (request != null) {
93-
currentUrl = request.getRequestURL().toString();
92+
if (request != null) {
93+
currentUrl = request.getRequestURL();
9494
loadXmlFromBase64(request.getParameter("SAMLResponse"));
9595
}
9696
}
@@ -616,6 +616,8 @@ public String getSessionIndex() throws XPathExpressionException {
616616

617617
/**
618618
* @return the ID of the assertion in the Response
619+
* @throws XPathExpressionException
620+
*
619621
*/
620622
public String getAssertionId() throws XPathExpressionException {
621623
validateNumAssertions();
@@ -625,6 +627,8 @@ public String getAssertionId() throws XPathExpressionException {
625627

626628
/**
627629
* @return a list of NotOnOrAfter values from SubjectConfirmationData nodes in this Response
630+
* @throws XPathExpressionException
631+
*
628632
*/
629633
public List<Instant> getAssertionNotOnOrAfter() throws XPathExpressionException {
630634
final NodeList notOnOrAfterNodes = queryAssertion("/saml:Subject/saml:SubjectConfirmation/saml:SubjectConfirmationData");
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package com.onelogin.saml2.http;
2+
3+
import static com.onelogin.saml2.util.Preconditions.checkNotNull;
4+
import static java.util.Collections.unmodifiableList;
5+
import static java.util.Collections.unmodifiableMap;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.Objects;
13+
14+
/**
15+
* Framework-agnostic representation of an HTTP request.
16+
*
17+
* @since 2.0.0
18+
*/
19+
public final class HttpRequest {
20+
private final String requestURL;
21+
private final Map<String, List<String>> parameters;
22+
23+
/**
24+
* Creates a new HttpRequest.
25+
*
26+
* @param requestURL the request URL (up to but not including query parameters)
27+
* @throws NullPointerException if requestURL is null
28+
*/
29+
public HttpRequest(String requestURL) {
30+
this(requestURL, Collections.<String, List<String>>emptyMap());
31+
}
32+
33+
/**
34+
* Creates a new HttpRequest.
35+
*
36+
* @param requestURL the request URL (up to but not including query parameters)
37+
* @param parameters the request query parameters
38+
* @throws NullPointerException if any of the parameters is null
39+
*/
40+
public HttpRequest(String requestURL, Map<String, List<String>> parameters) {
41+
this.requestURL = checkNotNull(requestURL, "requestURL");
42+
this.parameters = unmodifiableCopyOf(checkNotNull(parameters, "queryParams"));
43+
}
44+
45+
/**
46+
* @param name the query parameter name
47+
* @param value the query parameter value
48+
* @return a new HttpRequest with the given query parameter added
49+
* @throws NullPointerException if any of the parameters is null
50+
*/
51+
public HttpRequest addParameter(String name, String value) {
52+
checkNotNull(name, "name");
53+
checkNotNull(value, "value");
54+
55+
final List<String> oldValues = parameters.containsKey(name) ? parameters.get(name) : new ArrayList<String>();
56+
final List<String> newValues = new ArrayList<>(oldValues);
57+
newValues.add(value);
58+
final Map<String, List<String>> params = new HashMap<>(parameters);
59+
params.put(name, newValues);
60+
61+
return new HttpRequest(requestURL, params);
62+
}
63+
64+
/**
65+
* @param name the query parameter name
66+
* @return a new HttpRequest with the given query parameter removed
67+
* @throws NullPointerException if any of the parameters is null
68+
*/
69+
public HttpRequest removeParameter(String name) {
70+
checkNotNull(name, "name");
71+
72+
final Map<String, List<String>> params = new HashMap<>(parameters);
73+
params.remove(name);
74+
75+
return new HttpRequest(requestURL, params);
76+
}
77+
78+
/**
79+
* The URL the client used to make the request. Includes a protocol, server name, port number, and server path, but
80+
* not the query string parameters.
81+
*
82+
* @return the request URL
83+
*/
84+
public String getRequestURL() {
85+
return requestURL;
86+
}
87+
88+
/**
89+
* @param name the query parameter name
90+
* @return the first value for the parameter, or null
91+
*/
92+
public String getParameter(String name) {
93+
List<String> values = getParameters(name);
94+
return values.isEmpty() ? null : values.get(0);
95+
}
96+
97+
/**
98+
* @param name the query parameter name
99+
* @return a List containing all values for the parameter
100+
*/
101+
public List<String> getParameters(String name) {
102+
List<String> values = parameters.get(name);
103+
return values != null ? values : Collections.<String>emptyList();
104+
}
105+
106+
/**
107+
* @return a map of all query parameters
108+
*/
109+
public Map<String, List<String>> getParameters() {
110+
return parameters;
111+
}
112+
113+
@Override
114+
public boolean equals(Object o) {
115+
if (this == o) return true;
116+
if (o == null || getClass() != o.getClass()) return false;
117+
HttpRequest that = (HttpRequest) o;
118+
return Objects.equals(requestURL, that.requestURL) &&
119+
Objects.equals(parameters, that.parameters);
120+
}
121+
122+
@Override
123+
public int hashCode() {
124+
return Objects.hash(requestURL, parameters);
125+
}
126+
127+
@Override
128+
public String toString() {
129+
return "HttpRequest{" +
130+
"requestURL='" + requestURL + '\'' +
131+
", parameters=" + parameters +
132+
'}';
133+
}
134+
135+
private static Map<String, List<String>> unmodifiableCopyOf(Map<String, List<String>> orig) {
136+
Map<String, List<String>> copy = new HashMap<>();
137+
for (Map.Entry<String, List<String>> entry : orig.entrySet()) {
138+
copy.put(entry.getKey(), unmodifiableList(new ArrayList<>(entry.getValue())));
139+
}
140+
141+
return unmodifiableMap(copy);
142+
}
143+
}

core/src/main/java/com/onelogin/saml2/logout/LogoutRequest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.util.List;
1111
import java.util.Map;
1212

13-
import javax.servlet.http.HttpServletRequest;
1413
import javax.xml.xpath.XPathExpressionException;
1514

1615
import org.apache.commons.lang3.text.StrSubstitutor;
@@ -22,6 +21,7 @@
2221
import org.w3c.dom.NodeList;
2322

2423
import com.onelogin.saml2.exception.XMLEntityException;
24+
import com.onelogin.saml2.http.HttpRequest;
2525
import com.onelogin.saml2.settings.Saml2Settings;
2626
import com.onelogin.saml2.util.Util;
2727
import com.onelogin.saml2.util.Constants;
@@ -54,9 +54,9 @@ public class LogoutRequest {
5454
private final Saml2Settings settings;
5555

5656
/**
57-
* HttpServletRequest object to be processed (Contains GET and POST parameters, session, ...).
57+
* HttpRequest object to be processed (Contains GET and POST parameters, request URL, ...).
5858
*/
59-
private HttpServletRequest request;
59+
private final HttpRequest request;
6060

6161
/**
6262
* NameID.
@@ -89,15 +89,15 @@ public class LogoutRequest {
8989
* @param settings
9090
* OneLogin_Saml2_Settings
9191
* @param request
92-
* HttpServletRequest object to be processed (Contains GET and POST parameters, session, ...).
92+
* the HttpRequest object to be processed (Contains GET and POST parameters, request URL, ...).
9393
* @param nameId
9494
* The NameID that will be set in the LogoutRequest.
9595
* @param sessionIndex
9696
* The SessionIndex (taken from the SAML Response in the SSO process).
9797
*
9898
* @throws XMLEntityException
9999
*/
100-
public LogoutRequest(Saml2Settings settings, HttpServletRequest request, String nameId, String sessionIndex) throws XMLEntityException {
100+
public LogoutRequest(Saml2Settings settings, HttpRequest request, String nameId, String sessionIndex) throws XMLEntityException {
101101
this.settings = settings;
102102
this.request = request;
103103

@@ -140,11 +140,11 @@ public LogoutRequest(Saml2Settings settings) throws XMLEntityException {
140140
* @param settings
141141
* OneLogin_Saml2_Settings
142142
* @param request
143-
* HttpServletRequest object to be processed (Contains GET and POST parameters, session, ...).
143+
* the HttpRequest object to be processed (Contains GET and POST parameters, request URL, ...).
144144
*
145145
* @throws XMLEntityException
146146
*/
147-
public LogoutRequest(Saml2Settings settings, HttpServletRequest request) throws XMLEntityException {
147+
public LogoutRequest(Saml2Settings settings, HttpRequest request) throws XMLEntityException {
148148
this(settings, request, null, null);
149149
}
150150

@@ -246,7 +246,7 @@ public Boolean isValid() throws XMLEntityException {
246246
}
247247

248248
if (this.request == null) {
249-
throw new Exception("The HttpServletRequest of the current host was not established");
249+
throw new Exception("The HttpRequest of the current host was not established");
250250
}
251251

252252
if (this.currentUrl == null || this.currentUrl.isEmpty()) {

0 commit comments

Comments
 (0)