Skip to content

Commit ab24f74

Browse files
author
Luis Miranda
committed
Merge remote-tracking branch 'onelogin/v2.0.0' into Remove-core-servlet-dependency
2 parents ab2bda2 + e7ffe2f commit ab24f74

7 files changed

Lines changed: 149 additions & 5 deletions

File tree

core/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212

1313
<dependencies>
1414
<!-- for test -->
15+
<dependency>
16+
<groupId>org.hamcrest</groupId>
17+
<artifactId>hamcrest-core</artifactId>
18+
<scope>test</scope>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.hamcrest</groupId>
22+
<artifactId>hamcrest-library</artifactId>
23+
<scope>test</scope>
24+
</dependency>
1525
<dependency>
1626
<groupId>junit</groupId>
1727
<artifactId>junit</artifactId>

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.apache.commons.lang3.ObjectUtils;
1313
import org.joda.time.DateTime;
14+
import org.joda.time.Instant;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
1617
import org.w3c.dom.Document;
@@ -613,6 +614,29 @@ public String getSessionIndex() throws XPathExpressionException {
613614
return sessionIndex;
614615
}
615616

617+
/**
618+
* @return the ID of the assertion in the Response
619+
*/
620+
public String getAssertionId() throws XPathExpressionException {
621+
validateNumAssertions();
622+
final NodeList assertionNode = queryAssertion("");
623+
return assertionNode.item(0).getAttributes().getNamedItem("ID").getNodeValue();
624+
}
625+
626+
/**
627+
* @return a list of NotOnOrAfter values from SubjectConfirmationData nodes in this Response
628+
*/
629+
public List<Instant> getAssertionNotOnOrAfter() throws XPathExpressionException {
630+
final NodeList notOnOrAfterNodes = queryAssertion("/saml:Subject/saml:SubjectConfirmation/saml:SubjectConfirmationData");
631+
final ArrayList<Instant> notOnOrAfters = new ArrayList<>();
632+
for (int i = 0; i < notOnOrAfterNodes.getLength(); i++) {
633+
final Node notOnOrAfterAttribute = notOnOrAfterNodes.item(i).getAttributes().getNamedItem("NotOnOrAfter");
634+
if (notOnOrAfterAttribute != null) {
635+
notOnOrAfters.add(new Instant(notOnOrAfterAttribute.getNodeValue()));
636+
}}
637+
return notOnOrAfters;
638+
}
639+
616640
/**
617641
* Verifies that the document only contains a single Assertion (encrypted or not).
618642
*

core/src/test/java/com/onelogin/saml2/test/authn/AuthnResponseTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.hamcrest.CoreMatchers.containsString;
44
import static org.hamcrest.CoreMatchers.not;
5+
import static org.hamcrest.Matchers.contains;
56
import static org.junit.Assert.assertEquals;
67
import static org.junit.Assert.assertFalse;
78
import static org.junit.Assert.assertNull;
@@ -12,6 +13,7 @@
1213
import java.util.HashMap;
1314
import java.util.List;
1415

16+
import org.joda.time.Instant;
1517
import org.junit.Test;
1618
import org.w3c.dom.Document;
1719
import org.w3c.dom.Node;
@@ -437,6 +439,48 @@ public void testGetSessionIndex() throws Exception {
437439
assertEquals("_7164a9a9f97828bfdb8d0ebc004a05d2e7d873f70c", samlResponse.getSessionIndex());
438440
}
439441

442+
@Test
443+
public void testGetAssertionDetails() throws Exception {
444+
final SamlResponse samlResponse = new SamlResponse(
445+
new SettingsBuilder().fromFile("config/config.my.properties").build(),
446+
newHttpRequest(Util.getFileAsString("data/responses/response1.xml.base64"))
447+
);
448+
final List<Instant> notOnOrAfters = samlResponse.getAssertionNotOnOrAfter();
449+
450+
assertEquals("pfxa46574df-b3b0-a06a-23c8-636413198772", samlResponse.getAssertionId());
451+
assertThat(notOnOrAfters, contains(new Instant("2010-11-18T22:02:37Z")));
452+
453+
}
454+
455+
@Test
456+
public void testGetAssertionDetails_encrypted() throws Exception {
457+
final SamlResponse samlResponse = new SamlResponse(
458+
new SettingsBuilder().fromFile("config/config.my.properties").build(),
459+
newHttpRequest(Util.getFileAsString("data/responses/valid_encrypted_assertion.xml.base64"))
460+
);
461+
final List<Instant> notOnOrAfters = samlResponse.getAssertionNotOnOrAfter();
462+
463+
assertEquals("_519c2712648ee09a06d1f9a08e9e835715fea60267", samlResponse.getAssertionId());
464+
assertThat(notOnOrAfters, contains(new Instant("2055-06-07T20:17:08Z")));
465+
466+
}
467+
468+
@Test
469+
public void testGetAssertionDetails_multiple() throws Exception {
470+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();
471+
settings.setWantAssertionsSigned(false);
472+
settings.setWantMessagesSigned(true);
473+
474+
final SamlResponse samlResponse = new SamlResponse(
475+
settings,
476+
newHttpRequest(loadSignMessageAndEncode("data/responses/invalids/invalid_subjectconfirmation_multiple_issues.xml"))
477+
);
478+
final List<Instant> notOnOrAfters = samlResponse.getAssertionNotOnOrAfter();
479+
480+
assertEquals("pfx7841991c-c73f-4035-e2ee-c170c0e1d3e4", samlResponse.getAssertionId());
481+
assertThat(notOnOrAfters, contains(new Instant("2120-06-17T14:53:44Z"), new Instant("2010-06-17T14:53:44Z")));
482+
}
483+
440484
/**
441485
* Tests the getAttributes method of SamlResponse
442486
*

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636
<artifactId>mockito-core</artifactId>
3737
<version>1.10.19</version>
3838
</dependency>
39+
<dependency>
40+
<groupId>org.hamcrest</groupId>
41+
<artifactId>hamcrest-core</artifactId>
42+
<version>1.3</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.hamcrest</groupId>
46+
<artifactId>hamcrest-library</artifactId>
47+
<version>1.3</version>
48+
</dependency>
3949
<dependency>
4050
<groupId>org.slf4j</groupId>
4151
<artifactId>slf4j-api</artifactId>

toolkit/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
<type>test-jar</type>
2626
<scope>test</scope>
2727
</dependency>
28+
<dependency>
29+
<groupId>org.hamcrest</groupId>
30+
<artifactId>hamcrest-core</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.hamcrest</groupId>
35+
<artifactId>hamcrest-library</artifactId>
36+
<scope>test</scope>
37+
</dependency>
2838
<dependency>
2939
<groupId>junit</groupId>
3040
<artifactId>junit</artifactId>

toolkit/src/main/java/com/onelogin/saml2/Auth.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.apache.commons.lang3.StringUtils;
1919
import org.joda.time.DateTime;
20+
import org.joda.time.Instant;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
2223

@@ -66,7 +67,7 @@ public class Auth {
6667

6768
/**
6869
* NameID.
69-
*/
70+
*/
7071
private String nameid;
7172

7273
/**
@@ -79,6 +80,16 @@ public class Auth {
7980
*/
8081
private DateTime sessionExpiration;
8182

83+
/**
84+
* The ID of the last assertion processed
85+
*/
86+
private String lastAssertionId;
87+
88+
/**
89+
* The NotOnOrAfter values of the last assertion processed
90+
*/
91+
private List<Instant> lastAssertionNotOnOrAfter;
92+
8293
/**
8394
* User attributes data.
8495
*/
@@ -367,6 +378,8 @@ public void processResponse(String requestId) throws Exception {
367378
attributes = samlResponse.getAttributes();
368379
sessionIndex = samlResponse.getSessionIndex();
369380
sessionExpiration = samlResponse.getSessionNotOnOrAfter();
381+
lastAssertionId = samlResponse.getAssertionId();
382+
lastAssertionNotOnOrAfter = samlResponse.getAssertionNotOnOrAfter();
370383
LOGGER.debug("processResponse success --> " + samlResponseParameter);
371384
} else {
372385
errors.add("invalid_response");
@@ -537,9 +550,23 @@ public final DateTime getSessionExpiration()
537550
return sessionExpiration;
538551
}
539552

540-
/**
541-
* @return an array with the errors, the array is empty when the validation was successful
542-
*/
553+
/**
554+
* @return The ID of the last assertion processed
555+
*/
556+
public String getLastAssertionId() {
557+
return lastAssertionId;
558+
}
559+
560+
/**
561+
* @return The NotOnOrAfter values of the last assertion processed
562+
*/
563+
public List<Instant> getLastAssertionNotOnOrAfter() {
564+
return lastAssertionNotOnOrAfter;
565+
}
566+
567+
/**
568+
* @return an array with the errors, the array is empty when the validation was successful
569+
*/
543570
public List<String> getErrors()
544571
{
545572
return errors;

toolkit/src/test/java/com/onelogin/saml2/test/AuthTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
import static java.util.Collections.singletonMap;
55
import static org.hamcrest.CoreMatchers.containsString;
6+
import static org.hamcrest.CoreMatchers.is;
67
import static org.hamcrest.CoreMatchers.startsWith;
8+
import static org.hamcrest.Matchers.contains;
79
import static org.junit.Assert.assertEquals;
810
import static org.junit.Assert.assertFalse;
911
import static org.junit.Assert.assertNull;
@@ -25,6 +27,7 @@
2527
import javax.servlet.http.HttpServletResponse;
2628
import javax.servlet.http.HttpSession;
2729

30+
import org.joda.time.Instant;
2831
import org.junit.Test;
2932

3033
import com.onelogin.saml2.Auth;
@@ -226,8 +229,8 @@ public void testSetStrict() throws IOException, SettingsException, URISyntaxExce
226229
*/
227230
@Test
228231
public void testIsDebugActive() throws IOException, SettingsException, URISyntaxException {
229-
HttpServletRequest request = mock(HttpServletRequest.class);
230232
HttpServletResponse response = mock(HttpServletResponse.class);
233+
HttpServletRequest request = mock(HttpServletRequest.class);
231234
String samlResponseEncoded = Util.getFileAsString("data/responses/response1.xml.base64");
232235
when(request.getParameterMap()).thenReturn(singletonMap("SAMLResponse", new String[]{samlResponseEncoded}));
233236

@@ -805,6 +808,22 @@ public void testGetSessionIndex() throws Exception {
805808
assertEquals("_6273d77b8cde0c333ec79d22a9fa0003b9fe2d75cb", auth2.getSessionIndex());
806809
}
807810

811+
@Test
812+
public void testGetAssertionDetails() throws Exception {
813+
HttpServletResponse response = mock(HttpServletResponse.class);
814+
HttpServletRequest request = mock(HttpServletRequest.class);
815+
String samlResponseEncoded = Util.getFileAsString("data/responses/valid_response.xml.base64");
816+
when(request.getParameterMap()).thenReturn(singletonMap("SAMLResponse", new String[]{samlResponseEncoded}));
817+
when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/java-saml-jspsample/acs.jsp"));
818+
819+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();
820+
Auth auth = new Auth(settings, request, response);
821+
auth.processResponse();
822+
823+
assertThat(auth.getLastAssertionId(), is("pfxeac87197-11cb-ec12-c181-ae739b54debe"));
824+
assertThat(auth.getLastAssertionNotOnOrAfter(), contains(new Instant("2023-08-23T06:57:01Z")));
825+
}
826+
808827
/**
809828
* Tests the getSessionExpiration method of Auth
810829
*

0 commit comments

Comments
 (0)