|
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 | 19 | import java.net.URLEncoder;
|
21 | 20 | import java.nio.charset.StandardCharsets;
|
22 | 21 | import java.util.Collections;
|
|
29 | 28 | import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
|
30 | 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 |
| 32 | + * {@link OAuth2AuthorizationCodeGrantRequestEntityConverter} that convert from an |
| 33 | + * implementation of an {@link AbstractOAuth2AuthorizationGrantRequest} to a |
| 34 | + * {@link RequestEntity} representation of an OAuth 2.0 Access Token Request for the |
| 35 | + * specific Authorization Grant. |
36 | 36 | *
|
| 37 | + * @author Peter Eastham |
37 | 38 | * @author Joe Grandja
|
38 |
| - * @since 5.1 |
39 |
| - * @see OAuth2AuthorizationCodeGrantRequestEntityConverter |
| 39 | + * @since 6.3 |
40 | 40 | * @see OAuth2ClientCredentialsGrantRequestEntityConverter
|
41 | 41 | */
|
42 |
| -final class OAuth2AuthorizationGrantRequestEntityUtils { |
| 42 | +public class DefaultOAuth2TokenRequestHeadersConverter<T extends AbstractOAuth2AuthorizationGrantRequest> |
| 43 | + implements Converter<T, HttpHeaders> { |
43 | 44 |
|
44 |
| - private static HttpHeaders DEFAULT_TOKEN_REQUEST_HEADERS = getDefaultTokenRequestHeaders(); |
| 45 | + private static final HttpHeaders DEFAULT_TOKEN_HEADERS = getDefaultTokenRequestHeaders(); |
45 | 46 |
|
46 |
| - private OAuth2AuthorizationGrantRequestEntityUtils() { |
| 47 | + private boolean encodeClientCredentials = true; |
| 48 | + |
| 49 | + private static HttpHeaders getDefaultTokenRequestHeaders() { |
| 50 | + HttpHeaders headers = new HttpHeaders(); |
| 51 | + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8)); |
| 52 | + final MediaType contentType = MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8"); |
| 53 | + headers.setContentType(contentType); |
| 54 | + return headers; |
47 | 55 | }
|
48 | 56 |
|
49 |
| - static HttpHeaders getTokenRequestHeaders(ClientRegistration clientRegistration) { |
| 57 | + @Override |
| 58 | + public HttpHeaders convert(T source) { |
50 | 59 | HttpHeaders headers = new HttpHeaders();
|
51 |
| - headers.addAll(DEFAULT_TOKEN_REQUEST_HEADERS); |
| 60 | + headers.addAll(DEFAULT_TOKEN_HEADERS); |
| 61 | + ClientRegistration clientRegistration = source.getClientRegistration(); |
52 | 62 | if (ClientAuthenticationMethod.CLIENT_SECRET_BASIC.equals(clientRegistration.getClientAuthenticationMethod())) {
|
53 |
| - String clientId = encodeClientCredential(clientRegistration.getClientId()); |
54 |
| - String clientSecret = encodeClientCredential(clientRegistration.getClientSecret()); |
| 63 | + String clientId = this.encodeClientCredentials ? encodeClientCredential(clientRegistration.getClientId()) |
| 64 | + : clientRegistration.getClientId(); |
| 65 | + String clientSecret = this.encodeClientCredentials |
| 66 | + ? encodeClientCredential(clientRegistration.getClientSecret()) |
| 67 | + : clientRegistration.getClientSecret(); |
55 | 68 | headers.setBasicAuth(clientId, clientSecret);
|
56 | 69 | }
|
57 | 70 | return headers;
|
58 | 71 | }
|
59 | 72 |
|
60 | 73 | 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 |
| - } |
| 74 | + return URLEncoder.encode(clientCredential, StandardCharsets.UTF_8); |
68 | 75 | }
|
69 | 76 |
|
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; |
| 77 | + public void setEncodeClientCredentials(boolean encodeClientCredentials) { |
| 78 | + this.encodeClientCredentials = encodeClientCredentials; |
76 | 79 | }
|
77 | 80 |
|
78 | 81 | }
|
0 commit comments