|
| 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.security.oauth2.client.registration.ClientRegistration; |
| 21 | +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
| 22 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 23 | +import org.springframework.util.Assert; |
| 24 | +import org.springframework.util.LinkedMultiValueMap; |
| 25 | +import org.springframework.util.MultiValueMap; |
| 26 | + |
| 27 | +/** |
| 28 | + * Default {@link Converter} used to convert an |
| 29 | + * {@link AbstractOAuth2AuthorizationGrantRequest} to the default {@link MultiValueMap |
| 30 | + * parameters} of an OAuth 2.0 Access Token Request. |
| 31 | + * <p> |
| 32 | + * This implementation does not provide grant-type specific parameters. The following |
| 33 | + * parameters are provided: |
| 34 | + * |
| 35 | + * <ul> |
| 36 | + * <li>{@code grant_type} - always provided</li> |
| 37 | + * <li>{@code client_id} - provided unless the {@code clientAuthenticationMethod} is |
| 38 | + * {@code client_secret_basic}</li> |
| 39 | + * <li>{@code client_secret} - provided when the {@code clientAuthenticationMethod} is |
| 40 | + * {@code client_secret_post}</li> |
| 41 | + * </ul> |
| 42 | + * |
| 43 | + * @param <T> type of grant request |
| 44 | + * @author Steve Riesenberg |
| 45 | + * @since 6.4 |
| 46 | + * @see AbstractWebClientReactiveOAuth2AccessTokenResponseClient |
| 47 | + * @see AbstractRestClientOAuth2AccessTokenResponseClient |
| 48 | + */ |
| 49 | +public final class DefaultOAuth2TokenRequestParametersConverter<T extends AbstractOAuth2AuthorizationGrantRequest> |
| 50 | + implements Converter<T, MultiValueMap<String, String>> { |
| 51 | + |
| 52 | + private final Converter<T, MultiValueMap<String, String>> defaultParametersConverter; |
| 53 | + |
| 54 | + public DefaultOAuth2TokenRequestParametersConverter() { |
| 55 | + this(DefaultOAuth2TokenRequestParametersConverter::defaultParameters); |
| 56 | + } |
| 57 | + |
| 58 | + private DefaultOAuth2TokenRequestParametersConverter( |
| 59 | + Converter<T, MultiValueMap<String, String>> defaultParametersConverter) { |
| 60 | + Assert.notNull(defaultParametersConverter, "defaultParametersConverter cannot be null"); |
| 61 | + this.defaultParametersConverter = defaultParametersConverter; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public MultiValueMap<String, String> convert(T grantRequest) { |
| 66 | + return this.defaultParametersConverter.convert(grantRequest); |
| 67 | + } |
| 68 | + |
| 69 | + private static <T extends AbstractOAuth2AuthorizationGrantRequest> MultiValueMap<String, String> defaultParameters( |
| 70 | + T grantRequest) { |
| 71 | + ClientRegistration clientRegistration = grantRequest.getClientRegistration(); |
| 72 | + MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(); |
| 73 | + parameters.set(OAuth2ParameterNames.GRANT_TYPE, grantRequest.getGrantType().getValue()); |
| 74 | + if (!ClientAuthenticationMethod.CLIENT_SECRET_BASIC |
| 75 | + .equals(clientRegistration.getClientAuthenticationMethod())) { |
| 76 | + parameters.set(OAuth2ParameterNames.CLIENT_ID, clientRegistration.getClientId()); |
| 77 | + } |
| 78 | + if (ClientAuthenticationMethod.CLIENT_SECRET_POST.equals(clientRegistration.getClientAuthenticationMethod())) { |
| 79 | + parameters.set(OAuth2ParameterNames.CLIENT_SECRET, clientRegistration.getClientSecret()); |
| 80 | + } |
| 81 | + return parameters; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Wrap a custom {@link Converter} in order to override or omit default parameters |
| 86 | + * with |
| 87 | + * {@link AbstractWebClientReactiveOAuth2AccessTokenResponseClient#setParametersConverter(Converter)}. |
| 88 | + * @param defaultParametersConverter the {@link Converter} used for converting the |
| 89 | + * @param <T> type of grant request {@link AbstractOAuth2AuthorizationGrantRequest} to |
| 90 | + * {@link MultiValueMap parameters} |
| 91 | + * @return the wrapped {@link Converter} |
| 92 | + */ |
| 93 | + public static <T extends AbstractOAuth2AuthorizationGrantRequest> Converter<T, MultiValueMap<String, String>> of( |
| 94 | + Converter<T, MultiValueMap<String, String>> defaultParametersConverter) { |
| 95 | + return new DefaultOAuth2TokenRequestParametersConverter<>(defaultParametersConverter); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments