Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement request] [spring-boot-starter-oauth2-client] ability to change the key "authorization-grant-type" #16372

Closed
patpatpat123 opened this issue Jan 8, 2025 · 0 comments
Assignees
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) status: invalid An issue that we don't feel is valid type: enhancement A general enhancement

Comments

@patpatpat123
Copy link

patpatpat123 commented Jan 8, 2025

Context

What are you trying to accomplish?

I am trying to use spring-boot-starter-oauth2-client to get a bearer token, where the service is accepting grant_type=client_credentials and not authorization-grant-type.

What other alternatives have you considered?
Are you aware of any workarounds?

We are authenticating against a third-party service that will provide the token.

if we do this:

String authStr = "theusername:thepassword";
        String base64Creds = Base64.getEncoder().encodeToString(authStr.getBytes());

        final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("Authorization", "Basic " + base64Creds);

        Consumer<HttpHeaders> consumer = it -> it.addAll(params);
        Map tokenMap = restClient.post()
                .uri("https://thirdpartyservice.com/token?scope=resolve+download&grant_type=client_credentials")
                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                .headers(consumer)
                .retrieve()
                .body(Map.class);
        System.out.println(tokenMap.get("access_token"));
curl -s -X POST -H "Content-Type: application/x-www-form-urlencoded" -u theusername:thepassword  "https://thirdpartyservice.com/token?scope=resolve+download&grant_type=client_credentials" | jq -r '.access_token'

Above would work and give up the correct token, which we can use to request a resource server.

We saw Dan Vega's cool video https://www.youtube.com/watch?v=nFKcJDpUuZ8 and wanted to try spring-boot-starter-oauth2-client.

We following his example, we configured as follow:

@Configuration
public class RestClientConfig {

    @Bean
    public RestClient restClient(OAuth2AuthorizedClientManager authorizedClientManager) {
        OAuth2ClientHttpRequestInterceptor interceptor = new OAuth2ClientHttpRequestInterceptor(authorizedClientManager);
        return RestClient.builder()
                .requestInterceptor(interceptor)
                .build();
    }

}
return restClient.get()
                .uri("https://resource...")
                .attributes(clientRegistrationId("my-client"))
                .retrieve()
                .body(String.class);
spring:
  application:
    name: client-application
  security:
    oauth2:
      client:
        registration:
          my-client:
            provider: the-provider
            client-id: theusername
            client-secret: thepassword
            authorization-grant-type: client_credentials
            #grant_type: client_credentials
            scope: resolve+download
        provider:
          the-provider:
            token-uri: https://thirdpartyservice.com/token
logging:
  level:
    root: DEBUG

Expected Behavior

We expected this would work

Current Behavior

Unfortunately, this is not working, as the token provider returns 400 error, as it is expecting grant_type.

We tried to replace configuration with "grant_type: client_credentials" but would get Caused by: java.lang.IllegalArgumentException: authorizationGrantType cannot be null

Question: Would it be possible to enhance spring-boot-starter-oauth2-client to change the key authorization-grant-type to grant_type?

Thank you for you time.

@patpatpat123 patpatpat123 added status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels Jan 8, 2025
@sjohnr sjohnr added status: invalid An issue that we don't feel is valid in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 9, 2025
@sjohnr sjohnr self-assigned this Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) status: invalid An issue that we don't feel is valid type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants