Search in sources :

Example 1 with Saml2MetadataFilter

use of org.springframework.security.saml2.provider.service.web.Saml2MetadataFilter in project spring-security by spring-projects.

the class Saml2MetadataFilterTests method doFilterWhenSetMetadataFilenameThenUses.

@Test
public void doFilterWhenSetMetadataFilenameThenUses() throws Exception {
    RelyingPartyRegistration validRegistration = TestRelyingPartyRegistrations.full().build();
    String testMetadataFilename = "test-{registrationId}-metadata.xml";
    String fileName = testMetadataFilename.replace("{registrationId}", validRegistration.getRegistrationId());
    String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name());
    String generatedMetadata = "<xml>test</xml>";
    this.request.setPathInfo("/saml2/service-provider-metadata/registration-id");
    given(this.resolver.resolve(validRegistration)).willReturn(generatedMetadata);
    this.filter = new Saml2MetadataFilter((request) -> validRegistration, this.resolver);
    this.filter.setMetadataFilename(testMetadataFilename);
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.response.getHeaderValue(HttpHeaders.CONTENT_DISPOSITION)).asString().isEqualTo("attachment; filename=\"%s\"; filename*=UTF-8''%s", fileName, encodedFileName);
}
Also used : RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Saml2MetadataResolver(org.springframework.security.saml2.provider.service.metadata.Saml2MetadataResolver) MockFilterChain(org.springframework.mock.web.MockFilterChain) HttpHeaders(org.springframework.http.HttpHeaders) RelyingPartyRegistrationRepository(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TestSaml2X509Credentials(org.springframework.security.saml2.core.TestSaml2X509Credentials) StandardCharsets(java.nio.charset.StandardCharsets) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) Mockito.verify(org.mockito.Mockito.verify) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) Test(org.junit.jupiter.api.Test) URLEncoder(java.net.URLEncoder) BDDMockito.given(org.mockito.BDDMockito.given) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) TestRelyingPartyRegistrations(org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) Mockito.mock(org.mockito.Mockito.mock) Test(org.junit.jupiter.api.Test)

Example 2 with Saml2MetadataFilter

use of org.springframework.security.saml2.provider.service.web.Saml2MetadataFilter in project spring-security by spring-projects.

the class Saml2MetadataFilterTests method doFilterWhenPathStartsWithRegistrationIdThenServesMetadata.

@Test
public void doFilterWhenPathStartsWithRegistrationIdThenServesMetadata() throws Exception {
    RelyingPartyRegistration registration = TestRelyingPartyRegistrations.full().build();
    given(this.repository.findByRegistrationId("registration-id")).willReturn(registration);
    given(this.resolver.resolve(any())).willReturn("metadata");
    RelyingPartyRegistrationResolver resolver = new DefaultRelyingPartyRegistrationResolver((id) -> this.repository.findByRegistrationId("registration-id"));
    this.filter = new Saml2MetadataFilter(resolver, this.resolver);
    this.filter.setRequestMatcher(new AntPathRequestMatcher("/metadata"));
    this.request.setPathInfo("/metadata");
    this.filter.doFilter(this.request, this.response, new MockFilterChain());
    verify(this.repository).findByRegistrationId("registration-id");
}
Also used : RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.jupiter.api.Test)

Example 3 with Saml2MetadataFilter

use of org.springframework.security.saml2.provider.service.web.Saml2MetadataFilter in project midpoint by Evolveum.

the class SamlModuleWebSecurityConfigurer method configure.

@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    MidpointSaml2LoginConfigurer configurer = new MidpointSaml2LoginConfigurer<>(auditProvider);
    configurer.relyingPartyRegistrationRepository(relyingPartyRegistrations()).loginProcessingUrl(getConfiguration().getPrefixOfModule() + SamlModuleWebSecurityConfiguration.SSO_LOCATION_URL_SUFFIX).successHandler(getObjectPostProcessor().postProcess(new MidPointAuthenticationSuccessHandler())).failureHandler(new MidpointAuthenticationFailureHandler());
    try {
        configurer.authenticationManager(new ProviderManager(Collections.emptyList(), authenticationManager()));
    } catch (Exception e) {
        LOGGER.error("Couldn't initialize authentication manager for saml2 module");
    }
    getOrApply(http, configurer);
    Saml2MetadataFilter filter = new Saml2MetadataFilter(new MidpointMetadataRelyingPartyRegistrationResolver(relyingPartyRegistrations()), new OpenSamlMetadataResolver());
    filter.setRequestMatcher(new AntPathRequestMatcher(getConfiguration().getPrefixOfModule() + "/metadata/*"));
    http.addFilterAfter(filter, Saml2WebSsoAuthenticationFilter.class);
}
Also used : MidPointAuthenticationSuccessHandler(com.evolveum.midpoint.authentication.impl.handler.MidPointAuthenticationSuccessHandler) MidpointAuthenticationFailureHandler(com.evolveum.midpoint.authentication.impl.handler.MidpointAuthenticationFailureHandler) Saml2MetadataFilter(org.springframework.security.saml2.provider.service.web.Saml2MetadataFilter) ProviderManager(org.springframework.security.authentication.ProviderManager) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) MidpointSaml2LoginConfigurer(com.evolveum.midpoint.authentication.impl.saml.MidpointSaml2LoginConfigurer) MidpointMetadataRelyingPartyRegistrationResolver(com.evolveum.midpoint.authentication.impl.saml.MidpointMetadataRelyingPartyRegistrationResolver) OpenSamlMetadataResolver(org.springframework.security.saml2.provider.service.metadata.OpenSamlMetadataResolver)

Example 4 with Saml2MetadataFilter

use of org.springframework.security.saml2.provider.service.web.Saml2MetadataFilter in project spring-security by spring-projects.

the class Saml2MetadataFilterTests method doFilterWhenRelyingPartyRegistrationFoundThenInvokesMetadataResolver.

@Test
public void doFilterWhenRelyingPartyRegistrationFoundThenInvokesMetadataResolver() throws Exception {
    this.request.setPathInfo("/saml2/service-provider-metadata/validRegistration");
    RelyingPartyRegistration validRegistration = TestRelyingPartyRegistrations.noCredentials().assertingPartyDetails((party) -> party.verificationX509Credentials((c) -> c.add(TestSaml2X509Credentials.relyingPartyVerifyingCredential()))).build();
    String generatedMetadata = "<xml>test</xml>";
    given(this.resolver.resolve(validRegistration)).willReturn(generatedMetadata);
    this.filter = new Saml2MetadataFilter((request) -> validRegistration, this.resolver);
    this.filter.doFilter(this.request, this.response, this.chain);
    verifyNoInteractions(this.chain);
    assertThat(this.response.getStatus()).isEqualTo(200);
    assertThat(this.response.getContentAsString()).isEqualTo(generatedMetadata);
    verify(this.resolver).resolve(validRegistration);
}
Also used : RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Saml2MetadataResolver(org.springframework.security.saml2.provider.service.metadata.Saml2MetadataResolver) MockFilterChain(org.springframework.mock.web.MockFilterChain) HttpHeaders(org.springframework.http.HttpHeaders) RelyingPartyRegistrationRepository(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FilterChain(jakarta.servlet.FilterChain) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TestSaml2X509Credentials(org.springframework.security.saml2.core.TestSaml2X509Credentials) StandardCharsets(java.nio.charset.StandardCharsets) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) Mockito.verify(org.mockito.Mockito.verify) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) Test(org.junit.jupiter.api.Test) URLEncoder(java.net.URLEncoder) BDDMockito.given(org.mockito.BDDMockito.given) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) TestRelyingPartyRegistrations(org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) Mockito.mock(org.mockito.Mockito.mock) Test(org.junit.jupiter.api.Test)

Example 5 with Saml2MetadataFilter

use of org.springframework.security.saml2.provider.service.web.Saml2MetadataFilter in project spring-security by spring-projects.

the class Saml2MetadataFilterTests method setup.

@BeforeEach
public void setup() {
    this.repository = mock(RelyingPartyRegistrationRepository.class);
    this.resolver = mock(Saml2MetadataResolver.class);
    RelyingPartyRegistrationResolver relyingPartyRegistrationResolver = new DefaultRelyingPartyRegistrationResolver(this.repository);
    this.filter = new Saml2MetadataFilter(relyingPartyRegistrationResolver, this.resolver);
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
    this.chain = mock(FilterChain.class);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) FilterChain(jakarta.servlet.FilterChain) RelyingPartyRegistrationRepository(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository) Saml2MetadataResolver(org.springframework.security.saml2.provider.service.metadata.Saml2MetadataResolver) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

MockFilterChain (org.springframework.mock.web.MockFilterChain)4 AntPathRequestMatcher (org.springframework.security.web.util.matcher.AntPathRequestMatcher)4 FilterChain (jakarta.servlet.FilterChain)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Test (org.junit.jupiter.api.Test)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 Saml2MetadataResolver (org.springframework.security.saml2.provider.service.metadata.Saml2MetadataResolver)3 RelyingPartyRegistration (org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration)3 RelyingPartyRegistrationRepository (org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository)3 URLEncoder (java.net.URLEncoder)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)2 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 BDDMockito.given (org.mockito.BDDMockito.given)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.verify (org.mockito.Mockito.verify)2 Mockito.verifyNoInteractions (org.mockito.Mockito.verifyNoInteractions)2