spring-bootspring-security-oauth2auth0

Spring Boot add additional attribute to WebClient request in ServerOAuth2AuthorizedClientExchangeFilterFunction


I am trying to implement the client_credentials grant to get a token in my spring boot resource server. I am using Auth0 as an Authorization server. They seem to require an extra parameter in the request body to be added called audience.

I have tried to do the request through postman and it works. I am now trying to reproduce it within Spring. Here is the working postman request

curl -X POST \
  https://XXX.auth0.com/oauth/token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'grant_type=client_credentials&audience=https%3A%2F%2Fxxxxx.auth0.com%2Fapi%2Fv2%2F&client_id=SOME_CLIENT_ID&client_secret=SOME_CLIENT_SECRET'

The problem I am facing is that i have no way to add the missing audience parameter to the token request.

I have a configuration defined in my application.yml

client:
    provider:
      auth0:
        issuer-uri: https://XXXX.auth0.com//
    registration:
      auth0-client:
        provider: auth0
        client-id: Client
        client-secret: Secret
        authorization_grant_type: client_credentials
      auth0:
        client-id: Client
        client-secret: Secret

I have the web client filter configured like this.

@Bean
WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations,
                    ServerOAuth2AuthorizedClientRepository authorizedClients) {
    ServerOAuth2AuthorizedClientExchangeFilterFunction oauth2 = new ServerOAuth2AuthorizedClientExchangeFilterFunction(
            clientRegistrations, authorizedClients);
    oauth2.setDefaultClientRegistrationId("auth0");
    return WebClient.builder()
            .filter(oauth2)
            .build();
}

I am injecting the instance and trying to do a request to get the user by email

 return this.webClient.get()
            .uri(this.usersUrl + "/api/v2/users-by-email?email={email}", email)
            .attributes(auth0ClientCredentials())
            .retrieve()
            .bodyToMono(User.class);

The way i understand it, the filter intercepts this userByEmail request and before it executes it it tries to execute the /oauth/token request to get JWT Bearer token which it can append to the first one and execute it.

Is there a way to add a parameter to the filter? It has been extremely difficult to step through it and figure out where exactly the parameters are being appended since its reactive and am quite new at this. Even some pointers to where to look would be helpful.


Solution

  • Here is what i found out after further investigation. The code described in my question was never going to call the client_credentials and fit my use-case. I think (not 100% sure on this) it will be very useful in the future if i am trying to propagate the user submitted token around multiple services in a micro-service architecture. A chain of actions like this comes to mind:

    User calls Service A -> Service A calls Service B -> Service B responds -> Service A responds back to user request.

    And using the same token to begin with through the whole process.

    My solution to my use-case:

    What i did was create a new Filter class largely based on the original and implement a step before executing the request where i check if i have a JWT token stored that can be used for the Auth0 Management API. If i don't i build up the client_credentials grant request and get one, then attach this token as a bearer to the initial request and execute that one. I also added a small token in-memory caching mechanism so that if the token is valid any other requests at a later date will just use it. Here is my code.

    Filter

    public class Auth0ClientCredentialsGrantFilterFunction implements ExchangeFilterFunction {
    
        private ReactiveClientRegistrationRepository clientRegistrationRepository;
    
        /**
         * Required by auth0 when requesting a client credentials token
         */
        private String audience;
    
        private String clientRegistrationId;
    
        private Auth0InMemoryAccessTokenStore auth0InMemoryAccessTokenStore;
    
        public Auth0ClientCredentialsGrantFilterFunction(ReactiveClientRegistrationRepository clientRegistrationRepository,
                                                         String clientRegistrationId,
                                                         String audience) {
            this.clientRegistrationRepository = clientRegistrationRepository;
            this.audience = audience;
            this.clientRegistrationId = clientRegistrationId;
            this.auth0InMemoryAccessTokenStore = new Auth0InMemoryAccessTokenStore();
        }
    
        public void setAuth0InMemoryAccessTokenStore(Auth0InMemoryAccessTokenStore auth0InMemoryAccessTokenStore) {
            this.auth0InMemoryAccessTokenStore = auth0InMemoryAccessTokenStore;
        }
    
        @Override
        public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
            return auth0ClientCredentialsToken(next)
                    .map(token -> bearer(request, token.getTokenValue()))
                    .flatMap(next::exchange)
                    .switchIfEmpty(next.exchange(request));
        }
    
        private Mono<OAuth2AccessToken> auth0ClientCredentialsToken(ExchangeFunction next) {
            return Mono.defer(this::loadClientRegistration)
                    .map(clientRegistration -> new ClientCredentialsRequest(clientRegistration, audience))
                    .flatMap(request -> this.auth0InMemoryAccessTokenStore.retrieveToken()
                            .switchIfEmpty(refreshAuth0Token(request, next)));
        }
    
        private Mono<OAuth2AccessToken> refreshAuth0Token(ClientCredentialsRequest clientCredentialsRequest, ExchangeFunction next) {
            ClientRegistration clientRegistration = clientCredentialsRequest.getClientRegistration();
            String tokenUri = clientRegistration
                    .getProviderDetails().getTokenUri();
            ClientRequest clientCredentialsTokenRequest = ClientRequest.create(HttpMethod.POST, URI.create(tokenUri))
                    .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
                    .body(clientCredentialsTokenBody(clientCredentialsRequest))
                    .build();
            return next.exchange(clientCredentialsTokenRequest)
                    .flatMap(response -> response.body(oauth2AccessTokenResponse()))
                    .map(OAuth2AccessTokenResponse::getAccessToken)
                    .doOnNext(token -> this.auth0InMemoryAccessTokenStore.storeToken(token));
        }
    
        private static BodyInserters.FormInserter<String> clientCredentialsTokenBody(ClientCredentialsRequest clientCredentialsRequest) {
            ClientRegistration clientRegistration = clientCredentialsRequest.getClientRegistration();
            return BodyInserters
                    .fromFormData("grant_type", AuthorizationGrantType.CLIENT_CREDENTIALS.getValue())
                    .with("client_id", clientRegistration.getClientId())
                    .with("client_secret", clientRegistration.getClientSecret())
                    .with("audience", clientCredentialsRequest.getAudience());
        }
    
        private Mono<ClientRegistration> loadClientRegistration() {
            return Mono.just(clientRegistrationId)
                    .flatMap(r -> clientRegistrationRepository.findByRegistrationId(r));
        }
    
        private ClientRequest bearer(ClientRequest request, String token) {
            return ClientRequest.from(request)
                    .headers(headers -> headers.setBearerAuth(token))
                    .build();
        }
    
    
        static class ClientCredentialsRequest {
            private final ClientRegistration clientRegistration;
            private final String audience;
    
            public ClientCredentialsRequest(ClientRegistration clientRegistration, String audience) {
                this.clientRegistration = clientRegistration;
                this.audience = audience;
            }
    
            public ClientRegistration getClientRegistration() {
                return clientRegistration;
            }
    
            public String getAudience() {
                return audience;
            }
        }
    
    }
    

    Token Store

    public class Auth0InMemoryAccessTokenStore implements ReactiveInMemoryAccessTokenStore {
    
        private AtomicReference<OAuth2AccessToken> token = new AtomicReference<>();
        private Clock clock = Clock.systemUTC();
        private Duration accessTokenExpiresSkew = Duration.ofMinutes(1);
    
        public Auth0InMemoryAccessTokenStore() {
        }
    
        @Override
        public Mono<OAuth2AccessToken> retrieveToken() {
            return Mono.justOrEmpty(token.get())
                    .filter(Objects::nonNull)
                    .filter(token -> token.getExpiresAt() != null)
                    .filter(token -> {
                        Instant now = this.clock.instant();
                        Instant expiresAt = token.getExpiresAt();
                        if (now.isBefore(expiresAt.minus(this.accessTokenExpiresSkew))) {
                            return true;
                        }
                        return false;
                    });
        }
    
        @Override
        public Mono<Void> storeToken(OAuth2AccessToken token) {
            this.token.set(token);
            return Mono.empty();
        }
    }
    

    Token Store Interface

    public interface ReactiveInMemoryAccessTokenStore {
        Mono<OAuth2AccessToken> retrieveToken();
    
        Mono<Void> storeToken(OAuth2AccessToken token);
    }
    

    And finally defining the beans and using it.

        @Bean
        public Auth0ClientCredentialsGrantFilterFunction auth0FilterFunction(ReactiveClientRegistrationRepository clientRegistrations,
                                                                             @Value("${auth0.client-registration-id}") String clientRegistrationId,
                                                                             @Value("${auth0.audience}") String audience) {
            return new Auth0ClientCredentialsGrantFilterFunction(clientRegistrations, clientRegistrationId, audience);
        }
    
        @Bean(name = "auth0-webclient")
        WebClient webClient(Auth0ClientCredentialsGrantFilterFunction filter) {
            return WebClient.builder()
                    .filter(filter)
                    .build();
        }
    

    There is a slight problem with the token store at this time as the client_credentials token request will be executed multiple on parallel requests that come at the same time, but i can live with that for the foreseeable future.