|
@@ -16,6 +16,10 @@
|
|
|
|
|
|
package org.springframework.security.oauth2.client.endpoint;
|
|
|
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
import org.springframework.core.convert.converter.Converter;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.MediaType;
|
|
@@ -23,87 +27,82 @@ import org.springframework.http.RequestEntity;
|
|
|
import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
|
|
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
|
|
|
|
|
|
-import java.net.URLEncoder;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.Collections;
|
|
|
-
|
|
|
/**
|
|
|
* Default {@link Converter} used to convert an
|
|
|
- * {@link AbstractOAuth2AuthorizationGrantRequest} to the {@link HttpHeaders} of aKk
|
|
|
+ * {@link AbstractOAuth2AuthorizationGrantRequest} to the {@link HttpHeaders} of a
|
|
|
* {@link RequestEntity} representation of an OAuth 2.0 Access Token Request for the
|
|
|
* specific Authorization Grant.
|
|
|
*
|
|
|
* @author Peter Eastham
|
|
|
- * @author Joe Grandja
|
|
|
- * @see AbstractOAuth2AuthorizationGrantRequestEntityConverter
|
|
|
+ * @author Steve Riesenberg
|
|
|
* @since 6.3
|
|
|
+ * @see AbstractOAuth2AuthorizationGrantRequestEntityConverter
|
|
|
*/
|
|
|
public final class DefaultOAuth2TokenRequestHeadersConverter<T extends AbstractOAuth2AuthorizationGrantRequest>
|
|
|
implements Converter<T, HttpHeaders> {
|
|
|
|
|
|
- private MediaType accept = MediaType.APPLICATION_JSON;
|
|
|
+ private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON,
|
|
|
+ StandardCharsets.UTF_8);
|
|
|
+
|
|
|
+ private static final MediaType APPLICATION_FORM_URLENCODED_UTF8 = new MediaType(
|
|
|
+ MediaType.APPLICATION_FORM_URLENCODED, StandardCharsets.UTF_8);
|
|
|
+
|
|
|
+ private List<MediaType> accept = List.of(MediaType.APPLICATION_JSON);
|
|
|
|
|
|
private MediaType contentType = MediaType.APPLICATION_FORM_URLENCODED;
|
|
|
|
|
|
- private boolean encodeClientCredentialsIfRequired = true;
|
|
|
+ private boolean encodeClientCredentials = true;
|
|
|
|
|
|
/**
|
|
|
- * Populates the headers for the token request.
|
|
|
- * @param grantRequest the grant request
|
|
|
+ * Populates the default headers for the token request.
|
|
|
+ * @param grantRequest the authorization grant request
|
|
|
* @return the headers populated for the token request
|
|
|
*/
|
|
|
@Override
|
|
|
public HttpHeaders convert(T grantRequest) {
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.setAccept(Collections.singletonList(accept));
|
|
|
- headers.setContentType(contentType);
|
|
|
+ headers.setAccept(this.accept);
|
|
|
+ headers.setContentType(this.contentType);
|
|
|
ClientRegistration clientRegistration = grantRequest.getClientRegistration();
|
|
|
if (ClientAuthenticationMethod.CLIENT_SECRET_BASIC.equals(clientRegistration.getClientAuthenticationMethod())) {
|
|
|
- String clientId = encodeClientCredential(clientRegistration.getClientId());
|
|
|
- String clientSecret = encodeClientCredential(clientRegistration.getClientSecret());
|
|
|
+ String clientId = encodeClientCredentialIfRequired(clientRegistration.getClientId());
|
|
|
+ String clientSecret = encodeClientCredentialIfRequired(clientRegistration.getClientSecret());
|
|
|
headers.setBasicAuth(clientId, clientSecret);
|
|
|
}
|
|
|
return headers;
|
|
|
}
|
|
|
|
|
|
- private String encodeClientCredential(String clientCredential) {
|
|
|
- String encodedCredential = clientCredential;
|
|
|
- if (this.encodeClientCredentialsIfRequired) {
|
|
|
- encodedCredential = URLEncoder.encode(clientCredential, StandardCharsets.UTF_8);
|
|
|
+ private String encodeClientCredentialIfRequired(String clientCredential) {
|
|
|
+ if (!this.encodeClientCredentials) {
|
|
|
+ return clientCredential;
|
|
|
}
|
|
|
- return encodedCredential;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Sets the behavior for if this URL Encoding the Client Credentials during the
|
|
|
- * conversion.
|
|
|
- * @param encodeClientCredentialsIfRequired if false, no URL encoding will happen
|
|
|
- */
|
|
|
- public void setEncodeClientCredentials(boolean encodeClientCredentialsIfRequired) {
|
|
|
- this.encodeClientCredentialsIfRequired = encodeClientCredentialsIfRequired;
|
|
|
+ return URLEncoder.encode(clientCredential, StandardCharsets.UTF_8);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * MediaType to set for the Accept header. Default is application/json
|
|
|
- * @param accept MediaType to use for the Accept header
|
|
|
+ * Sets whether the client credentials of the {@code Authorization} header will be
|
|
|
+ * encoded using the {@code application/x-www-form-urlencoded} encoding algorithm
|
|
|
+ * according to RFC 6749. Default is {@code true}.
|
|
|
+ * @param encodeClientCredentials whether the client credentials will be encoded
|
|
|
+ * @see <a target="_blank" href=
|
|
|
+ * "https://datatracker.ietf.org/doc/html/rfc6749#section-2.3.1">2.3.1 Client
|
|
|
+ * Password</a>
|
|
|
*/
|
|
|
- private void setAccept(MediaType accept) {
|
|
|
- this.accept = accept;
|
|
|
+ public void setEncodeClientCredentials(boolean encodeClientCredentials) {
|
|
|
+ this.encodeClientCredentials = encodeClientCredentials;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * MediaType to set for the Content Type header. Default is
|
|
|
- * application/x-www-form-urlencoded
|
|
|
- * @param contentType MediaType to use for the Content Type header
|
|
|
+ * Creates a {@link DefaultOAuth2TokenRequestHeadersConverter} that populates default
|
|
|
+ * {@link HttpHeaders} that includes {@code charset=UTF-8} on both the {@code Accept}
|
|
|
+ * and {@code Content-Type} headers to provide backwards compatibility for
|
|
|
+ * {@link AbstractOAuth2AuthorizationGrantRequestEntityConverter}.
|
|
|
+ * @return the default headers converter
|
|
|
*/
|
|
|
- private void setContentType(MediaType contentType) {
|
|
|
- this.contentType = contentType;
|
|
|
- }
|
|
|
-
|
|
|
- static <T extends AbstractOAuth2AuthorizationGrantRequest> DefaultOAuth2TokenRequestHeadersConverter<T> historicalConverter() {
|
|
|
+ static <T extends AbstractOAuth2AuthorizationGrantRequest> DefaultOAuth2TokenRequestHeadersConverter<T> withCharsetUtf8() {
|
|
|
DefaultOAuth2TokenRequestHeadersConverter<T> converter = new DefaultOAuth2TokenRequestHeadersConverter<>();
|
|
|
- converter.setAccept(MediaType.APPLICATION_JSON_UTF8);
|
|
|
- converter.setContentType(MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8"));
|
|
|
+ converter.accept = List.of(APPLICATION_JSON_UTF8);
|
|
|
+ converter.contentType = APPLICATION_FORM_URLENCODED_UTF8;
|
|
|
return converter;
|
|
|
}
|
|
|
|