Skip to content

Commit 3b45261

Browse files
author
Luis Miranda
committed
added java-saml-core module, left servlet dependencies in java-saml
updated SamlResponse to take in the requestURL and SAMLResponse parameter directly. the new module does not depend on javax.servlet.*, meaning it can more easily be reused together with different server stacks. improved Util.getFileAsString() to work for classpath resources that are in JARs as well (previously only on the filesystem worked).
1 parent 9e7a25c commit 3b45261

22 files changed

Lines changed: 608 additions & 500 deletions

File tree

core/.gitignore

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

core/pom.xml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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 -->
@@ -34,14 +34,6 @@
3434
<scope>test</scope>
3535
</dependency>
3636

37-
<!-- httprequest and httpresponse -->
38-
<dependency>
39-
<groupId>javax.servlet</groupId>
40-
<artifactId>servlet-api</artifactId>
41-
<version>2.5</version>
42-
<scope>provided</scope>
43-
</dependency>
44-
4537
<!-- date and time library for Java -->
4638
<dependency>
4739
<groupId>joda-time</groupId>
@@ -85,6 +77,17 @@
8577
</execution>
8678
</executions>
8779
</plugin>
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-jar-plugin</artifactId>
83+
<executions>
84+
<execution>
85+
<goals>
86+
<goal>test-jar</goal>
87+
</goals>
88+
</execution>
89+
</executions>
90+
</plugin>
8891
<plugin>
8992
<groupId>org.apache.maven.plugins</groupId>
9093
<artifactId>maven-surefire-plugin</artifactId>

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

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public boolean isValid(String requestId) {
184184
if (requestId != null && !ObjectUtils.equals(responseInResponseTo, requestId)) {
185185
throw new Exception("The InResponseTo of the Response: " + responseInResponseTo
186186
+ ", does not match the ID of the AuthNRequest sent by the SP: " + requestId);
187-
}
187+
}
188188

189189
if (!this.encrypted && settings.getWantAssertionsEncrypted()) {
190190
throw new Exception("The assertion of the Response is not encrypted and the SP requires it");
@@ -286,68 +286,68 @@ public boolean isValid(String requestId) {
286286
}
287287
}
288288

289-
// Check SubjectConfirmation, at least one SubjectConfirmation must be valid
289+
// Check SubjectConfirmation, at least one SubjectConfirmation must be valid
290290
private void validateSubjectConfirmation(String responseInResponseTo) throws Exception {
291291
final List<SubjectConfirmationIssue> validationIssues = new ArrayList<>();
292-
boolean validSubjectConfirmation = false;
293-
NodeList subjectConfirmationNodes = this.queryAssertion("/saml:Subject/saml:SubjectConfirmation");
294-
for (int i = 0; i < subjectConfirmationNodes.getLength(); i++) {
295-
Node scn = subjectConfirmationNodes.item(i);
296-
297-
Node method = scn.getAttributes().getNamedItem("Method");
298-
if (method != null && !method.getNodeValue().equals(Constants.CM_BEARER)) {
299-
continue;
300-
}
292+
boolean validSubjectConfirmation = false;
293+
NodeList subjectConfirmationNodes = this.queryAssertion("/saml:Subject/saml:SubjectConfirmation");
294+
for (int i = 0; i < subjectConfirmationNodes.getLength(); i++) {
295+
Node scn = subjectConfirmationNodes.item(i);
296+
297+
Node method = scn.getAttributes().getNamedItem("Method");
298+
if (method != null && !method.getNodeValue().equals(Constants.CM_BEARER)) {
299+
continue;
300+
}
301301

302-
NodeList subjectConfirmationDataNodes = scn.getChildNodes();
303-
for (int c = 0; c < subjectConfirmationDataNodes.getLength(); c++) {
304-
if (subjectConfirmationDataNodes.item(c).getLocalName() != null && subjectConfirmationDataNodes.item(c).getLocalName().equals("SubjectConfirmationData")) {
302+
NodeList subjectConfirmationDataNodes = scn.getChildNodes();
303+
for (int c = 0; c < subjectConfirmationDataNodes.getLength(); c++) {
304+
if (subjectConfirmationDataNodes.item(c).getLocalName() != null && subjectConfirmationDataNodes.item(c).getLocalName().equals("SubjectConfirmationData")) {
305305

306-
Node recipient = subjectConfirmationDataNodes.item(c).getAttributes().getNamedItem("Recipient");
306+
Node recipient = subjectConfirmationDataNodes.item(c).getAttributes().getNamedItem("Recipient");
307307
if (recipient == null) {
308308
validationIssues.add(new SubjectConfirmationIssue(i, "SubjectConfirmationData doesn't contain a Recipient"));
309309
continue;
310310
}
311311

312312
if (!recipient.getNodeValue().equals(currentUrl)) {
313313
validationIssues.add(new SubjectConfirmationIssue(i, "SubjectConfirmationData doesn't match a valid Recipient"));
314-
continue;
315-
}
314+
continue;
315+
}
316316

317-
Node inResponseTo = subjectConfirmationDataNodes.item(c).getAttributes().getNamedItem("InResponseTo");
317+
Node inResponseTo = subjectConfirmationDataNodes.item(c).getAttributes().getNamedItem("InResponseTo");
318318
if (inResponseTo == null && responseInResponseTo != null ||
319319
inResponseTo != null && !inResponseTo.getNodeValue().equals(responseInResponseTo)) {
320320
validationIssues.add(new SubjectConfirmationIssue(i, "SubjectConfirmationData has an invalid InResponseTo value"));;
321-
continue;
322-
}
321+
continue;
322+
}
323323

324-
325-
Node notOnOrAfter = subjectConfirmationDataNodes.item(c).getAttributes().getNamedItem("NotOnOrAfter");
324+
325+
Node notOnOrAfter = subjectConfirmationDataNodes.item(c).getAttributes().getNamedItem("NotOnOrAfter");
326326
if (notOnOrAfter == null) {
327327
validationIssues.add(new SubjectConfirmationIssue(i, "SubjectConfirmationData doesn't contain a NotOnOrAfter attribute"));
328328
continue;
329329
}
330330

331-
DateTime noa = Util.parseDateTime(notOnOrAfter.getNodeValue());
332-
if (noa.isEqualNow() || noa.isBeforeNow()) {
331+
DateTime noa = Util.parseDateTime(notOnOrAfter.getNodeValue());
332+
if (noa.isEqualNow() || noa.isBeforeNow()) {
333333
validationIssues.add(new SubjectConfirmationIssue(i, "SubjectConfirmationData is no longer valid"));
334-
continue;
335-
}
334+
continue;
335+
}
336336

337-
Node notBefore = subjectConfirmationDataNodes.item(c).getAttributes().getNamedItem("NotBefore");
338-
if (notBefore != null) {
339-
DateTime nb = Util.parseDateTime(notBefore.getNodeValue());
340-
if (nb.isAfterNow()) {
337+
Node notBefore = subjectConfirmationDataNodes.item(c).getAttributes().getNamedItem("NotBefore");
338+
if (notBefore != null) {
339+
DateTime nb = Util.parseDateTime(notBefore.getNodeValue());
340+
if (nb.isAfterNow()) {
341341
validationIssues.add(new SubjectConfirmationIssue(i, "SubjectConfirmationData is not yet valid"));
342-
continue;
343-
}
344-
}
345-
validSubjectConfirmation = true;
342+
continue;
346343
}
347344
}
345+
validSubjectConfirmation = true;
348346
}
347+
}
348+
}
349349

350-
if (!validSubjectConfirmation) {
350+
if (!validSubjectConfirmation) {
351351
throw new Exception(SubjectConfirmationIssue.prettyPrintIssues(validationIssues));
352352
}
353353
}

core/src/main/java/com/onelogin/saml2/Metadata.java renamed to core/src/main/java/com/onelogin/saml2/settings/Metadata.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.onelogin.saml2;
1+
package com.onelogin.saml2.settings;
22

33
import java.net.URL;
44
import java.util.Calendar;
@@ -19,7 +19,6 @@
1919
import org.slf4j.LoggerFactory;
2020
import org.w3c.dom.Document;
2121

22-
import com.onelogin.saml2.settings.Saml2Settings;
2322
import com.onelogin.saml2.model.Contact;
2423
import com.onelogin.saml2.model.Organization;
2524
import com.onelogin.saml2.util.Util;

core/src/main/java/com/onelogin/saml2/settings/Saml2Settings.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88
import java.util.LinkedList;
99
import java.util.List;
1010

11-
import com.onelogin.saml2.authn.SamlResponse;
12-
import com.onelogin.saml2.logout.LogoutResponse;
1311
import org.slf4j.Logger;
1412
import org.slf4j.LoggerFactory;
1513
import org.w3c.dom.Document;
1614
import org.w3c.dom.Element;
1715

18-
import com.onelogin.saml2.Metadata;
1916
import com.onelogin.saml2.model.Contact;
2017
import com.onelogin.saml2.model.Organization;
2118
import com.onelogin.saml2.util.Constants;
@@ -640,7 +637,7 @@ public void setSignatureAlgorithm(String signatureAlgorithm) {
640637
/**
641638
* Controls if unsolicited Responses are rejected if they contain an InResponseTo value.
642639
*
643-
* If false using a validate method {@link SamlResponse#isValid(String)} with a null argument will
640+
* If false using a validate method {@link com.onelogin.saml2.authn.SamlResponse#isValid(String)} with a null argument will
644641
* accept messages with any (or none) InResponseTo value.
645642
*
646643
* If true using these methods with a null argument will only accept messages with no InRespoonseTo value,
@@ -912,4 +909,4 @@ public static List<String> validateMetadata(String metadataString) throws Except
912909

913910
return errors;
914911
}
915-
}
912+
}

0 commit comments

Comments
 (0)