|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.security.oauth2.client.endpoint;
|
18 | 18 |
|
19 |
| -import java.io.UnsupportedEncodingException; |
20 |
| -import java.net.URLEncoder; |
21 |
| -import java.nio.charset.StandardCharsets; |
22 |
| -import java.util.Collections; |
23 |
| - |
24 | 19 | import org.springframework.core.convert.converter.Converter;
|
25 | 20 | import org.springframework.http.HttpHeaders;
|
26 | 21 | import org.springframework.http.MediaType;
|
27 | 22 | import org.springframework.http.RequestEntity;
|
28 | 23 | import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
29 | 24 | import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
|
30 | 25 |
|
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.util.Collections; |
| 28 | +import java.net.URLEncoder; |
| 29 | + |
31 | 30 | /**
|
32 |
| - * Utility methods used by the {@link Converter}'s that convert from an implementation of |
33 |
| - * an {@link AbstractOAuth2AuthorizationGrantRequest} to a {@link RequestEntity} |
34 |
| - * representation of an OAuth 2.0 Access Token Request for the specific Authorization |
35 |
| - * Grant. |
| 31 | + * Default Converter used by the {@link OAuth2AuthorizationCodeGrantRequestEntityConverter} |
| 32 | + * that convert from an implementation of an {@link AbstractOAuth2AuthorizationGrantRequest} |
| 33 | + * to a {@link RequestEntity} representation of an OAuth 2.0 Access Token Request for the |
| 34 | + * specific Authorization Grant. |
36 | 35 | *
|
| 36 | + * @author Peter Eastham |
37 | 37 | * @author Joe Grandja
|
38 |
| - * @since 5.1 |
39 |
| - * @see OAuth2AuthorizationCodeGrantRequestEntityConverter |
| 38 | + * @since 6.3 |
40 | 39 | * @see OAuth2ClientCredentialsGrantRequestEntityConverter
|
41 | 40 | */
|
42 |
| -final class OAuth2AuthorizationGrantRequestEntityUtils { |
| 41 | +public class DefaultOAuth2TokenRequestHeadersConverter<T extends AbstractOAuth2AuthorizationGrantRequest> |
| 42 | + implements Converter<T, HttpHeaders> { |
43 | 43 |
|
44 |
| - private static HttpHeaders DEFAULT_TOKEN_REQUEST_HEADERS = getDefaultTokenRequestHeaders(); |
| 44 | + private static final HttpHeaders DEFAULT_TOKEN_HEADERS = getDefaultTokenRequestHeaders(); |
| 45 | + private boolean encodeClientCredentials = true; |
45 | 46 |
|
46 |
| - private OAuth2AuthorizationGrantRequestEntityUtils() { |
| 47 | + private static HttpHeaders getDefaultTokenRequestHeaders() { |
| 48 | + HttpHeaders headers = new HttpHeaders(); |
| 49 | + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8)); |
| 50 | + final MediaType contentType = MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8"); |
| 51 | + headers.setContentType(contentType); |
| 52 | + return headers; |
47 | 53 | }
|
48 | 54 |
|
49 |
| - static HttpHeaders getTokenRequestHeaders(ClientRegistration clientRegistration) { |
| 55 | + |
| 56 | + @Override |
| 57 | + public HttpHeaders convert(T source) { |
50 | 58 | HttpHeaders headers = new HttpHeaders();
|
51 |
| - headers.addAll(DEFAULT_TOKEN_REQUEST_HEADERS); |
| 59 | + headers.addAll(DEFAULT_TOKEN_HEADERS); |
| 60 | + ClientRegistration clientRegistration = source.getClientRegistration(); |
52 | 61 | if (ClientAuthenticationMethod.CLIENT_SECRET_BASIC.equals(clientRegistration.getClientAuthenticationMethod())) {
|
53 |
| - String clientId = encodeClientCredential(clientRegistration.getClientId()); |
54 |
| - String clientSecret = encodeClientCredential(clientRegistration.getClientSecret()); |
| 62 | + String clientId = encodeClientCredentials ? |
| 63 | + encodeClientCredential(clientRegistration.getClientId()) : clientRegistration.getClientId(); |
| 64 | + String clientSecret = encodeClientCredentials ? |
| 65 | + encodeClientCredential(clientRegistration.getClientSecret()) : clientRegistration.getClientSecret(); |
55 | 66 | headers.setBasicAuth(clientId, clientSecret);
|
56 | 67 | }
|
57 | 68 | return headers;
|
58 | 69 | }
|
59 | 70 |
|
60 | 71 | private static String encodeClientCredential(String clientCredential) {
|
61 |
| - try { |
62 |
| - return URLEncoder.encode(clientCredential, StandardCharsets.UTF_8.toString()); |
63 |
| - } |
64 |
| - catch (UnsupportedEncodingException ex) { |
65 |
| - // Will not happen since UTF-8 is a standard charset |
66 |
| - throw new IllegalArgumentException(ex); |
67 |
| - } |
68 |
| - } |
| 72 | + return URLEncoder.encode(clientCredential, StandardCharsets.UTF_8); |
| 73 | + } |
69 | 74 |
|
70 |
| - private static HttpHeaders getDefaultTokenRequestHeaders() { |
71 |
| - HttpHeaders headers = new HttpHeaders(); |
72 |
| - headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8)); |
73 |
| - final MediaType contentType = MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8"); |
74 |
| - headers.setContentType(contentType); |
75 |
| - return headers; |
| 75 | + public void setEncodeClientCredentials(boolean encodeClientCredentials) { |
| 76 | + this.encodeClientCredentials = encodeClientCredentials; |
76 | 77 | }
|
77 |
| - |
78 | 78 | }
|
0 commit comments