Search in sources :

Example 11 with Saml2LogoutResponse

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

the class Saml2LogoutRequestFilterTests method doFilterWhenSamlRequestThenRedirects.

@Test
public void doFilterWhenSamlRequestThenRedirects() throws Exception {
    RelyingPartyRegistration registration = TestRelyingPartyRegistrations.full().build();
    Authentication authentication = new TestingAuthenticationToken("user", "password");
    SecurityContextHolder.getContext().setAuthentication(authentication);
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/logout/saml2/slo");
    request.setServletPath("/logout/saml2/slo");
    request.setParameter(Saml2ParameterNames.SAML_REQUEST, "request");
    MockHttpServletResponse response = new MockHttpServletResponse();
    given(this.relyingPartyRegistrationResolver.resolve(any(), any())).willReturn(registration);
    given(this.logoutRequestValidator.validate(any())).willReturn(Saml2LogoutValidatorResult.success());
    Saml2LogoutResponse logoutResponse = Saml2LogoutResponse.withRelyingPartyRegistration(registration).samlResponse("response").build();
    given(this.logoutResponseResolver.resolve(any(), any())).willReturn(logoutResponse);
    this.logoutRequestProcessingFilter.doFilterInternal(request, response, new MockFilterChain());
    verify(this.logoutRequestValidator).validate(any());
    verify(this.logoutHandler).logout(any(), any(), any());
    verify(this.logoutResponseResolver).resolve(any(), any());
    String content = response.getHeader("Location");
    assertThat(content).contains(Saml2ParameterNames.SAML_RESPONSE);
    assertThat(content).startsWith(registration.getAssertingPartyDetails().getSingleLogoutServiceResponseLocation());
}
Also used : RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) Authentication(org.springframework.security.core.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Saml2LogoutResponse(org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutResponse) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 12 with Saml2LogoutResponse

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

the class OpenSamlLogoutResponseValidatorTests method handleWhenAuthenticatedThenHandles.

@Test
public void handleWhenAuthenticatedThenHandles() {
    RelyingPartyRegistration registration = signing(verifying(registration())).build();
    Saml2LogoutRequest logoutRequest = Saml2LogoutRequest.withRelyingPartyRegistration(registration).id("id").build();
    LogoutResponse logoutResponse = TestOpenSamlObjects.assertingPartyLogoutResponse(registration);
    sign(logoutResponse, registration);
    Saml2LogoutResponse response = post(logoutResponse, registration);
    Saml2LogoutResponseValidatorParameters parameters = new Saml2LogoutResponseValidatorParameters(response, logoutRequest, registration);
    this.manager.validate(parameters);
}
Also used : RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) LogoutResponse(org.opensaml.saml.saml2.core.LogoutResponse) Test(org.junit.jupiter.api.Test)

Example 13 with Saml2LogoutResponse

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

the class OpenSamlLogoutResponseValidatorTests method handleWhenStatusNotSuccessThenInvalidResponseError.

@Test
public void handleWhenStatusNotSuccessThenInvalidResponseError() {
    RelyingPartyRegistration registration = registration().build();
    Saml2LogoutRequest logoutRequest = Saml2LogoutRequest.withRelyingPartyRegistration(registration).id("id").build();
    LogoutResponse logoutResponse = TestOpenSamlObjects.assertingPartyLogoutResponse(registration);
    logoutResponse.getStatus().getStatusCode().setValue(StatusCode.UNKNOWN_PRINCIPAL);
    sign(logoutResponse, registration);
    Saml2LogoutResponse response = post(logoutResponse, registration);
    Saml2LogoutResponseValidatorParameters parameters = new Saml2LogoutResponseValidatorParameters(response, logoutRequest, registration);
    Saml2LogoutValidatorResult result = this.manager.validate(parameters);
    assertThat(result.hasErrors()).isTrue();
    assertThat(result.getErrors().iterator().next().getErrorCode()).isEqualTo(Saml2ErrorCodes.INVALID_RESPONSE);
}
Also used : RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) LogoutResponse(org.opensaml.saml.saml2.core.LogoutResponse) Test(org.junit.jupiter.api.Test)

Example 14 with Saml2LogoutResponse

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

the class OpenSamlLogoutResponseValidatorTests method handleWhenRedirectBindingThenValidatesSignatureParameter.

@Test
public void handleWhenRedirectBindingThenValidatesSignatureParameter() {
    RelyingPartyRegistration registration = signing(verifying(registration())).assertingPartyDetails((party) -> party.singleLogoutServiceBinding(Saml2MessageBinding.REDIRECT)).build();
    Saml2LogoutRequest logoutRequest = Saml2LogoutRequest.withRelyingPartyRegistration(registration).id("id").build();
    LogoutResponse logoutResponse = TestOpenSamlObjects.assertingPartyLogoutResponse(registration);
    Saml2LogoutResponse response = redirect(logoutResponse, registration, OpenSamlSigningUtils.sign(registration));
    Saml2LogoutResponseValidatorParameters parameters = new Saml2LogoutResponseValidatorParameters(response, logoutRequest, registration);
    this.manager.validate(parameters);
}
Also used : RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) Saml2ErrorCodes(org.springframework.security.saml2.core.Saml2ErrorCodes) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LogoutResponse(org.opensaml.saml.saml2.core.LogoutResponse) QueryParametersPartial(org.springframework.security.saml2.provider.service.authentication.logout.OpenSamlSigningUtils.QueryParametersPartial) TestSaml2X509Credentials(org.springframework.security.saml2.core.TestSaml2X509Credentials) StandardCharsets(java.nio.charset.StandardCharsets) RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) Saml2MessageBinding(org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding) Test(org.junit.jupiter.api.Test) TestOpenSamlObjects(org.springframework.security.saml2.provider.service.authentication.TestOpenSamlObjects) StatusCode(org.opensaml.saml.saml2.core.StatusCode) Saml2ParameterNames(org.springframework.security.saml2.core.Saml2ParameterNames) Map(java.util.Map) XMLObject(org.opensaml.core.xml.XMLObject) TestRelyingPartyRegistrations(org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations) LogoutResponse(org.opensaml.saml.saml2.core.LogoutResponse) Test(org.junit.jupiter.api.Test)

Example 15 with Saml2LogoutResponse

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

the class OpenSamlLogoutResponseValidatorTests method handleWhenInvalidIssuerThenInvalidSignatureError.

@Test
public void handleWhenInvalidIssuerThenInvalidSignatureError() {
    RelyingPartyRegistration registration = registration().build();
    Saml2LogoutRequest logoutRequest = Saml2LogoutRequest.withRelyingPartyRegistration(registration).id("id").build();
    LogoutResponse logoutResponse = TestOpenSamlObjects.assertingPartyLogoutResponse(registration);
    logoutResponse.getIssuer().setValue("wrong");
    sign(logoutResponse, registration);
    Saml2LogoutResponse response = post(logoutResponse, registration);
    Saml2LogoutResponseValidatorParameters parameters = new Saml2LogoutResponseValidatorParameters(response, logoutRequest, registration);
    Saml2LogoutValidatorResult result = this.manager.validate(parameters);
    assertThat(result.hasErrors()).isTrue();
    assertThat(result.getErrors().iterator().next().getErrorCode()).isEqualTo(Saml2ErrorCodes.INVALID_SIGNATURE);
}
Also used : RelyingPartyRegistration(org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration) LogoutResponse(org.opensaml.saml.saml2.core.LogoutResponse) Test(org.junit.jupiter.api.Test)

Aggregations

RelyingPartyRegistration (org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration)16 Test (org.junit.jupiter.api.Test)12 Saml2LogoutResponse (org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutResponse)10 LogoutResponse (org.opensaml.saml.saml2.core.LogoutResponse)9 Authentication (org.springframework.security.core.Authentication)8 Saml2ParameterNames (org.springframework.security.saml2.core.Saml2ParameterNames)8 Saml2MessageBinding (org.springframework.security.saml2.provider.service.registration.Saml2MessageBinding)7 RelyingPartyRegistrationResolver (org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationResolver)7 LogoutRequest (org.opensaml.saml.saml2.core.LogoutRequest)6 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 StandardCharsets (java.nio.charset.StandardCharsets)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 BDDMockito.given (org.mockito.BDDMockito.given)4 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)4 TestRelyingPartyRegistrations (org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)3 Log (org.apache.commons.logging.Log)3 LogFactory (org.apache.commons.logging.LogFactory)3 Mockito.mock (org.mockito.Mockito.mock)3