|
|
|
@ -74,39 +74,39 @@ public class Saml2MetadataFilterTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void doFilterWhenMatcherSucceedsThenResolverInvoked() throws Exception { |
|
|
|
public void doFilterWhenMatcherSucceedsThenResolverInvoked() throws Exception { |
|
|
|
this.request.setPathInfo("/saml2/service-provider-metadata/registration-id"); |
|
|
|
MockHttpServletRequest request = uri("/saml2/service-provider-metadata/registration-id"); |
|
|
|
this.filter.doFilter(this.request, this.response, this.chain); |
|
|
|
this.filter.doFilter(request, this.response, this.chain); |
|
|
|
verifyNoInteractions(this.chain); |
|
|
|
verifyNoInteractions(this.chain); |
|
|
|
verify(this.repository).findByRegistrationId("registration-id"); |
|
|
|
verify(this.repository).findByRegistrationId("registration-id"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void doFilterWhenMatcherFailsThenProcessesFilterChain() throws Exception { |
|
|
|
public void doFilterWhenMatcherFailsThenProcessesFilterChain() throws Exception { |
|
|
|
this.request.setPathInfo("/saml2/authenticate/registration-id"); |
|
|
|
MockHttpServletRequest request = uri("/saml2/authenticate/registration-id"); |
|
|
|
this.filter.doFilter(this.request, this.response, this.chain); |
|
|
|
this.filter.doFilter(request, this.response, this.chain); |
|
|
|
verify(this.chain).doFilter(this.request, this.response); |
|
|
|
verify(this.chain).doFilter(request, this.response); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void doFilterWhenNoRelyingPartyRegistrationThenUnauthorized() throws Exception { |
|
|
|
public void doFilterWhenNoRelyingPartyRegistrationThenUnauthorized() throws Exception { |
|
|
|
this.request.setPathInfo("/saml2/service-provider-metadata/invalidRegistration"); |
|
|
|
MockHttpServletRequest request = uri("/saml2/service-provider-metadata/invalidRegistration"); |
|
|
|
given(this.repository.findByRegistrationId("invalidRegistration")).willReturn(null); |
|
|
|
given(this.repository.findByRegistrationId("invalidRegistration")).willReturn(null); |
|
|
|
this.filter.doFilter(this.request, this.response, this.chain); |
|
|
|
this.filter.doFilter(request, this.response, this.chain); |
|
|
|
verifyNoInteractions(this.chain); |
|
|
|
verifyNoInteractions(this.chain); |
|
|
|
assertThat(this.response.getStatus()).isEqualTo(401); |
|
|
|
assertThat(this.response.getStatus()).isEqualTo(401); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void doFilterWhenRelyingPartyRegistrationFoundThenInvokesMetadataResolver() throws Exception { |
|
|
|
public void doFilterWhenRelyingPartyRegistrationFoundThenInvokesMetadataResolver() throws Exception { |
|
|
|
this.request.setPathInfo("/saml2/service-provider-metadata/validRegistration"); |
|
|
|
MockHttpServletRequest request = uri("/saml2/service-provider-metadata/validRegistration"); |
|
|
|
RelyingPartyRegistration validRegistration = TestRelyingPartyRegistrations.noCredentials() |
|
|
|
RelyingPartyRegistration validRegistration = TestRelyingPartyRegistrations.noCredentials() |
|
|
|
.assertingPartyDetails((party) -> party |
|
|
|
.assertingPartyDetails((party) -> party |
|
|
|
.verificationX509Credentials((c) -> c.add(TestSaml2X509Credentials.relyingPartyVerifyingCredential()))) |
|
|
|
.verificationX509Credentials((c) -> c.add(TestSaml2X509Credentials.relyingPartyVerifyingCredential()))) |
|
|
|
.build(); |
|
|
|
.build(); |
|
|
|
String generatedMetadata = "<xml>test</xml>"; |
|
|
|
String generatedMetadata = "<xml>test</xml>"; |
|
|
|
given(this.resolver.resolve(validRegistration)).willReturn(generatedMetadata); |
|
|
|
given(this.resolver.resolve(validRegistration)).willReturn(generatedMetadata); |
|
|
|
this.filter = new Saml2MetadataFilter((request, registrationId) -> validRegistration, this.resolver); |
|
|
|
this.filter = new Saml2MetadataFilter((r, registrationId) -> validRegistration, this.resolver); |
|
|
|
this.filter.doFilter(this.request, this.response, this.chain); |
|
|
|
this.filter.doFilter(request, this.response, this.chain); |
|
|
|
verifyNoInteractions(this.chain); |
|
|
|
verifyNoInteractions(this.chain); |
|
|
|
assertThat(this.response.getStatus()).isEqualTo(200); |
|
|
|
assertThat(this.response.getStatus()).isEqualTo(200); |
|
|
|
assertThat(this.response.getContentAsString()).isEqualTo(generatedMetadata); |
|
|
|
assertThat(this.response.getContentAsString()).isEqualTo(generatedMetadata); |
|
|
|
@ -128,9 +128,9 @@ public class Saml2MetadataFilterTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void doFilterWhenCustomRequestMatcherThenUses() throws Exception { |
|
|
|
public void doFilterWhenCustomRequestMatcherThenUses() throws Exception { |
|
|
|
this.request.setPathInfo("/path"); |
|
|
|
MockHttpServletRequest request = uri("/path"); |
|
|
|
this.filter.setRequestMatcher(new AntPathRequestMatcher("/path")); |
|
|
|
this.filter.setRequestMatcher(new AntPathRequestMatcher("/path")); |
|
|
|
this.filter.doFilter(this.request, this.response, this.chain); |
|
|
|
this.filter.doFilter(request, this.response, this.chain); |
|
|
|
verifyNoInteractions(this.chain); |
|
|
|
verifyNoInteractions(this.chain); |
|
|
|
verify(this.repository).findByRegistrationId("path"); |
|
|
|
verify(this.repository).findByRegistrationId("path"); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -142,11 +142,11 @@ public class Saml2MetadataFilterTests { |
|
|
|
String fileName = testMetadataFilename.replace("{registrationId}", validRegistration.getRegistrationId()); |
|
|
|
String fileName = testMetadataFilename.replace("{registrationId}", validRegistration.getRegistrationId()); |
|
|
|
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()); |
|
|
|
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()); |
|
|
|
String generatedMetadata = "<xml>test</xml>"; |
|
|
|
String generatedMetadata = "<xml>test</xml>"; |
|
|
|
this.request.setPathInfo("/saml2/service-provider-metadata/registration-id"); |
|
|
|
MockHttpServletRequest request = uri("/saml2/service-provider-metadata/registration-id"); |
|
|
|
given(this.resolver.resolve(validRegistration)).willReturn(generatedMetadata); |
|
|
|
given(this.resolver.resolve(validRegistration)).willReturn(generatedMetadata); |
|
|
|
this.filter = new Saml2MetadataFilter((request, registrationId) -> validRegistration, this.resolver); |
|
|
|
this.filter = new Saml2MetadataFilter((r, registrationId) -> validRegistration, this.resolver); |
|
|
|
this.filter.setMetadataFilename(testMetadataFilename); |
|
|
|
this.filter.setMetadataFilename(testMetadataFilename); |
|
|
|
this.filter.doFilter(this.request, this.response, this.chain); |
|
|
|
this.filter.doFilter(request, this.response, this.chain); |
|
|
|
assertThat(this.response.getHeaderValue(HttpHeaders.CONTENT_DISPOSITION)).asString() |
|
|
|
assertThat(this.response.getHeaderValue(HttpHeaders.CONTENT_DISPOSITION)).asString() |
|
|
|
.isEqualTo("attachment; filename=\"%s\"; filename*=UTF-8''%s", fileName, encodedFileName); |
|
|
|
.isEqualTo("attachment; filename=\"%s\"; filename*=UTF-8''%s", fileName, encodedFileName); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -160,8 +160,8 @@ public class Saml2MetadataFilterTests { |
|
|
|
(id) -> this.repository.findByRegistrationId("registration-id")); |
|
|
|
(id) -> this.repository.findByRegistrationId("registration-id")); |
|
|
|
this.filter = new Saml2MetadataFilter(resolver, this.resolver); |
|
|
|
this.filter = new Saml2MetadataFilter(resolver, this.resolver); |
|
|
|
this.filter.setRequestMatcher(new AntPathRequestMatcher("/metadata")); |
|
|
|
this.filter.setRequestMatcher(new AntPathRequestMatcher("/metadata")); |
|
|
|
this.request.setPathInfo("/metadata"); |
|
|
|
MockHttpServletRequest request = uri("/metadata"); |
|
|
|
this.filter.doFilter(this.request, this.response, new MockFilterChain()); |
|
|
|
this.filter.doFilter(request, this.response, new MockFilterChain()); |
|
|
|
verify(this.repository).findByRegistrationId("registration-id"); |
|
|
|
verify(this.repository).findByRegistrationId("registration-id"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -174,8 +174,8 @@ public class Saml2MetadataFilterTests { |
|
|
|
this.filter = new Saml2MetadataFilter((id) -> this.repository.findByRegistrationId("registration-id"), |
|
|
|
this.filter = new Saml2MetadataFilter((id) -> this.repository.findByRegistrationId("registration-id"), |
|
|
|
this.resolver); |
|
|
|
this.resolver); |
|
|
|
this.filter.setRequestMatcher(new AntPathRequestMatcher("/metadata")); |
|
|
|
this.filter.setRequestMatcher(new AntPathRequestMatcher("/metadata")); |
|
|
|
this.request.setPathInfo("/metadata"); |
|
|
|
MockHttpServletRequest request = uri("/metadata"); |
|
|
|
this.filter.doFilter(this.request, this.response, new MockFilterChain()); |
|
|
|
this.filter.doFilter(request, this.response, new MockFilterChain()); |
|
|
|
verify(this.repository).findByRegistrationId("registration-id"); |
|
|
|
verify(this.repository).findByRegistrationId("registration-id"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -185,11 +185,11 @@ public class Saml2MetadataFilterTests { |
|
|
|
RelyingPartyRegistration validRegistration = TestRelyingPartyRegistrations.full().build(); |
|
|
|
RelyingPartyRegistration validRegistration = TestRelyingPartyRegistrations.full().build(); |
|
|
|
String testMetadataFilename = "test-{registrationId}-metadata.xml"; |
|
|
|
String testMetadataFilename = "test-{registrationId}-metadata.xml"; |
|
|
|
String generatedMetadata = "<xml>testäöü</xml>"; |
|
|
|
String generatedMetadata = "<xml>testäöü</xml>"; |
|
|
|
this.request.setPathInfo("/saml2/service-provider-metadata/registration-id"); |
|
|
|
MockHttpServletRequest request = uri("/saml2/service-provider-metadata/registration-id"); |
|
|
|
given(this.resolver.resolve(validRegistration)).willReturn(generatedMetadata); |
|
|
|
given(this.resolver.resolve(validRegistration)).willReturn(generatedMetadata); |
|
|
|
this.filter = new Saml2MetadataFilter((req, id) -> validRegistration, this.resolver); |
|
|
|
this.filter = new Saml2MetadataFilter((req, id) -> validRegistration, this.resolver); |
|
|
|
this.filter.setMetadataFilename(testMetadataFilename); |
|
|
|
this.filter.setMetadataFilename(testMetadataFilename); |
|
|
|
this.filter.doFilter(this.request, this.response, this.chain); |
|
|
|
this.filter.doFilter(request, this.response, this.chain); |
|
|
|
assertThat(this.response.getCharacterEncoding()).isEqualTo(StandardCharsets.UTF_8.name()); |
|
|
|
assertThat(this.response.getCharacterEncoding()).isEqualTo(StandardCharsets.UTF_8.name()); |
|
|
|
assertThat(this.response.getContentAsString(StandardCharsets.UTF_8)).isEqualTo(generatedMetadata); |
|
|
|
assertThat(this.response.getContentAsString(StandardCharsets.UTF_8)).isEqualTo(generatedMetadata); |
|
|
|
assertThat(this.response.getContentLength()) |
|
|
|
assertThat(this.response.getContentLength()) |
|
|
|
@ -218,9 +218,15 @@ public class Saml2MetadataFilterTests { |
|
|
|
public void constructorWhenRelyingPartyRegistrationRepositoryThenUses() throws Exception { |
|
|
|
public void constructorWhenRelyingPartyRegistrationRepositoryThenUses() throws Exception { |
|
|
|
RelyingPartyRegistrationRepository repository = mock(RelyingPartyRegistrationRepository.class); |
|
|
|
RelyingPartyRegistrationRepository repository = mock(RelyingPartyRegistrationRepository.class); |
|
|
|
this.filter = new Saml2MetadataFilter(repository, this.resolver); |
|
|
|
this.filter = new Saml2MetadataFilter(repository, this.resolver); |
|
|
|
this.request.setPathInfo("/saml2/service-provider-metadata/one"); |
|
|
|
MockHttpServletRequest request = uri("/saml2/service-provider-metadata/one"); |
|
|
|
this.filter.doFilter(this.request, this.response, this.chain); |
|
|
|
this.filter.doFilter(request, this.response, this.chain); |
|
|
|
verify(repository).findByRegistrationId("one"); |
|
|
|
verify(repository).findByRegistrationId("one"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private MockHttpServletRequest uri(String uri) { |
|
|
|
|
|
|
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri); |
|
|
|
|
|
|
|
request.setPathInfo(uri); |
|
|
|
|
|
|
|
return request; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|