|
19 | 19 | import static org.mockito.Mockito.when; |
20 | 20 |
|
21 | 21 | import java.io.IOException; |
| 22 | +import java.io.UnsupportedEncodingException; |
| 23 | +import java.net.URI; |
22 | 24 | import java.net.URISyntaxException; |
| 25 | +import java.net.URLDecoder; |
23 | 26 | import java.util.ArrayList; |
24 | 27 | import java.util.HashMap; |
25 | 28 | import java.util.List; |
@@ -51,6 +54,20 @@ public class AuthTest { |
51 | 54 | @Rule |
52 | 55 | public ExpectedException expectedEx = ExpectedException.none(); |
53 | 56 |
|
| 57 | + private String getSAMLRequestFromURL(String url) throws URISyntaxException, UnsupportedEncodingException { |
| 58 | + String xml = ""; |
| 59 | + URI uri = new URI(url); |
| 60 | + String query = uri.getQuery(); |
| 61 | + String[] pairs = query.split("&"); |
| 62 | + for (String pair : pairs) { |
| 63 | + int idx = pair.indexOf("="); |
| 64 | + if (pair.substring(0, idx).equals("SAMLRequest")) { |
| 65 | + xml = Util.base64decodedInflated(pair.substring(idx + 1)); |
| 66 | + } |
| 67 | + } |
| 68 | + return xml; |
| 69 | + } |
| 70 | + |
54 | 71 | /** |
55 | 72 | * Tests the constructor of Auth |
56 | 73 | * Case: No parameters |
@@ -1161,7 +1178,56 @@ public void testLoginStay() throws IOException, SettingsException, URISyntaxExce |
1161 | 1178 | assertThat(target, startsWith("https://pitbulk.no-ip.org/simplesaml/saml2/idp/SSOService.php?SAMLRequest=")); |
1162 | 1179 | assertThat(target, containsString("&RelayState=http%3A%2F%2Flocalhost%3A8080%2Fexpected.jsp")); |
1163 | 1180 | } |
1164 | | - |
| 1181 | + |
| 1182 | + /** |
| 1183 | + * Tests the login method of Auth |
| 1184 | + * Case: Login with Subject enabled |
| 1185 | + * |
| 1186 | + * @throws SettingsException |
| 1187 | + * @throws IOException |
| 1188 | + * @throws URISyntaxException |
| 1189 | + * @throws Error |
| 1190 | + * |
| 1191 | + * @see com.onelogin.saml2.Auth#login |
| 1192 | + */ |
| 1193 | + @Test |
| 1194 | + public void testLoginSubject() throws IOException, SettingsException, URISyntaxException, Error { |
| 1195 | + HttpServletRequest request = mock(HttpServletRequest.class); |
| 1196 | + HttpServletResponse response = mock(HttpServletResponse.class); |
| 1197 | + when(request.getScheme()).thenReturn("http"); |
| 1198 | + when(request.getServerPort()).thenReturn(8080); |
| 1199 | + when(request.getServerName()).thenReturn("localhost"); |
| 1200 | + when(request.getRequestURI()).thenReturn("/initial.jsp"); |
| 1201 | + |
| 1202 | + Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build(); |
| 1203 | + |
| 1204 | + Auth auth = new Auth(settings, request, response); |
| 1205 | + String target = auth.login("", false, false, false, true); |
| 1206 | + assertThat(target, startsWith("http://idp.example.com/simplesaml/saml2/idp/SSOService.php?SAMLRequest=")); |
| 1207 | + String authNRequestStr = getSAMLRequestFromURL(target); |
| 1208 | + assertThat(authNRequestStr, containsString("<samlp:AuthnRequest")); |
| 1209 | + assertThat(authNRequestStr, not(containsString("<saml:Subject"))); |
| 1210 | + |
| 1211 | + target = auth.login("", false, false, false, true, "testuser@example.com"); |
| 1212 | + assertThat(target, startsWith("http://idp.example.com/simplesaml/saml2/idp/SSOService.php?SAMLRequest=")); |
| 1213 | + authNRequestStr = getSAMLRequestFromURL(target); |
| 1214 | + assertThat(authNRequestStr, containsString("<samlp:AuthnRequest")); |
| 1215 | + assertThat(authNRequestStr, containsString("<saml:Subject")); |
| 1216 | + assertThat(authNRequestStr, containsString("Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified\">testuser@example.com</saml:NameID>")); |
| 1217 | + assertThat(authNRequestStr, containsString("<saml:SubjectConfirmation Method=\"urn:oasis:names:tc:SAML:2.0:cm:bearer\">")); |
| 1218 | + |
| 1219 | + settings = new SettingsBuilder().fromFile("config/config.emailaddressformat.properties").build(); |
| 1220 | + auth = new Auth(settings, request, response); |
| 1221 | + target = auth.login("", false, false, false, true, "testuser@example.com"); |
| 1222 | + assertThat(target, startsWith("http://idp.example.com/simplesaml/saml2/idp/SSOService.php?SAMLRequest=")); |
| 1223 | + authNRequestStr = getSAMLRequestFromURL(target); |
| 1224 | + assertThat(authNRequestStr, containsString("<samlp:AuthnRequest")); |
| 1225 | + assertThat(authNRequestStr, containsString("<saml:Subject")); |
| 1226 | + assertThat(authNRequestStr, containsString("Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress\">testuser@example.com</saml:NameID>")); |
| 1227 | + assertThat(authNRequestStr, containsString("<saml:SubjectConfirmation Method=\"urn:oasis:names:tc:SAML:2.0:cm:bearer\">")); |
| 1228 | + |
| 1229 | + } |
| 1230 | + |
1165 | 1231 | /** |
1166 | 1232 | * Tests the login method of Auth |
1167 | 1233 | * Case: Signed Login but no sp key |
|
0 commit comments