Commit 1ea921bf authored by Phillip Webb's avatar Phillip Webb

Migrate to OAuth2Client to use PropertyMapper

See gh-9018
parent ba86b684
...@@ -18,12 +18,10 @@ package org.springframework.boot.autoconfigure.security.oauth2.client; ...@@ -18,12 +18,10 @@ package org.springframework.boot.autoconfigure.security.oauth2.client;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider; import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider;
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Registration; import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Registration;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.context.properties.bind.convert.BinderConversionService; import org.springframework.boot.context.properties.bind.convert.BinderConversionService;
import org.springframework.core.convert.ConversionException; import org.springframework.core.convert.ConversionException;
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider; import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
...@@ -56,16 +54,19 @@ final class OAuth2ClientPropertiesRegistrationAdapter { ...@@ -56,16 +54,19 @@ final class OAuth2ClientPropertiesRegistrationAdapter {
private static ClientRegistration getClientRegistration(String registrationId, private static ClientRegistration getClientRegistration(String registrationId,
Registration properties, Map<String, Provider> providers) { Registration properties, Map<String, Provider> providers) {
Builder builder = getBuilder(registrationId, properties.getProvider(), providers); Builder builder = getBuilder(registrationId, properties.getProvider(), providers);
copyIfNotNull(properties::getClientId, builder::clientId); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
copyIfNotNull(properties::getClientSecret, builder::clientSecret); map.from(properties::getClientId).to(builder::clientId);
copyIfNotNull(properties::getClientAuthenticationMethod, map.from(properties::getClientSecret).to(builder::clientSecret);
builder::clientAuthenticationMethod, ClientAuthenticationMethod::new); map.from(properties::getClientAuthenticationMethod)
copyIfNotNull(properties::getAuthorizationGrantType, .as(ClientAuthenticationMethod::new)
builder::authorizationGrantType, AuthorizationGrantType::new); .to(builder::clientAuthenticationMethod);
copyIfNotNull(properties::getRedirectUriTemplate, builder::redirectUriTemplate); map.from(properties::getAuthorizationGrantType).as(AuthorizationGrantType::new)
copyIfNotNull(properties::getScope, builder::scope, .to(builder::authorizationGrantType);
(scope) -> scope.toArray(new String[scope.size()])); map.from(properties::getRedirectUriTemplate).to(builder::redirectUriTemplate);
copyIfNotNull(properties::getClientName, builder::clientName); map.from(properties::getScope)
.as((scope) -> scope.toArray(new String[scope.size()]))
.to(builder::scope);
map.from(properties::getClientName).to(builder::clientName);
return builder.build(); return builder.build();
} }
...@@ -95,11 +96,12 @@ final class OAuth2ClientPropertiesRegistrationAdapter { ...@@ -95,11 +96,12 @@ final class OAuth2ClientPropertiesRegistrationAdapter {
} }
private static Builder getBuilder(Builder builder, Provider provider) { private static Builder getBuilder(Builder builder, Provider provider) {
copyIfNotNull(provider::getAuthorizationUri, builder::authorizationUri); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
copyIfNotNull(provider::getTokenUri, builder::tokenUri); map.from(provider::getAuthorizationUri).to(builder::authorizationUri);
copyIfNotNull(provider::getUserInfoUri, builder::userInfoUri); map.from(provider::getTokenUri).to(builder::tokenUri);
copyIfNotNull(provider::getJwkSetUri, builder::jwkSetUri); map.from(provider::getUserInfoUri).to(builder::userInfoUri);
copyIfNotNull(provider::getUserNameAttribute, builder::userNameAttributeName); map.from(provider::getJwkSetUri).to(builder::jwkSetUri);
map.from(provider::getUserNameAttribute).to(builder::userNameAttributeName);
return builder; return builder;
} }
...@@ -113,16 +115,4 @@ final class OAuth2ClientPropertiesRegistrationAdapter { ...@@ -113,16 +115,4 @@ final class OAuth2ClientPropertiesRegistrationAdapter {
} }
} }
private static <T> void copyIfNotNull(Supplier<T> supplier, Consumer<T> consumer) {
copyIfNotNull(supplier, consumer, Function.identity());
}
private static <S, C> void copyIfNotNull(Supplier<S> supplier, Consumer<C> consumer,
Function<S, C> converter) {
S value = supplier.get();
if (value != null) {
consumer.accept(converter.apply(value));
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment