Skip to content

Commit f55247e

Browse files
author
Steve Riesenberg
committed
Revert "URL encode client credentials"
This reverts commit 6cafa48. Issue gh-9610 gh-9862 Closes gh-10018
1 parent 1cf377c commit f55247e

File tree

4 files changed

+11
-116
lines changed

4 files changed

+11
-116
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/endpoint/AbstractWebClientReactiveOAuth2AccessTokenResponseClient.java

+6-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,14 +15,6 @@
1515
*/
1616
package org.springframework.security.oauth2.client.endpoint;
1717

18-
import java.io.UnsupportedEncodingException;
19-
import java.net.URLEncoder;
20-
import java.nio.charset.StandardCharsets;
21-
import java.util.Collections;
22-
import java.util.Set;
23-
24-
import reactor.core.publisher.Mono;
25-
2618
import org.springframework.http.HttpHeaders;
2719
import org.springframework.http.MediaType;
2820
import org.springframework.security.oauth2.client.registration.ClientRegistration;
@@ -35,6 +27,10 @@
3527
import org.springframework.web.reactive.function.BodyInserters;
3628
import org.springframework.web.reactive.function.client.ClientResponse;
3729
import org.springframework.web.reactive.function.client.WebClient;
30+
import reactor.core.publisher.Mono;
31+
32+
import java.util.Collections;
33+
import java.util.Set;
3834

3935
import static org.springframework.security.oauth2.core.web.reactive.function.OAuth2BodyExtractors.oauth2AccessTokenResponse;
4036

@@ -90,19 +86,7 @@ private void populateTokenRequestHeaders(T grantRequest, HttpHeaders headers) {
9086
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
9187
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
9288
if (ClientAuthenticationMethod.BASIC.equals(clientRegistration.getClientAuthenticationMethod())) {
93-
String clientId = encodeClientCredential(clientRegistration.getClientId());
94-
String clientSecret = encodeClientCredential(clientRegistration.getClientSecret());
95-
headers.setBasicAuth(clientId, clientSecret);
96-
}
97-
}
98-
99-
private static String encodeClientCredential(String clientCredential) {
100-
try {
101-
return URLEncoder.encode(clientCredential, StandardCharsets.UTF_8.toString());
102-
}
103-
catch (UnsupportedEncodingException ex) {
104-
// Will not happen since UTF-8 is a standard charset
105-
throw new IllegalArgumentException(ex);
89+
headers.setBasicAuth(clientRegistration.getClientId(), clientRegistration.getClientSecret());
10690
}
10791
}
10892

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/endpoint/OAuth2AuthorizationGrantRequestEntityUtils.java

+4-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,18 +15,15 @@
1515
*/
1616
package org.springframework.security.oauth2.client.endpoint;
1717

18-
import java.io.UnsupportedEncodingException;
19-
import java.net.URLEncoder;
20-
import java.nio.charset.StandardCharsets;
21-
import java.util.Collections;
22-
2318
import org.springframework.core.convert.converter.Converter;
2419
import org.springframework.http.HttpHeaders;
2520
import org.springframework.http.MediaType;
2621
import org.springframework.http.RequestEntity;
2722
import org.springframework.security.oauth2.client.registration.ClientRegistration;
2823
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
2924

25+
import java.util.Collections;
26+
3027
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE;
3128

3229
/**
@@ -47,23 +44,11 @@ static HttpHeaders getTokenRequestHeaders(ClientRegistration clientRegistration)
4744
HttpHeaders headers = new HttpHeaders();
4845
headers.addAll(DEFAULT_TOKEN_REQUEST_HEADERS);
4946
if (ClientAuthenticationMethod.BASIC.equals(clientRegistration.getClientAuthenticationMethod())) {
50-
String clientId = encodeClientCredential(clientRegistration.getClientId());
51-
String clientSecret = encodeClientCredential(clientRegistration.getClientSecret());
52-
headers.setBasicAuth(clientId, clientSecret);
47+
headers.setBasicAuth(clientRegistration.getClientId(), clientRegistration.getClientSecret());
5348
}
5449
return headers;
5550
}
5651

57-
private static String encodeClientCredential(String clientCredential) {
58-
try {
59-
return URLEncoder.encode(clientCredential, StandardCharsets.UTF_8.toString());
60-
}
61-
catch (UnsupportedEncodingException ex) {
62-
// Will not happen since UTF-8 is a standard charset
63-
throw new IllegalArgumentException(ex);
64-
}
65-
}
66-
6752
private static HttpHeaders getDefaultTokenRequestHeaders() {
6853
HttpHeaders headers = new HttpHeaders();
6954
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/OAuth2ClientCredentialsGrantRequestEntityConverterTests.java

-40
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,13 @@
1515
*/
1616
package org.springframework.security.oauth2.client.endpoint;
1717

18-
import java.io.UnsupportedEncodingException;
19-
import java.net.URLEncoder;
20-
import java.nio.charset.StandardCharsets;
21-
import java.util.Base64;
22-
2318
import org.junit.Before;
2419
import org.junit.Test;
25-
2620
import org.springframework.http.HttpHeaders;
2721
import org.springframework.http.HttpMethod;
2822
import org.springframework.http.MediaType;
2923
import org.springframework.http.RequestEntity;
3024
import org.springframework.security.oauth2.client.registration.ClientRegistration;
31-
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
3225
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3326
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
3427
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
@@ -81,37 +74,4 @@ public void convertWhenGrantRequestValidThenConverts() {
8174
AuthorizationGrantType.CLIENT_CREDENTIALS.getValue());
8275
assertThat(formParameters.getFirst(OAuth2ParameterNames.SCOPE)).isEqualTo("read write");
8376
}
84-
85-
// gh-9610
86-
@SuppressWarnings("unchecked")
87-
@Test
88-
public void convertWhenSpecialCharactersThenConvertsWithEncodedClientCredentials()
89-
throws UnsupportedEncodingException {
90-
String clientCredentialWithAnsiKeyboardSpecialCharacters = "~!@#$%^&*()_+{}|:\"<>?`-=[]\\;',./ ";
91-
// @formatter:off
92-
ClientRegistration clientRegistration = TestClientRegistrations.clientCredentials()
93-
.clientId(clientCredentialWithAnsiKeyboardSpecialCharacters)
94-
.clientSecret(clientCredentialWithAnsiKeyboardSpecialCharacters)
95-
.build();
96-
// @formatter:on
97-
OAuth2ClientCredentialsGrantRequest clientCredentialsGrantRequest = new OAuth2ClientCredentialsGrantRequest(
98-
clientRegistration);
99-
RequestEntity<?> requestEntity = this.converter.convert(clientCredentialsGrantRequest);
100-
assertThat(requestEntity.getMethod()).isEqualTo(HttpMethod.POST);
101-
assertThat(requestEntity.getUrl().toASCIIString())
102-
.isEqualTo(clientRegistration.getProviderDetails().getTokenUri());
103-
HttpHeaders headers = requestEntity.getHeaders();
104-
assertThat(headers.getAccept()).contains(MediaType.APPLICATION_JSON_UTF8);
105-
assertThat(headers.getContentType())
106-
.isEqualTo(MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8"));
107-
String urlEncodedClientCredential = URLEncoder.encode(clientCredentialWithAnsiKeyboardSpecialCharacters,
108-
StandardCharsets.UTF_8.toString());
109-
String clientCredentials = Base64.getEncoder().encodeToString(
110-
(urlEncodedClientCredential + ":" + urlEncodedClientCredential).getBytes(StandardCharsets.UTF_8));
111-
assertThat(headers.getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Basic " + clientCredentials);
112-
MultiValueMap<String, String> formParameters = (MultiValueMap<String, String>) requestEntity.getBody();
113-
assertThat(formParameters.getFirst(OAuth2ParameterNames.GRANT_TYPE))
114-
.isEqualTo(AuthorizationGrantType.CLIENT_CREDENTIALS.getValue());
115-
assertThat(formParameters.getFirst(OAuth2ParameterNames.SCOPE)).contains(clientRegistration.getScopes());
116-
}
11777
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/WebClientReactiveClientCredentialsTokenResponseClientTests.java

+1-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,17 +16,12 @@
1616

1717
package org.springframework.security.oauth2.client.endpoint;
1818

19-
import java.net.URLEncoder;
20-
import java.nio.charset.StandardCharsets;
21-
import java.util.Base64;
22-
2319
import okhttp3.mockwebserver.MockResponse;
2420
import okhttp3.mockwebserver.MockWebServer;
2521
import okhttp3.mockwebserver.RecordedRequest;
2622
import org.junit.After;
2723
import org.junit.Before;
2824
import org.junit.Test;
29-
3025
import org.springframework.http.HttpHeaders;
3126
import org.springframework.http.MediaType;
3227
import org.springframework.security.oauth2.client.registration.ClientRegistration;
@@ -93,35 +88,6 @@ public void getTokenResponseWhenHeaderThenSuccess() throws Exception {
9388
assertThat(body).isEqualTo("grant_type=client_credentials&scope=read%3Auser");
9489
}
9590

96-
// gh-9610
97-
@Test
98-
public void getTokenResponseWhenSpecialCharactersThenSuccessWithEncodedClientCredentials() throws Exception {
99-
// @formatter:off
100-
enqueueJson("{\n"
101-
+ " \"access_token\":\"MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3\",\n"
102-
+ " \"token_type\":\"bearer\",\n"
103-
+ " \"expires_in\":3600,\n"
104-
+ " \"refresh_token\":\"IwOGYzYTlmM2YxOTQ5MGE3YmNmMDFkNTVk\",\n"
105-
+ " \"scope\":\"create\"\n"
106-
+ "}");
107-
// @formatter:on
108-
String clientCredentialWithAnsiKeyboardSpecialCharacters = "~!@#$%^&*()_+{}|:\"<>?`-=[]\\;',./ ";
109-
OAuth2ClientCredentialsGrantRequest request = new OAuth2ClientCredentialsGrantRequest(
110-
this.clientRegistration.clientId(clientCredentialWithAnsiKeyboardSpecialCharacters)
111-
.clientSecret(clientCredentialWithAnsiKeyboardSpecialCharacters).build());
112-
OAuth2AccessTokenResponse response = this.client.getTokenResponse(request).block();
113-
RecordedRequest actualRequest = this.server.takeRequest();
114-
String body = actualRequest.getBody().readUtf8();
115-
assertThat(response.getAccessToken()).isNotNull();
116-
String urlEncodedClientCredentialecret = URLEncoder.encode(clientCredentialWithAnsiKeyboardSpecialCharacters,
117-
StandardCharsets.UTF_8.toString());
118-
String clientCredentials = Base64.getEncoder()
119-
.encodeToString((urlEncodedClientCredentialecret + ":" + urlEncodedClientCredentialecret)
120-
.getBytes(StandardCharsets.UTF_8));
121-
assertThat(actualRequest.getHeader(HttpHeaders.AUTHORIZATION)).isEqualTo("Basic " + clientCredentials);
122-
assertThat(body).isEqualTo("grant_type=client_credentials&scope=read%3Auser");
123-
}
124-
12591
@Test
12692
public void getTokenResponseWhenPostThenSuccess() throws Exception {
12793
ClientRegistration registration = this.clientRegistration

0 commit comments

Comments
 (0)