Skip to content

Commit 1e4534d

Browse files
committed
Refactor. Add test. Add support in LogoutResponse
1 parent 72520f8 commit 1e4534d

File tree

5 files changed

+63
-21
lines changed

5 files changed

+63
-21
lines changed

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

Lines changed: 7 additions & 6 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
*
@@ -107,11 +112,6 @@ public SamlResponse(Saml2Settings settings, String currentUrl, String samlRespon
107112
loadXmlFromBase64(samlResponse);
108113
}
109114

110-
/**
111-
* The respone status code and messages
112-
*/
113-
private SamlResponseStatus responseStatus;
114-
115115
/**
116116
* Constructor to have a Response object fully built and ready to validate the saml response.
117117
*
@@ -126,6 +126,7 @@ public SamlResponse(Saml2Settings settings, String currentUrl, String samlRespon
126126
* @throws SAXException
127127
* @throws ParserConfigurationException
128128
* @throws XPathExpressionException
129+
* @throws NullPointerException
129130
*
130131
*/
131132
public SamlResponse(Saml2Settings settings, HttpRequest request) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException, SettingsException, ValidationError {
@@ -603,7 +604,7 @@ public HashMap<String, List<String>> getAttributes() throws XPathExpressionExcep
603604
}
604605

605606
/**
606-
* Returns the latest response status
607+
* Returns the ResponseStatus object
607608
*
608609
* @return
609610
*/

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: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -754,17 +754,17 @@ public void processResponse(String requestId) throws Exception {
754754
validationException = samlResponse.getValidationException();
755755
SamlResponseStatus samlResponseStatus = samlResponse.getResponseStatus();
756756
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);
757+
errors.add("response_not_success");
758+
LOGGER.error("processResponse error. sso_not_success");
759+
LOGGER.debug(" --> " + samlResponseParameter);
760760
errors.add(samlResponseStatus.getStatusCode());
761-
if (samlResponseStatus.getSubStatusCode() != null) {
762-
errors.add(samlResponseStatus.getSubStatusCode());
763-
}
761+
if (samlResponseStatus.getSubStatusCode() != null) {
762+
errors.add(samlResponseStatus.getSubStatusCode());
763+
}
764764
} else {
765-
errors.add("invalid_response");
766-
LOGGER.error("processResponse error. invalid_response");
767-
LOGGER.debug(" --> " + samlResponseParameter);
765+
errors.add("invalid_response");
766+
LOGGER.error("processResponse error. invalid_response");
767+
LOGGER.debug(" --> " + samlResponseParameter);
768768
}
769769
}
770770
} else {
@@ -810,11 +810,16 @@ public void processSLO(Boolean keepLocalSession, String requestId) throws Except
810810
errorReason = logoutResponse.getError();
811811
validationException = logoutResponse.getValidationException();
812812
} else {
813-
String status = logoutResponse.getStatus();
813+
SamlResponseStatus samlResponseStatus = logoutResponse.getSamlResponseStatus();
814+
String status = samlResponseStatus.getStatusCode();
814815
if (status == null || !status.equals(Constants.STATUS_SUCCESS)) {
815816
errors.add("logout_not_success");
816817
LOGGER.error("processSLO error. logout_not_success");
817818
LOGGER.debug(" --> " + samlResponseParameter);
819+
errors.add(samlResponseStatus.getStatusCode());
820+
if (samlResponseStatus.getSubStatusCode() != null) {
821+
errors.add(samlResponseStatus.getSubStatusCode());
822+
}
818823
} else {
819824
lastMessageId = logoutResponse.getId();
820825
LOGGER.debug("processSLO success --> " + samlResponseParameter);

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

Lines changed: 34 additions & 2 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
/**
@@ -853,7 +887,6 @@ public void testIsAuthenticated() throws Exception {
853887
assertFalse(auth.getErrors().isEmpty());
854888
List<String> expectedErrors = new ArrayList<String>();
855889
expectedErrors.add("invalid_response");
856-
expectedErrors.add("urn:oasis:names:tc:SAML:2.0:status:Success");
857890
assertEquals(expectedErrors, auth.getErrors());
858891
assertEquals("SAML Response must contain 1 Assertion.", auth.getLastErrorReason());
859892
assertTrue(auth.getLastValidationException() instanceof ValidationError);
@@ -868,7 +901,6 @@ public void testIsAuthenticated() throws Exception {
868901
assertFalse(auth2.getErrors().isEmpty());
869902
expectedErrors = new ArrayList<String>();
870903
expectedErrors.add("invalid_response");
871-
expectedErrors.add("urn:oasis:names:tc:SAML:2.0:status:Success");
872904
assertEquals(expectedErrors, auth2.getErrors());
873905
assertThat(auth2.getLastErrorReason(), containsString("Invalid issuer in the Assertion/Response"));
874906
assertTrue(auth2.getLastValidationException() instanceof ValidationError);

0 commit comments

Comments
 (0)