-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Client credentials not correctly encoded in Basic Auth #9610
Comments
Thanks for reaching out @DrZ7. |
This ticket is the counterpart to spring-attic/spring-security-oauth#1826... stack with special character password and without encoding:
|
I believe this can be addressed by supplying a custom headers converter: @Bean
public OAuth2AuthorizedClientManager authorizedClientManager(
ClientRegistrationRepository registrations,
OAuth2AuthorizedClientRepository clients) {
Converter<OAuth2ClientCredentialsGrantRequest, HttpHeaders> headersConverter = (request) -> {
ClientRegistration registration = request.getClientRegistration();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.setBasicAuth(
URLEncoder.encode(registration.getClientId(), StandardCharsets.UTF_8),
URLEncoder.encode(registration.getClientSecret(), StandardCharsets.UTF_8));
return headers;
};
OAuth2ClientCredentialsGrantRequestEntityConverter entityConverter =
new OAuth2ClientCredentialsGrantRequestEntityConverter();
entityConverter.setHeadersConverter(headersConverter);
DefaultClientCredentialsTokenResponseClient client =
new DefaultClientCredentialsTokenResponseClient();
client.setRequestEntityConverter(entityConverter);
OAuth2AuthorizedClientProvider authorizedClientProvider =
OAuth2AuthorizedClientProviderBuilder.builder()
.clientCredentials((clientCredentials) -> clientCredentials
.accessTokenResponseClient(client))
.build();
DefaultOAuth2AuthorizedClientManager authorizedClientManager =
new DefaultOAuth2AuthorizedClientManager(registrations, clients);
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
return authorizedClientManager;
} Perhaps @jgrandja can provide a simpler configuration. The spec does seem to indicate that URL encoding should be performed by default. If so, then applications using a non-compliant provider would need to provide a headers converter instead. @jgrandja do you think the default client behavior should change? This question has cropped up on the authorization server side of things in a couple of different places recently. |
@DrZ7 The configuration that @jzheaux provided is correct and will work for the compliant provider you are using. I'm assuming you already applied a similar custom configuration and it's working? I agree that Therefore, I've scheduled this fix for Spring Security 6.0 (Update: Spring Security 5.6). |
At the moment we are using our own OAuth2ClientCredentialsGrantRequestEntityConverter b/c setHeadersConverter is not yet available. |
@DrZ7 Regarding my last comment:
We're going to apply this fix in 5.5 since this is a bug. We'll come up with a strategy that does not break existing applications. |
Hello together, |
Hi @AT92. Can you describe what you mean by this? Are you saying it did not encode correctly, or it is now encoding and you don't want it to? |
Hi, we're facing the same. After updating to 5.6.0 credentials are being encoded again and we don't want it to (since the do contain some special characters) |
@rynoj that is correct. See this comment on #10018. This change is permanent starting with 5.6. See this comment if you need help working around the issue with a non-compliant provider. Also note that if you're using reactive, we've merged a change for #10130 which simplifies customizing headers beyond what the stackoverflow post linked above demonstrates. |
Ah thanks @sjohnr missed those comments! I'll have a look at the provided workarounds. |
Summary
OAuth2AuthorizationGrantRequestEntityUtils.getTokenRequestHeaders does not work properly if client credentials contain special characters. From RFC 6749:
Actual Behavior
The client with client name or password containing special characters cannot login. The provider returns exception.
Expected Behavior
The client with client name or password containing special characters can be authenticated.
Configuration Sample
spring.security.oauth2.client.registration.sth.client-secret = sthUI=+2~fubar
Where
org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationGrantRequestEntityUtils.getTokenRequestHeaders(ClientRegistration)
Related
This is related to spring-attic/spring-security-oauth#1826
The text was updated successfully, but these errors were encountered: