|
16 | 16 |
|
17 | 17 | package org.springframework.security.oauth2.client.endpoint;
|
18 | 18 |
|
| 19 | +import java.net.URLEncoder; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.util.List; |
| 22 | + |
19 | 23 | import org.springframework.core.convert.converter.Converter;
|
20 | 24 | import org.springframework.http.HttpHeaders;
|
21 | 25 | import org.springframework.http.MediaType;
|
22 | 26 | import org.springframework.http.RequestEntity;
|
23 | 27 | import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
24 | 28 | import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
|
25 | 29 |
|
26 |
| -import java.net.URLEncoder; |
27 |
| -import java.nio.charset.StandardCharsets; |
28 |
| -import java.util.Collections; |
29 |
| - |
30 | 30 | /**
|
31 | 31 | * Default {@link Converter} used to convert an
|
32 |
| - * {@link AbstractOAuth2AuthorizationGrantRequest} to the {@link HttpHeaders} of aKk |
| 32 | + * {@link AbstractOAuth2AuthorizationGrantRequest} to the {@link HttpHeaders} of a |
33 | 33 | * {@link RequestEntity} representation of an OAuth 2.0 Access Token Request for the
|
34 | 34 | * specific Authorization Grant.
|
35 | 35 | *
|
36 | 36 | * @author Peter Eastham
|
37 |
| - * @author Joe Grandja |
38 |
| - * @see AbstractOAuth2AuthorizationGrantRequestEntityConverter |
| 37 | + * @author Steve Riesenberg |
39 | 38 | * @since 6.3
|
| 39 | + * @see AbstractOAuth2AuthorizationGrantRequestEntityConverter |
40 | 40 | */
|
41 | 41 | public final class DefaultOAuth2TokenRequestHeadersConverter<T extends AbstractOAuth2AuthorizationGrantRequest>
|
42 | 42 | implements Converter<T, HttpHeaders> {
|
43 | 43 |
|
44 |
| - private MediaType accept = MediaType.APPLICATION_JSON; |
| 44 | + private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON, |
| 45 | + StandardCharsets.UTF_8); |
| 46 | + |
| 47 | + private static final MediaType APPLICATION_FORM_URLENCODED_UTF8 = new MediaType( |
| 48 | + MediaType.APPLICATION_FORM_URLENCODED, StandardCharsets.UTF_8); |
| 49 | + |
| 50 | + private List<MediaType> accept = List.of(MediaType.APPLICATION_JSON); |
45 | 51 |
|
46 | 52 | private MediaType contentType = MediaType.APPLICATION_FORM_URLENCODED;
|
47 | 53 |
|
48 |
| - private boolean encodeClientCredentialsIfRequired = true; |
| 54 | + private boolean encodeClientCredentials = true; |
49 | 55 |
|
50 | 56 | /**
|
51 |
| - * Populates the headers for the token request. |
52 |
| - * @param grantRequest the grant request |
| 57 | + * Populates the default headers for the token request. |
| 58 | + * @param grantRequest the authorization grant request |
53 | 59 | * @return the headers populated for the token request
|
54 | 60 | */
|
55 | 61 | @Override
|
56 | 62 | public HttpHeaders convert(T grantRequest) {
|
57 | 63 | HttpHeaders headers = new HttpHeaders();
|
58 |
| - headers.setAccept(Collections.singletonList(accept)); |
59 |
| - headers.setContentType(contentType); |
| 64 | + headers.setAccept(this.accept); |
| 65 | + headers.setContentType(this.contentType); |
60 | 66 | ClientRegistration clientRegistration = grantRequest.getClientRegistration();
|
61 | 67 | if (ClientAuthenticationMethod.CLIENT_SECRET_BASIC.equals(clientRegistration.getClientAuthenticationMethod())) {
|
62 |
| - String clientId = encodeClientCredential(clientRegistration.getClientId()); |
63 |
| - String clientSecret = encodeClientCredential(clientRegistration.getClientSecret()); |
| 68 | + String clientId = encodeClientCredentialIfRequired(clientRegistration.getClientId()); |
| 69 | + String clientSecret = encodeClientCredentialIfRequired(clientRegistration.getClientSecret()); |
64 | 70 | headers.setBasicAuth(clientId, clientSecret);
|
65 | 71 | }
|
66 | 72 | return headers;
|
67 | 73 | }
|
68 | 74 |
|
69 |
| - private String encodeClientCredential(String clientCredential) { |
70 |
| - String encodedCredential = clientCredential; |
71 |
| - if (this.encodeClientCredentialsIfRequired) { |
72 |
| - encodedCredential = URLEncoder.encode(clientCredential, StandardCharsets.UTF_8); |
| 75 | + private String encodeClientCredentialIfRequired(String clientCredential) { |
| 76 | + if (!this.encodeClientCredentials) { |
| 77 | + return clientCredential; |
73 | 78 | }
|
74 |
| - return encodedCredential; |
75 |
| - } |
76 |
| - |
77 |
| - /** |
78 |
| - * Sets the behavior for if this URL Encoding the Client Credentials during the |
79 |
| - * conversion. |
80 |
| - * @param encodeClientCredentialsIfRequired if false, no URL encoding will happen |
81 |
| - */ |
82 |
| - public void setEncodeClientCredentials(boolean encodeClientCredentialsIfRequired) { |
83 |
| - this.encodeClientCredentialsIfRequired = encodeClientCredentialsIfRequired; |
| 79 | + return URLEncoder.encode(clientCredential, StandardCharsets.UTF_8); |
84 | 80 | }
|
85 | 81 |
|
86 | 82 | /**
|
87 |
| - * MediaType to set for the Accept header. Default is application/json |
88 |
| - * @param accept MediaType to use for the Accept header |
| 83 | + * Sets whether the client credentials of the {@code Authorization} header will be |
| 84 | + * encoded using the {@code application/x-www-form-urlencoded} encoding algorithm |
| 85 | + * according to RFC 6749. Default is {@code true}. |
| 86 | + * @param encodeClientCredentials whether the client credentials will be encoded |
| 87 | + * @see <a target="_blank" href= |
| 88 | + * "https://datatracker.ietf.org/doc/html/rfc6749#section-2.3.1">2.3.1 Client |
| 89 | + * Password</a> |
89 | 90 | */
|
90 |
| - private void setAccept(MediaType accept) { |
91 |
| - this.accept = accept; |
| 91 | + public void setEncodeClientCredentials(boolean encodeClientCredentials) { |
| 92 | + this.encodeClientCredentials = encodeClientCredentials; |
92 | 93 | }
|
93 | 94 |
|
94 | 95 | /**
|
95 |
| - * MediaType to set for the Content Type header. Default is |
96 |
| - * application/x-www-form-urlencoded |
97 |
| - * @param contentType MediaType to use for the Content Type header |
| 96 | + * Creates a {@link DefaultOAuth2TokenRequestHeadersConverter} that populates default |
| 97 | + * {@link HttpHeaders} that includes {@code charset=UTF-8} on both the {@code Accept} |
| 98 | + * and {@code Content-Type} headers to provide backwards compatibility for |
| 99 | + * {@link AbstractOAuth2AuthorizationGrantRequestEntityConverter}. |
| 100 | + * @return the default headers converter |
98 | 101 | */
|
99 |
| - private void setContentType(MediaType contentType) { |
100 |
| - this.contentType = contentType; |
101 |
| - } |
102 |
| - |
103 |
| - static <T extends AbstractOAuth2AuthorizationGrantRequest> DefaultOAuth2TokenRequestHeadersConverter<T> historicalConverter() { |
| 102 | + static <T extends AbstractOAuth2AuthorizationGrantRequest> DefaultOAuth2TokenRequestHeadersConverter<T> withCharsetUtf8() { |
104 | 103 | DefaultOAuth2TokenRequestHeadersConverter<T> converter = new DefaultOAuth2TokenRequestHeadersConverter<>();
|
105 |
| - converter.setAccept(MediaType.APPLICATION_JSON_UTF8); |
106 |
| - converter.setContentType(MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8")); |
| 104 | + converter.accept = List.of(APPLICATION_JSON_UTF8); |
| 105 | + converter.contentType = APPLICATION_FORM_URLENCODED_UTF8; |
107 | 106 | return converter;
|
108 | 107 | }
|
109 | 108 |
|
|
0 commit comments