|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.oauth2.client.endpoint; |
| 18 | + |
| 19 | +import org.springframework.core.convert.converter.Converter; |
| 20 | +import org.springframework.http.HttpHeaders; |
| 21 | +import org.springframework.http.MediaType; |
| 22 | +import org.springframework.http.RequestEntity; |
| 23 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 24 | +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
| 25 | + |
| 26 | +import java.net.URLEncoder; |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.util.Collections; |
| 29 | + |
| 30 | +/** |
| 31 | + * Default {@link Converter} used to convert an |
| 32 | + * {@link AbstractOAuth2AuthorizationGrantRequest} to the {@link HttpHeaders} of aKk |
| 33 | + * {@link RequestEntity} representation of an OAuth 2.0 Access Token Request for the |
| 34 | + * specific Authorization Grant. |
| 35 | + * |
| 36 | + * @author Peter Eastham |
| 37 | + * @author Joe Grandja |
| 38 | + * @see AbstractOAuth2AuthorizationGrantRequestEntityConverter |
| 39 | + * @since 6.3 |
| 40 | + */ |
| 41 | +public final class DefaultOAuth2TokenRequestHeadersConverter<T extends AbstractOAuth2AuthorizationGrantRequest> |
| 42 | + implements Converter<T, HttpHeaders> { |
| 43 | + |
| 44 | + private MediaType accept = MediaType.APPLICATION_JSON; |
| 45 | + |
| 46 | + private MediaType contentType = MediaType.APPLICATION_FORM_URLENCODED; |
| 47 | + |
| 48 | + private boolean encodeClientCredentialsIfRequired = true; |
| 49 | + |
| 50 | + /** |
| 51 | + * Populates the headers for the token request. |
| 52 | + * @param grantRequest the grant request |
| 53 | + * @return the headers populated for the token request |
| 54 | + */ |
| 55 | + @Override |
| 56 | + public HttpHeaders convert(T grantRequest) { |
| 57 | + HttpHeaders headers = new HttpHeaders(); |
| 58 | + headers.setAccept(Collections.singletonList(accept)); |
| 59 | + headers.setContentType(contentType); |
| 60 | + ClientRegistration clientRegistration = grantRequest.getClientRegistration(); |
| 61 | + if (ClientAuthenticationMethod.CLIENT_SECRET_BASIC.equals(clientRegistration.getClientAuthenticationMethod())) { |
| 62 | + String clientId = encodeClientCredential(clientRegistration.getClientId()); |
| 63 | + String clientSecret = encodeClientCredential(clientRegistration.getClientSecret()); |
| 64 | + headers.setBasicAuth(clientId, clientSecret); |
| 65 | + } |
| 66 | + return headers; |
| 67 | + } |
| 68 | + |
| 69 | + private String encodeClientCredential(String clientCredential) { |
| 70 | + String encodedCredential = clientCredential; |
| 71 | + if (this.encodeClientCredentialsIfRequired) { |
| 72 | + encodedCredential = URLEncoder.encode(clientCredential, StandardCharsets.UTF_8); |
| 73 | + } |
| 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; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * MediaType to set for the Accept header. Default is application/json |
| 88 | + * @param accept MediaType to use for the Accept header |
| 89 | + */ |
| 90 | + private void setAccept(MediaType accept) { |
| 91 | + this.accept = accept; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 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 |
| 98 | + */ |
| 99 | + private void setContentType(MediaType contentType) { |
| 100 | + this.contentType = contentType; |
| 101 | + } |
| 102 | + |
| 103 | + static <T extends AbstractOAuth2AuthorizationGrantRequest> DefaultOAuth2TokenRequestHeadersConverter<T> historicalConverter() { |
| 104 | + 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")); |
| 107 | + return converter; |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments