Search in sources :

Example 26 with Saml2LogoutRequest

use of org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest in project spring-security by spring-projects.

the class HttpSessionLogoutRequestRepositoryTests method saveLogoutRequestWhenNotNullThenSaved.

@Test
public void saveLogoutRequestWhenNotNullThenSaved() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    Saml2LogoutRequest logoutRequest = createLogoutRequest().build();
    this.logoutRequestRepository.saveLogoutRequest(logoutRequest, request, new MockHttpServletResponse());
    request.addParameter(Saml2ParameterNames.RELAY_STATE, logoutRequest.getRelayState());
    Saml2LogoutRequest loadedLogoutRequest = this.logoutRequestRepository.loadLogoutRequest(request);
    assertThat(loadedLogoutRequest).isEqualTo(logoutRequest);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Saml2LogoutRequest(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 27 with Saml2LogoutRequest

use of org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest in project spring-security by spring-projects.

the class HttpSessionLogoutRequestRepositoryTests method loadLogoutRequestWhenSavedAndStateParameterNullThenReturnNull.

@Test
public void loadLogoutRequestWhenSavedAndStateParameterNullThenReturnNull() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    Saml2LogoutRequest logoutRequest = createLogoutRequest().build();
    this.logoutRequestRepository.saveLogoutRequest(logoutRequest, request, new MockHttpServletResponse());
    assertThat(this.logoutRequestRepository.loadLogoutRequest(request)).isNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Saml2LogoutRequest(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 28 with Saml2LogoutRequest

use of org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest in project midpoint by Evolveum.

the class MidpointSaml2LogoutRequestResolver method resolve.

@Override
public Saml2LogoutRequest resolve(HttpServletRequest httpServletRequest, Authentication authentication) {
    Saml2AuthenticationToken token = null;
    if (authentication instanceof MidpointAuthentication) {
        ModuleAuthentication authModule = ((MidpointAuthentication) authentication).getProcessingModuleAuthentication();
        if (authModule instanceof Saml2ModuleAuthenticationImpl) {
            if (authModule.getAuthentication() instanceof Saml2AuthenticationToken) {
                token = (Saml2AuthenticationToken) authModule.getAuthentication();
            } else if ((authModule.getAuthentication() instanceof PreAuthenticatedAuthenticationToken || authModule.getAuthentication() instanceof AnonymousAuthenticationToken) && authModule.getAuthentication().getDetails() instanceof Saml2AuthenticationToken) {
                token = (Saml2AuthenticationToken) authModule.getAuthentication().getDetails();
            }
        }
    } else if (authentication instanceof AnonymousAuthenticationToken && authentication.getDetails() instanceof Saml2AuthenticationToken) {
        token = (Saml2AuthenticationToken) authentication.getDetails();
    }
    if (token != null) {
        AuthenticatedPrincipal principal = token.getDetails() instanceof AuthenticatedPrincipal ? (AuthenticatedPrincipal) token.getDetails() : null;
        if (!(principal instanceof Saml2AuthenticatedPrincipal)) {
            String name = token.getRelyingPartyRegistration().getEntityId();
            String relyingPartyRegistrationId = token.getRelyingPartyRegistration().getRegistrationId();
            principal = new Saml2AuthenticatedPrincipal() {

                @Override
                public String getName() {
                    return name;
                }

                @Override
                public String getRelyingPartyRegistrationId() {
                    return relyingPartyRegistrationId;
                }
            };
        }
        return resolver.resolve(httpServletRequest, new Saml2Authentication(principal, token.getSaml2Response(), null));
    }
    return resolver.resolve(httpServletRequest, authentication);
}
Also used : ModuleAuthentication(com.evolveum.midpoint.authentication.api.config.ModuleAuthentication) Saml2Authentication(org.springframework.security.saml2.provider.service.authentication.Saml2Authentication) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) Saml2ModuleAuthenticationImpl(com.evolveum.midpoint.authentication.impl.module.authentication.Saml2ModuleAuthenticationImpl) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) MidpointAuthentication(com.evolveum.midpoint.authentication.api.config.MidpointAuthentication) Saml2AuthenticationToken(org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationToken) Saml2AuthenticatedPrincipal(org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal) Saml2AuthenticatedPrincipal(org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal) AuthenticatedPrincipal(org.springframework.security.core.AuthenticatedPrincipal)

Example 29 with Saml2LogoutRequest

use of org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest in project spring-security by spring-projects.

the class Saml2LogoutConfigurerTests method saml2LogoutWhenCustomLogoutRequestResolverThenUses.

@Test
public void saml2LogoutWhenCustomLogoutRequestResolverThenUses() throws Exception {
    this.spring.register(Saml2LogoutComponentsConfig.class).autowire();
    RelyingPartyRegistration registration = this.repository.findByRegistrationId("registration-id");
    Saml2LogoutRequest logoutRequest = Saml2LogoutRequest.withRelyingPartyRegistration(registration).samlRequest(this.rpLogoutRequest).id(this.rpLogoutRequestId).relayState(this.rpLogoutRequestRelayState).parameters((params) -> params.put("Signature", this.rpLogoutRequestSignature)).build();
    given(getBean(Saml2LogoutRequestResolver.class).resolve(any(), any())).willReturn(logoutRequest);
    this.mvc.perform(post("/logout").with(authentication(this.user)).with(csrf()));
    verify(getBean(Saml2LogoutRequestResolver.class)).resolve(any(), any());
}
Also used : RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) BeforeEach(org.junit.jupiter.api.BeforeEach) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) ObjectPostProcessor(org.springframework.security.config.annotation.ObjectPostProcessor) HttpSecurity(org.springframework.security.config.annotation.web.builders.HttpSecurity) Saml2LogoutResponse(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutResponse) RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) Saml2LogoutRequestResolver(org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestResolver) Saml2MessageBinding(org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding) LogoutRequest(org.opensaml.saml.saml2.core.LogoutRequest) Saml2LogoutRequestFilter(org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestFilter) BDDMockito.verify(org.mockito.BDDMockito.verify) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SecurityMockMvcRequestPostProcessors.authentication(org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication) BDDMockito.given(org.mockito.BDDMockito.given) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Saml2LogoutRequestValidator(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequestValidator) SignatureConstants(org.opensaml.xmlsec.signature.support.SignatureConstants) BDDMockito.verifyNoInteractions(org.mockito.BDDMockito.verifyNoInteractions) MockMvcRequestBuilders.put(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put) Saml2Authentication(org.springframework.security.saml2.provider.service.authentication.Saml2Authentication) Saml2LogoutResponseResolver(org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutResponseResolver) Collection(java.util.Collection) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Saml2X509Credential(org.springframework.security.saml2.core.Saml2X509Credential) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockMvcResultMatchers.redirectedUrl(org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl) Instant(java.time.Instant) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Saml2LogoutValidatorResult(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutValidatorResult) LogoutSuccessHandler(org.springframework.security.web.authentication.logout.LogoutSuccessHandler) LogoutHandler(org.springframework.security.web.authentication.logout.LogoutHandler) SecurityMockMvcRequestPostProcessors.csrf(org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf) Saml2LogoutResponseFilter(org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutResponseFilter) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpSessionLogoutRequestRepository(org.springframework.security.saml2.provider.service.web.authentication.logout.HttpSessionLogoutRequestRepository) DefaultSaml2AuthenticatedPrincipal(org.springframework.security.saml2.provider.service.authentication.DefaultSaml2AuthenticatedPrincipal) RelyingPartyRegistrationRepository(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository) InMemoryRelyingPartyRegistrationRepository(org.springframework.security.saml2.provider.service.registration.InMemoryRelyingPartyRegistrationRepository) Mockito.spy(org.mockito.Mockito.spy) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) TestOpenSamlObjects(org.springframework.security.saml2.provider.service.authentication.TestOpenSamlObjects) MockMvc(org.springframework.test.web.servlet.MockMvc) MockMvcRequestBuilders.delete(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete) Saml2LogoutResponseValidator(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutResponseValidator) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) MockMvcRequestBuilders.post(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post) MvcResult(org.springframework.test.web.servlet.MvcResult) LogoutFilter(org.springframework.security.web.authentication.logout.LogoutFilter) Saml2Utils(org.springframework.security.saml2.core.Saml2Utils) Import(org.springframework.context.annotation.Import) TestSaml2X509Credentials(org.springframework.security.saml2.core.TestSaml2X509Credentials) Saml2LogoutRequestRepository(org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestRepository) SecurityFilterChain(org.springframework.security.web.SecurityFilterChain) Consumer(java.util.function.Consumer) AfterEach(org.junit.jupiter.api.AfterEach) EnableWebSecurity(org.springframework.security.config.annotation.web.configuration.EnableWebSecurity) SpringTestContext(org.springframework.security.config.test.SpringTestContext) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) SpringTestContextExtension(org.springframework.security.config.test.SpringTestContextExtension) Bean(org.springframework.context.annotation.Bean) BDDMockito.mock(org.mockito.BDDMockito.mock) Customizer.withDefaults(org.springframework.security.config.Customizer.withDefaults) Saml2LogoutRequest(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest) Collections(java.util.Collections) AuthorityUtils(org.springframework.security.core.authority.AuthorityUtils) TestRelyingPartyRegistrations(org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) Saml2LogoutRequestResolver(org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestResolver) Saml2LogoutRequest(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest) Test(org.junit.jupiter.api.Test)

Example 30 with Saml2LogoutRequest

use of org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest in project spring-security by spring-projects.

the class Saml2LogoutConfigurerTests method saml2LogoutResponseWhenCustomLogoutResponseHandlerThenUses.

@Test
public void saml2LogoutResponseWhenCustomLogoutResponseHandlerThenUses() throws Exception {
    this.spring.register(Saml2LogoutComponentsConfig.class).autowire();
    RelyingPartyRegistration registration = this.repository.findByRegistrationId("get");
    Saml2LogoutRequest logoutRequest = Saml2LogoutRequest.withRelyingPartyRegistration(registration).samlRequest(this.rpLogoutRequest).id(this.rpLogoutRequestId).relayState(this.rpLogoutRequestRelayState).parameters((params) -> params.put("Signature", this.rpLogoutRequestSignature)).build();
    given(getBean(Saml2LogoutRequestRepository.class).removeLogoutRequest(any(), any())).willReturn(logoutRequest);
    given(getBean(Saml2LogoutResponseValidator.class).validate(any())).willReturn(Saml2LogoutValidatorResult.success());
    this.mvc.perform(get("/logout/saml2/slo").param("SAMLResponse", "samlResponse")).andReturn();
    verify(getBean(Saml2LogoutResponseValidator.class)).validate(any());
}
Also used : RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) BeforeEach(org.junit.jupiter.api.BeforeEach) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) ObjectPostProcessor(org.springframework.security.config.annotation.ObjectPostProcessor) HttpSecurity(org.springframework.security.config.annotation.web.builders.HttpSecurity) Saml2LogoutResponse(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutResponse) RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) Saml2LogoutRequestResolver(org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestResolver) Saml2MessageBinding(org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding) LogoutRequest(org.opensaml.saml.saml2.core.LogoutRequest) Saml2LogoutRequestFilter(org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestFilter) BDDMockito.verify(org.mockito.BDDMockito.verify) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SecurityMockMvcRequestPostProcessors.authentication(org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication) BDDMockito.given(org.mockito.BDDMockito.given) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Saml2LogoutRequestValidator(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequestValidator) SignatureConstants(org.opensaml.xmlsec.signature.support.SignatureConstants) BDDMockito.verifyNoInteractions(org.mockito.BDDMockito.verifyNoInteractions) MockMvcRequestBuilders.put(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put) Saml2Authentication(org.springframework.security.saml2.provider.service.authentication.Saml2Authentication) Saml2LogoutResponseResolver(org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutResponseResolver) Collection(java.util.Collection) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Saml2X509Credential(org.springframework.security.saml2.core.Saml2X509Credential) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockMvcResultMatchers.redirectedUrl(org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl) Instant(java.time.Instant) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Saml2LogoutValidatorResult(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutValidatorResult) LogoutSuccessHandler(org.springframework.security.web.authentication.logout.LogoutSuccessHandler) LogoutHandler(org.springframework.security.web.authentication.logout.LogoutHandler) SecurityMockMvcRequestPostProcessors.csrf(org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf) Saml2LogoutResponseFilter(org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutResponseFilter) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpSessionLogoutRequestRepository(org.springframework.security.saml2.provider.service.web.authentication.logout.HttpSessionLogoutRequestRepository) DefaultSaml2AuthenticatedPrincipal(org.springframework.security.saml2.provider.service.authentication.DefaultSaml2AuthenticatedPrincipal) RelyingPartyRegistrationRepository(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository) InMemoryRelyingPartyRegistrationRepository(org.springframework.security.saml2.provider.service.registration.InMemoryRelyingPartyRegistrationRepository) Mockito.spy(org.mockito.Mockito.spy) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) TestOpenSamlObjects(org.springframework.security.saml2.provider.service.authentication.TestOpenSamlObjects) MockMvc(org.springframework.test.web.servlet.MockMvc) MockMvcRequestBuilders.delete(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete) Saml2LogoutResponseValidator(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutResponseValidator) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) MockMvcRequestBuilders.post(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post) MvcResult(org.springframework.test.web.servlet.MvcResult) LogoutFilter(org.springframework.security.web.authentication.logout.LogoutFilter) Saml2Utils(org.springframework.security.saml2.core.Saml2Utils) Import(org.springframework.context.annotation.Import) TestSaml2X509Credentials(org.springframework.security.saml2.core.TestSaml2X509Credentials) Saml2LogoutRequestRepository(org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestRepository) SecurityFilterChain(org.springframework.security.web.SecurityFilterChain) Consumer(java.util.function.Consumer) AfterEach(org.junit.jupiter.api.AfterEach) EnableWebSecurity(org.springframework.security.config.annotation.web.configuration.EnableWebSecurity) SpringTestContext(org.springframework.security.config.test.SpringTestContext) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) SpringTestContextExtension(org.springframework.security.config.test.SpringTestContextExtension) Bean(org.springframework.context.annotation.Bean) BDDMockito.mock(org.mockito.BDDMockito.mock) Customizer.withDefaults(org.springframework.security.config.Customizer.withDefaults) Saml2LogoutRequest(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest) Collections(java.util.Collections) AuthorityUtils(org.springframework.security.core.authority.AuthorityUtils) TestRelyingPartyRegistrations(org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations) AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) Saml2LogoutRequestRepository(org.springframework.security.saml2.provider.service.web.authentication.logout.Saml2LogoutRequestRepository) Saml2LogoutRequest(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest) Saml2LogoutResponseValidator(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutResponseValidator) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)40 Saml2LogoutRequest (org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest)34 RelyingPartyRegistration (org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration)31 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)27 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)22 LogoutRequest (org.opensaml.saml.saml2.core.LogoutRequest)15 Authentication (org.springframework.security.core.Authentication)12 Saml2MessageBinding (org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)10 Saml2Authentication (org.springframework.security.saml2.provider.service.authentication.Saml2Authentication)10 TestRelyingPartyRegistrations (org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations)10 StandardCharsets (java.nio.charset.StandardCharsets)9 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)8 BDDMockito.given (org.mockito.BDDMockito.given)8 Saml2ParameterNames (org.springframework.security.saml2.core.Saml2ParameterNames)7 DefaultSaml2AuthenticatedPrincipal (org.springframework.security.saml2.provider.service.authentication.DefaultSaml2AuthenticatedPrincipal)7 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)6 AfterEach (org.junit.jupiter.api.AfterEach)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5