Skip to content

Commit baec1c7

Browse files
committed
Merge branch 'pelaxa-master'
2 parents ca8d59b + 1e4534d commit baec1c7

File tree

5 files changed

+83
-14
lines changed

5 files changed

+83
-14
lines changed

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public class SamlResponse {
8181
*/
8282
private Exception validationException;
8383

84+
/**
85+
* The respone status code and messages
86+
*/
87+
private SamlResponseStatus responseStatus;
88+
8489
/**
8590
* Constructor to have a Response object fully built and ready to validate the saml response.
8691
*
@@ -121,6 +126,7 @@ public SamlResponse(Saml2Settings settings, String currentUrl, String samlRespon
121126
* @throws SAXException
122127
* @throws ParserConfigurationException
123128
* @throws XPathExpressionException
129+
* @throws NullPointerException
124130
*
125131
*/
126132
public SamlResponse(Saml2Settings settings, HttpRequest request) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException, SettingsException, ValidationError {
@@ -597,19 +603,27 @@ public HashMap<String, List<String>> getAttributes() throws XPathExpressionExcep
597603
return attributes;
598604
}
599605

606+
/**
607+
* Returns the ResponseStatus object
608+
*
609+
* @return
610+
*/
611+
public SamlResponseStatus getResponseStatus() {
612+
return this.responseStatus;
613+
}
614+
600615
/**
601616
* Checks the Status
602617
*
603-
* @throws ValidationError
604-
* If status is not success
618+
* @throws ValidationError If status is not success
605619
*/
606620
public void checkStatus() throws ValidationError {
607-
SamlResponseStatus responseStatus = getStatus(samlResponseDocument);
608-
if (!responseStatus.is(Constants.STATUS_SUCCESS)) {
621+
this.responseStatus = getStatus(samlResponseDocument);
622+
if (!this.responseStatus.is(Constants.STATUS_SUCCESS)) {
609623
String statusExceptionMsg = "The status code of the Response was not Success, was "
610-
+ responseStatus.getStatusCode();
611-
if (responseStatus.getStatusMessage() != null) {
612-
statusExceptionMsg += " -> " + responseStatus.getStatusMessage();
624+
+ this.responseStatus.getStatusCode();
625+
if (this.responseStatus.getStatusMessage() != null) {
626+
statusExceptionMsg += " -> " + this.responseStatus.getStatusMessage();
613627
}
614628
throw new ValidationError(statusExceptionMsg, ValidationError.STATUS_CODE_IS_NOT_SUCCESS);
615629
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public class LogoutResponse {
8484
*/
8585
private Exception validationException;
8686

87+
/**
88+
* The respone status code and messages
89+
*/
90+
private SamlResponseStatus responseStatus;
91+
8792
/**
8893
* Constructs the LogoutResponse object.
8994
*
@@ -323,7 +328,7 @@ public String getStatus() throws XPathExpressionException
323328
*/
324329
public SamlResponseStatus getSamlResponseStatus() throws ValidationError
325330
{
326-
String statusXpath = "/samlp:Response/samlp:Status";
331+
String statusXpath = "/samlp:LogoutResponse/samlp:Status";
327332
return Util.getStatus(statusXpath, this.logoutResponseDocument);
328333
}
329334

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,9 +1345,8 @@ public void testValidateTimestampsNB() throws ValidationError, XPathExpressionEx
13451345
@Test
13461346
public void testNullRequest() throws IOException, Error, XPathExpressionException, ParserConfigurationException, SAXException, SettingsException, ValidationError {
13471347
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build();
1348+
expectedEx.expect(NullPointerException.class);
13481349
SamlResponse samlResponse = new SamlResponse(settings, null);
1349-
assertFalse(samlResponse.isValid());
1350-
assertEquals("SAML Response is not loaded", samlResponse.getError());
13511350
}
13521351

13531352
/**

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.onelogin.saml2.http.HttpRequest;
3131
import com.onelogin.saml2.logout.LogoutRequest;
3232
import com.onelogin.saml2.logout.LogoutResponse;
33+
import com.onelogin.saml2.model.SamlResponseStatus;
3334
import com.onelogin.saml2.model.KeyStoreSettings;
3435
import com.onelogin.saml2.servlet.ServletUtils;
3536
import com.onelogin.saml2.settings.Saml2Settings;
@@ -749,11 +750,22 @@ public void processResponse(String requestId) throws Exception {
749750
lastAssertionNotOnOrAfter = samlResponse.getAssertionNotOnOrAfter();
750751
LOGGER.debug("processResponse success --> " + samlResponseParameter);
751752
} else {
752-
errors.add("invalid_response");
753-
LOGGER.error("processResponse error. invalid_response");
754-
LOGGER.debug(" --> " + samlResponseParameter);
755753
errorReason = samlResponse.getError();
756754
validationException = samlResponse.getValidationException();
755+
SamlResponseStatus samlResponseStatus = samlResponse.getResponseStatus();
756+
if (samlResponseStatus.getStatusCode() == null || !samlResponseStatus.getStatusCode().equals(Constants.STATUS_SUCCESS)) {
757+
errors.add("response_not_success");
758+
LOGGER.error("processResponse error. sso_not_success");
759+
LOGGER.debug(" --> " + samlResponseParameter);
760+
errors.add(samlResponseStatus.getStatusCode());
761+
if (samlResponseStatus.getSubStatusCode() != null) {
762+
errors.add(samlResponseStatus.getSubStatusCode());
763+
}
764+
} else {
765+
errors.add("invalid_response");
766+
LOGGER.error("processResponse error. invalid_response");
767+
LOGGER.debug(" --> " + samlResponseParameter);
768+
}
757769
}
758770
} else {
759771
errors.add("invalid_binding");
@@ -798,11 +810,16 @@ public void processSLO(Boolean keepLocalSession, String requestId) throws Except
798810
errorReason = logoutResponse.getError();
799811
validationException = logoutResponse.getValidationException();
800812
} else {
801-
String status = logoutResponse.getStatus();
813+
SamlResponseStatus samlResponseStatus = logoutResponse.getSamlResponseStatus();
814+
String status = samlResponseStatus.getStatusCode();
802815
if (status == null || !status.equals(Constants.STATUS_SUCCESS)) {
803816
errors.add("logout_not_success");
804817
LOGGER.error("processSLO error. logout_not_success");
805818
LOGGER.debug(" --> " + samlResponseParameter);
819+
errors.add(samlResponseStatus.getStatusCode());
820+
if (samlResponseStatus.getSubStatusCode() != null) {
821+
errors.add(samlResponseStatus.getSubStatusCode());
822+
}
806823
} else {
807824
lastMessageId = logoutResponse.getId();
808825
LOGGER.debug("processSLO success --> " + samlResponseParameter);

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.onelogin.saml2.util.Util;
6565

6666
import org.mockito.ArgumentCaptor;
67+
import org.w3c.dom.Document;
6768

6869
public class AuthTest {
6970

@@ -563,6 +564,38 @@ public void testProcessResponse() throws Exception {
563564
assertEquals(keys, auth2.getAttributesName());
564565
}
565566

567+
/**
568+
* Tests the processResponse methods of Auth
569+
* Case: process Response, status code Responder and sub status
570+
*
571+
* @throws Exception
572+
*
573+
* @see com.onelogin.saml2.Auth#processSLO
574+
*/
575+
@Test
576+
public void testProcessResponseStatusResponder() throws Exception {
577+
HttpServletRequest request = mock(HttpServletRequest.class);
578+
HttpServletResponse response = mock(HttpServletResponse.class);
579+
HttpSession session = mock(HttpSession.class);
580+
when(request.getRequestURL()).thenReturn(new StringBuffer("https://example.com/opensso/Consumer/metaAlias/sp"));
581+
when(request.getSession()).thenReturn(session);
582+
583+
String samlResponseEncoded = Util.getFileAsString("data/responses/invalids/status_code_and_sub_status_code_responder_and_msg.xml.base64");
584+
Document samlResponseDoc = Util.loadXML(new String(Util.base64decoder(samlResponseEncoded)));
585+
when(request.getParameterMap()).thenReturn(singletonMap("SAMLResponse", new String[]{samlResponseEncoded}));
586+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build();
587+
Auth auth = new Auth(settings, request, response);
588+
assertFalse(auth.isAuthenticated());
589+
assertTrue(auth.getErrors().isEmpty());
590+
auth.processResponse();
591+
verify(session, times(0)).invalidate();
592+
assertFalse(auth.getErrors().isEmpty());
593+
assertEquals("The status code of the Response was not Success, was urn:oasis:names:tc:SAML:2.0:status:Responder -> something_is_wrong", auth.getLastErrorReason());
594+
assertTrue(auth.getErrors().contains("response_not_success"));
595+
assertTrue(auth.getErrors().contains(Constants.STATUS_RESPONDER));
596+
assertTrue(auth.getErrors().contains(Constants.STATUS_AUTHNFAILED));
597+
}
598+
566599
/**
567600
* Tests the processSLO methods of Auth
568601
*
@@ -825,6 +858,7 @@ public void testProcessSLOResponseStatusResponder() throws Exception {
825858
verify(session, times(0)).invalidate();
826859
assertFalse(auth.getErrors().isEmpty());
827860
assertTrue(auth.getErrors().contains("logout_not_success"));
861+
assertTrue(auth.getErrors().contains(Constants.STATUS_RESPONDER));
828862
}
829863

830864
/**

0 commit comments

Comments
 (0)