Search code examples
javaspring-bootauth0

Spring Boot/Auth0 - How do I specify the connection?


I am working on setting up an application using Spring Boot and Auth0. We are refactoring from a legacy codebase to use Spring Boot. In the legacy code, the Auth0 URL is created manually by appending the URL parameters:

https://[removed].auth0.com/authorize?
    response_type=code
    &client_id=[removed]
    &scope=openid email profile
    &connection=[removed]
    &state=[removed]
    &redirect_uri=http://localhost:8081/login/oauth2/code/auth0

With the Spring Boot configuration (guide here: https://auth0.com/docs/quickstart/webapp/java-spring-boot/01-login), this is the URL that generates:

https://[removed].auth0.com/authorize?
    response_type=code
    &client_id=[removed]
    &scope=openid email profile
    &state=[removed]
    &redirect_uri=http://localhost:8081/login/oauth2/code/auth0

The Spring Boot URL is giving me an error "[invalid_request] no connections enabled for the client".

I am missing the "connection" parameter with the Spring Boot setup. I have tested by manually copying the URL and adding the "connection" parameter and I get the login page. Without it, I get the error.

On Spring's configuration page (https://docs.spring.io/spring-security/reference/servlet/oauth2/login/core.html#oauth2login-boot-property-mappings), I don't see an option for Connection. I didn't see anything on the SecurityFilterChain that would allow me to change this either.

I see that Auth0.js has a function that allows a "connection" parameter (https://auth0.com/docs/libraries/auth0js). How do I add this using Spring Boot/Java?

EDIT

application.properties:

spring.security.oauth2.client.registration.auth0.client-id=[removed]
spring.security.oauth2.client.registration.auth0.client-secret=[removed]
spring.security.oauth2.client.registration.auth0.scope[0]=openid
spring.security.oauth2.client.registration.auth0.scope[1]=email
spring.security.oauth2.client.registration.auth0.scope[2]=profile
spring.security.oauth2.client.provider.auth0.issuer-uri=[removed]

EDIT 2

We were working in conjunction with Auth0 Support - they provided us the following information:

In case an Enterprise connection is the only enabled connection for an application and the "connection" parameter is not specified on the /authorize request, you need to enable the "show as a button" setting on that enterprise connection, otherwise you will get "no connections enabled for the client" error.

The "Display connection as a button" checkbox is on the "Login Experience" tab of the connection setting page.

Weird configuration requirement - you can't go directly to the login page. You have to have a button to take you there. This did resolve the original issue; however, I marked @Codo answer below as accepted, as it did answer this question and appears it would work from initial testing.


Solution

  • You are looking for a way to add an additional parameter to the authorization URI. It's isn't as straightforward as one would like but doable.

    Fortunately, it's described in Customizing Authorization and Token Requests with Spring Security 5.1 Client.

    You probably want to implement the steps 2 and 4:

    • Add your own implementation of OAuth2AuthorizationRequestResolver, override both resolve() methods to call customizeAuthorizationRequest()
    • Implement customizeAuthorizationRequest() to add the additional connection parameter (OAuth2AuthorizationRequest already support additional parameters)
    • Implement a security configuration class to register CustomAuthorizationRequestResolver as the authorization request resolver

    Several issues on GitHub ask for a simpler way. But the issues are still open (or closed as duplicates).

    Update

    Instead of clientRegistrationRepository() (at the end of step 2), you could declare clientRegistrationRepository as an injected dependency and the use it without parentheses:

    @Autowired
    private ClientRegistrationRepository clientRegistrationRepository;