Search in sources :

Example 6 with DefaultRelyingPartyRegistrationResolver

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

the class Saml2WebSsoAuthenticationFilterTests method doFilterWhenPathStartsWithRegistrationIdThenAuthenticates.

@Test
public void doFilterWhenPathStartsWithRegistrationIdThenAuthenticates() throws Exception {
    RelyingPartyRegistration registration = TestRelyingPartyRegistrations.full().build();
    Authentication authentication = new TestingAuthenticationToken("user", "password");
    given(this.repository.findByRegistrationId("registration-id")).willReturn(registration);
    given(this.authenticationManager.authenticate(authentication)).willReturn(authentication);
    String loginProcessingUrl = "/{registrationId}/login/saml2/sso";
    RequestMatcher matcher = new AntPathRequestMatcher(loginProcessingUrl);
    DefaultRelyingPartyRegistrationResolver delegate = new DefaultRelyingPartyRegistrationResolver(this.repository);
    RelyingPartyRegistrationResolver resolver = (request, id) -> {
        String registrationId = matcher.matcher(request).getVariables().get("registrationId");
        return delegate.resolve(request, registrationId);
    };
    Saml2AuthenticationTokenConverter authenticationConverter = new Saml2AuthenticationTokenConverter(resolver);
    this.filter = new Saml2WebSsoAuthenticationFilter(authenticationConverter, loginProcessingUrl);
    this.filter.setAuthenticationManager(this.authenticationManager);
    this.request.setPathInfo("/registration-id/login/saml2/sso");
    this.request.setParameter(Saml2ParameterNames.SAML_RESPONSE, "response");
    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) Saml2AuthenticationRequestRepository(org.springframework.security.saml2.provider.service.web.Saml2AuthenticationRequestRepository) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) BeforeEach(org.junit.jupiter.api.BeforeEach) MockFilterChain(org.springframework.mock.web.MockFilterChain) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) RelyingPartyRegistrationRepository(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) Assertions.assertThatNoException(org.assertj.core.api.Assertions.assertThatNoException) BDDMockito.given(org.mockito.BDDMockito.given) AbstractSaml2AuthenticationRequest(org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest) AuthenticationDetailsSource(org.springframework.security.authentication.AuthenticationDetailsSource) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) DefaultRelyingPartyRegistrationResolver(org.springframework.security.saml2.provider.service.web.DefaultRelyingPartyRegistrationResolver) Saml2AuthenticationToken(org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationToken) AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) RelyingPartyRegistrationResolver(org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationResolver) TestSaml2AuthenticationTokens(org.springframework.security.saml2.provider.service.authentication.TestSaml2AuthenticationTokens) Saml2AuthenticationTokenConverter(org.springframework.security.saml2.provider.service.web.Saml2AuthenticationTokenConverter) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Saml2ParameterNames(org.springframework.security.saml2.core.Saml2ParameterNames) Assertions(org.junit.jupiter.api.Assertions) AuthenticationConverter(org.springframework.security.web.authentication.AuthenticationConverter) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Saml2AuthenticationException(org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Authentication(org.springframework.security.core.Authentication) TestRelyingPartyRegistrations(org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) Mockito.mock(org.mockito.Mockito.mock) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) Saml2AuthenticationTokenConverter(org.springframework.security.saml2.provider.service.web.Saml2AuthenticationTokenConverter) Authentication(org.springframework.security.core.Authentication) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) DefaultRelyingPartyRegistrationResolver(org.springframework.security.saml2.provider.service.web.DefaultRelyingPartyRegistrationResolver) DefaultRelyingPartyRegistrationResolver(org.springframework.security.saml2.provider.service.web.DefaultRelyingPartyRegistrationResolver) RelyingPartyRegistrationResolver(org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationResolver) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockFilterChain(org.springframework.mock.web.MockFilterChain) Test(org.junit.jupiter.api.Test)

Example 7 with DefaultRelyingPartyRegistrationResolver

use of org.springframework.security.saml2.provider.service.web.DefaultRelyingPartyRegistrationResolver 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 DefaultRelyingPartyRegistrationResolver (org.springframework.security.saml2.provider.service.web.DefaultRelyingPartyRegistrationResolver)4 RelyingPartyRegistrationResolver (org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationResolver)4 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 RelyingPartyRegistration (org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration)3 RelyingPartyRegistrationRepository (org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository)3 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)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 AbstractSaml2AuthenticationRequest (org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest)2 Saml2AuthenticationRequestFactory (org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationRequestFactory)2 TestRelyingPartyRegistrations (org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations)2 DefaultSaml2AuthenticationRequestContextResolver (org.springframework.security.saml2.provider.service.web.DefaultSaml2AuthenticationRequestContextResolver)2 Saml2AuthenticationRequestContextResolver (org.springframework.security.saml2.provider.service.web.Saml2AuthenticationRequestContextResolver)2