|
4 | 4 | import static java.util.Collections.singletonMap; |
5 | 5 | import static org.hamcrest.CoreMatchers.containsString; |
6 | 6 | import static org.hamcrest.CoreMatchers.is; |
| 7 | +import static org.hamcrest.CoreMatchers.not; |
7 | 8 | import static org.hamcrest.CoreMatchers.startsWith; |
8 | 9 | import static org.hamcrest.Matchers.contains; |
9 | 10 | import static org.junit.Assert.assertEquals; |
|
38 | 39 | import com.onelogin.saml2.settings.SettingsBuilder; |
39 | 40 | import com.onelogin.saml2.util.Constants; |
40 | 41 | import com.onelogin.saml2.util.Util; |
| 42 | +import org.mockito.ArgumentCaptor; |
41 | 43 |
|
42 | 44 | public class AuthTest { |
43 | 45 |
|
@@ -104,7 +106,7 @@ public void testConstructorWithReqRes() throws IOException, SettingsException, U |
104 | 106 | assertEquals(settings.getIdpEntityId(), auth.getSettings().getIdpEntityId()); |
105 | 107 | assertEquals(settings.getSpEntityId(), auth.getSettings().getSpEntityId()); |
106 | 108 | } |
107 | | - |
| 109 | + |
108 | 110 | /** |
109 | 111 | * Tests the constructor of Auth |
110 | 112 | * Case: filename, HttpServletRequest and HttpServletResponse provided |
@@ -730,7 +732,7 @@ public void testIsAuthenticated() throws Exception { |
730 | 732 | expectedErrors = new ArrayList<String>(); |
731 | 733 | expectedErrors.add("invalid_response"); |
732 | 734 | assertEquals(expectedErrors, auth2.getErrors()); |
733 | | - assertThat(auth2.getLastErrorReason(), containsString("Invalid issuer in the Assertion/Response")); |
| 735 | + assertThat(auth2.getLastErrorReason(), containsString("Invalid issuer in the Assertion/Response")); |
734 | 736 |
|
735 | 737 | samlResponseEncoded = Util.getFileAsString("data/responses/valid_response.xml.base64"); |
736 | 738 | when(request.getParameterMap()).thenReturn(singletonMap("SAMLResponse", new String[]{samlResponseEncoded})); |
@@ -963,6 +965,36 @@ public void testLoginWithRelayState() throws IOException, SettingsException, URI |
963 | 965 | verify(response).sendRedirect(matches("https:\\/\\/pitbulk.no-ip.org\\/simplesaml\\/saml2\\/idp\\/SSOService.php\\?SAMLRequest=(.)*&RelayState=http%3A%2F%2Flocalhost%3A8080%2Fexpected.jsp")); |
964 | 966 | } |
965 | 967 |
|
| 968 | + /** |
| 969 | + * Tests the login method of Auth |
| 970 | + * Case: Login with empty relayState - no relayState appended |
| 971 | + * |
| 972 | + * @throws SettingsException |
| 973 | + * @throws IOException |
| 974 | + * @throws URISyntaxException |
| 975 | + * |
| 976 | + * @see com.onelogin.saml2.Auth#login |
| 977 | + */ |
| 978 | + @Test |
| 979 | + public void testLoginWithoutRelayState() throws IOException, SettingsException, URISyntaxException { |
| 980 | + HttpServletRequest request = mock(HttpServletRequest.class); |
| 981 | + HttpServletResponse response = mock(HttpServletResponse.class); |
| 982 | + when(request.getScheme()).thenReturn("http"); |
| 983 | + when(request.getServerPort()).thenReturn(8080); |
| 984 | + when(request.getServerName()).thenReturn("localhost"); |
| 985 | + when(request.getRequestURI()).thenReturn("/initial.jsp"); |
| 986 | + |
| 987 | + Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build(); |
| 988 | + settings.setAuthnRequestsSigned(false); |
| 989 | + |
| 990 | + Auth auth = new Auth(settings, request, response); |
| 991 | + auth.login(""); |
| 992 | + final ArgumentCaptor<String> urlCaptor = ArgumentCaptor.forClass(String.class); |
| 993 | + verify(response).sendRedirect(urlCaptor.capture()); |
| 994 | + assertThat(urlCaptor.getValue(), startsWith("https://pitbulk.no-ip.org/simplesaml/saml2/idp/SSOService.php?SAMLRequest=")); |
| 995 | + assertThat(urlCaptor.getValue(), not(containsString("&RelayState="))); |
| 996 | + } |
| 997 | + |
966 | 998 | /** |
967 | 999 | * Tests the login method of Auth |
968 | 1000 | * Case: Signed Login but no sp key |
@@ -1080,6 +1112,37 @@ public void testLogoutWithRelayState() throws IOException, SettingsException, XM |
1080 | 1112 | verify(response).sendRedirect(matches("https:\\/\\/pitbulk.no-ip.org\\/simplesaml\\/saml2\\/idp\\/SingleLogoutService.php\\?SAMLRequest=(.)*&RelayState=http%3A%2F%2Flocalhost%3A8080%2Fexpected.jsp")); |
1081 | 1113 | } |
1082 | 1114 |
|
| 1115 | + /** |
| 1116 | + * Tests the logout method of Auth |
| 1117 | + * Case: Logout with empty RelayState - no RelayState appended |
| 1118 | + * |
| 1119 | + * @throws IOException |
| 1120 | + * @throws SettingsException |
| 1121 | + * @throws XMLEntityException |
| 1122 | + * |
| 1123 | + * @see com.onelogin.saml2.Auth#logout |
| 1124 | + */ |
| 1125 | + @Test |
| 1126 | + public void testLogoutWithoutRelayState() throws IOException, SettingsException, XMLEntityException { |
| 1127 | + HttpServletRequest request = mock(HttpServletRequest.class); |
| 1128 | + HttpServletResponse response = mock(HttpServletResponse.class); |
| 1129 | + when(request.getScheme()).thenReturn("http"); |
| 1130 | + when(request.getServerPort()).thenReturn(8080); |
| 1131 | + when(request.getServerName()).thenReturn("localhost"); |
| 1132 | + when(request.getRequestURI()).thenReturn("/initial.jsp"); |
| 1133 | + |
| 1134 | + Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build(); |
| 1135 | + settings.setLogoutRequestSigned(false); |
| 1136 | + |
| 1137 | + Auth auth = new Auth(settings, request, response); |
| 1138 | + auth.logout(""); |
| 1139 | + |
| 1140 | + final ArgumentCaptor<String> urlCaptor = ArgumentCaptor.forClass(String.class); |
| 1141 | + verify(response).sendRedirect(urlCaptor.capture()); |
| 1142 | + assertThat(urlCaptor.getValue(), startsWith("https://pitbulk.no-ip.org/simplesaml/saml2/idp/SingleLogoutService.php?SAMLRequest=")); |
| 1143 | + assertThat(urlCaptor.getValue(), not(containsString("&RelayState="))); |
| 1144 | + } |
| 1145 | + |
1083 | 1146 | /** |
1084 | 1147 | * Tests the logout method of Auth |
1085 | 1148 | * Case: Signed Logout but no sp key |
@@ -1434,6 +1497,14 @@ public void testBuildSignature() throws URISyntaxException, IOException, Setting |
1434 | 1497 |
|
1435 | 1498 | signature = auth.buildResponseSignature(deflatedEncodedLogoutResponse, null, ""); |
1436 | 1499 | assertEquals(expectedSignature, signature); |
| 1500 | + |
| 1501 | + signature = auth.buildRequestSignature(deflatedEncodedAuthNRequest, "", signAlgorithm); |
| 1502 | + expectedSignature = "NS/yZ0WkHHtPU6LBWioxTzFsATJC6k7D8PcmBuM4NcC1klHSX5gmgDJdGs+7ee433RxhsTRLDNXJnXInAFG5iqZQK/Jps1aqx9iCAwfC4GCJs605e/hw3UXWKKo1lKxwE4Zu6eJ0TsMQ2gj/5qLezQL98CgqmFHLhvNgGJZcG6U="; |
| 1503 | + assertEquals(expectedSignature, signature); |
| 1504 | + |
| 1505 | + signature = auth.buildRequestSignature(deflatedEncodedLogoutResponse, "", signAlgorithm); |
| 1506 | + expectedSignature = "GiO58DZMcRb8QR+dxUvn9bp5tIp2Eal8+tvOAEbYoAX6+7TMO8tTkpPjRD60pG+SMYjTC+lXQHygX2AXcO5ZQj8snfqx94C3dCOP7gLKOowFcaD0TunmnFCBx6qLv2cOleS9PSx49BSZJiGuffNcfgvTvsyqGwC2EatPP2+AxDM="; |
| 1507 | + assertEquals(expectedSignature, signature); |
1437 | 1508 | } |
1438 | 1509 |
|
1439 | 1510 | } |
0 commit comments