1

I'm currently migrating from Spring Security SAML Extension to Spring Security SAML2 and use case requires language code to be sent in Extensions -element.

With Spring Security SAML Extension this was done by:

  1. Extending SAMLEntryPoint and storing locale as relayState to SAMLMessageContext like this:
public class CustomSAMLEntryPoint extends SAMLEntryPoint {

    private String relayState;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException {
        //read your request parameter
        setRelayState(request.getParameter("locale"));
        super.commence(request, response, authenticationException);
    }

    @Override
    protected WebSSOProfileOptions getProfileOptions(SAMLMessageContext samlMessageContext, AuthenticationException authenticationException) throws MetadataProviderException {
        //set the relayState to your SAML message context
        samlMessageContext.setRelayState(getRelayState());
        return super.getProfileOptions(samlMessageContext, authenticationException);
    }

    private void setRelayState(String relayState) {
        this.relayState = relayState;
    }

    private String getRelayState() {
        return relayState;
    }
}
  1. Extending WebSSOProfileImpl and using previously set relayState value to generate Extensions -element:
public class CustomWebSSOProfileImpl extends WebSSOProfileImpl {
    
    @Override
    protected AuthnRequest getAuthnRequest(SAMLMessageContext context, WebSSOProfileOptions options, AssertionConsumerService assertionConsumer, SingleSignOnService bindingService) throws SAMLException, MetadataProviderException {
        AuthnRequest authnRequest = super.getAuthnRequest(context, options, assertionConsumer, bindingService);
        authnRequest.setExtensions(buildExtensions(context.getRelayState()));
        return authnRequest;
    }
}

How could this same functionality be done with Spring Security Core SAML2? Is there some similar way than using SAMLMessageContext and relayState?

I could customize AuthenticationEntryPoint as well as authentication request creation but there seems to be no way to move locale between these two.

     public AuthenticationEntryPoint authenticationEntryPoint() {
        final AuthenticationEntryPoint authenticationEntryPoint = new LoginUrlAuthenticationEntryPoint(
                "/saml2/authenticate/sp");
        return (request, response, exception) -> {
            String locale = request.getParameter("locale");
            // Where shoud locale be stored???
            authenticationEntryPoint.commence(request, response, exception);
        };
    }
    @Bean
    public Saml2AuthenticationRequestFactory authenticationRequestFactory() {
        final OpenSamlAuthenticationRequestFactory authenticationRequestFactory = new OpenSamlAuthenticationRequestFactory();
        authenticationRequestFactory.setAuthenticationRequestContextConverter(context -> {
            final AuthnRequest request = new AuthnRequestBuilder().buildObject();
            request.setAssertionConsumerServiceURL(context.getAssertionConsumerServiceUrl());
            request.setDestination(context.getDestination());
            request.setID("A" + UUID.randomUUID());
            request.setIssueInstant(new DateTime());
            final Issuer issuer = new IssuerBuilder().buildObject();
            issuer.setValue(context.getIssuer());
            request.setIssuer(issuer);
            // Where can locale be read from???
            request.setExtensions(buildLanguageExtensions(???);
            return request;
        });
        return authenticationRequestFactory;
    }
KTJ
  • 11
  • 2

0 Answers0