Search in sources :

Example 6 with Saml2AuthenticationException

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

the class OpenSaml4AuthenticationProvider method authenticate.

/**
 * @param authentication the authentication request object, must be of type
 * {@link Saml2AuthenticationToken}
 * @return {@link Saml2Authentication} if the assertion is valid
 * @throws AuthenticationException if a validation exception occurs
 */
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    try {
        Saml2AuthenticationToken token = (Saml2AuthenticationToken) authentication;
        String serializedResponse = token.getSaml2Response();
        Response response = parse(serializedResponse);
        process(token, response);
        AbstractAuthenticationToken authenticationResponse = this.responseAuthenticationConverter.convert(new ResponseToken(response, token));
        if (authenticationResponse != null) {
            authenticationResponse.setDetails(authentication.getDetails());
        }
        return authenticationResponse;
    } catch (Saml2AuthenticationException ex) {
        throw ex;
    } catch (Exception ex) {
        throw createAuthenticationException(Saml2ErrorCodes.INTERNAL_VALIDATION_ERROR, ex.getMessage(), ex);
    }
}
Also used : Response(org.opensaml.saml.saml2.core.Response) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) XSString(org.opensaml.core.xml.schema.XSString) AuthenticationException(org.springframework.security.core.AuthenticationException) Saml2Exception(org.springframework.security.saml2.Saml2Exception)

Example 7 with Saml2AuthenticationException

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

the class OpenSaml4AuthenticationProvider method parse.

private Response parse(String response) throws Saml2Exception, Saml2AuthenticationException {
    try {
        Document document = this.parserPool.parse(new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8)));
        Element element = document.getDocumentElement();
        return (Response) this.responseUnmarshaller.unmarshall(element);
    } catch (Exception ex) {
        throw createAuthenticationException(Saml2ErrorCodes.MALFORMED_RESPONSE_DATA, ex.getMessage(), ex);
    }
}
Also used : Response(org.opensaml.saml.saml2.core.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) AuthenticationException(org.springframework.security.core.AuthenticationException) Saml2Exception(org.springframework.security.saml2.Saml2Exception)

Aggregations

Response (org.opensaml.saml.saml2.core.Response)4 AuthenticationException (org.springframework.security.core.AuthenticationException)4 Saml2Exception (org.springframework.security.saml2.Saml2Exception)4 Saml2AuthenticationException (org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 XSString (org.opensaml.core.xml.schema.XSString)2 AssertionValidationException (org.opensaml.saml.common.assertion.AssertionValidationException)2 Saml2Error (org.springframework.security.saml2.core.Saml2Error)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Inflater (java.util.zip.Inflater)1 InflaterOutputStream (java.util.zip.InflaterOutputStream)1 Test (org.junit.jupiter.api.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)1