Skip to content

Commit 7b3e687

Browse files
committed
Deprecate OAuth2ClientPropertiesRegistrationAdapter
This commit deprecates OAuth2ClientPropertiesRegistrationAdapter as it wasn't really an adapter because it provides a static utility method rather than adapting one contract to another. A replacement, OAuth2ClientPropertiesMapper, is introduced that maps the OAuth2 client properties to the required types. Closes gh-34714
1 parent 651f341 commit 7b3e687

File tree

5 files changed

+170
-114
lines changed

5 files changed

+170
-114
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright 2012-2023 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.boot.autoconfigure.security.oauth2.client;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider;
23+
import org.springframework.boot.context.properties.PropertyMapper;
24+
import org.springframework.boot.convert.ApplicationConversionService;
25+
import org.springframework.core.convert.ConversionException;
26+
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
27+
import org.springframework.security.oauth2.client.registration.ClientRegistration;
28+
import org.springframework.security.oauth2.client.registration.ClientRegistration.Builder;
29+
import org.springframework.security.oauth2.client.registration.ClientRegistrations;
30+
import org.springframework.security.oauth2.core.AuthenticationMethod;
31+
import org.springframework.security.oauth2.core.AuthorizationGrantType;
32+
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
33+
import org.springframework.util.StringUtils;
34+
35+
/**
36+
* Maps {@link OAuth2ClientProperties} to {@link ClientRegistration ClientRegistrations}.
37+
*
38+
* @author Phillip Webb
39+
* @author Thiago Hirata
40+
* @author Madhura Bhave
41+
* @author MyeongHyeon Lee
42+
* @author Andy Wilkinson
43+
* @since 3.1.0
44+
*/
45+
public final class OAuth2ClientPropertiesMapper {
46+
47+
private final OAuth2ClientProperties properties;
48+
49+
/**
50+
* Creates a new mapper for the given {@code properties}.
51+
* @param properties the properties to map
52+
*/
53+
public OAuth2ClientPropertiesMapper(OAuth2ClientProperties properties) {
54+
this.properties = properties;
55+
}
56+
57+
/**
58+
* Maps the properties to {@link ClientRegistration ClientRegistrations}.
59+
* @return the mapped {@code ClientRegistrations}
60+
*/
61+
public Map<String, ClientRegistration> asClientRegistrations() {
62+
Map<String, ClientRegistration> clientRegistrations = new HashMap<>();
63+
this.properties.getRegistration()
64+
.forEach((key, value) -> clientRegistrations.put(key,
65+
getClientRegistration(key, value, this.properties.getProvider())));
66+
return clientRegistrations;
67+
}
68+
69+
private static ClientRegistration getClientRegistration(String registrationId,
70+
OAuth2ClientProperties.Registration properties, Map<String, Provider> providers) {
71+
Builder builder = getBuilderFromIssuerIfPossible(registrationId, properties.getProvider(), providers);
72+
if (builder == null) {
73+
builder = getBuilder(registrationId, properties.getProvider(), providers);
74+
}
75+
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
76+
map.from(properties::getClientId).to(builder::clientId);
77+
map.from(properties::getClientSecret).to(builder::clientSecret);
78+
map.from(properties::getClientAuthenticationMethod)
79+
.as(ClientAuthenticationMethod::new)
80+
.to(builder::clientAuthenticationMethod);
81+
map.from(properties::getAuthorizationGrantType)
82+
.as(AuthorizationGrantType::new)
83+
.to(builder::authorizationGrantType);
84+
map.from(properties::getRedirectUri).to(builder::redirectUri);
85+
map.from(properties::getScope).as(StringUtils::toStringArray).to(builder::scope);
86+
map.from(properties::getClientName).to(builder::clientName);
87+
return builder.build();
88+
}
89+
90+
private static Builder getBuilderFromIssuerIfPossible(String registrationId, String configuredProviderId,
91+
Map<String, Provider> providers) {
92+
String providerId = (configuredProviderId != null) ? configuredProviderId : registrationId;
93+
if (providers.containsKey(providerId)) {
94+
Provider provider = providers.get(providerId);
95+
String issuer = provider.getIssuerUri();
96+
if (issuer != null) {
97+
Builder builder = ClientRegistrations.fromIssuerLocation(issuer).registrationId(registrationId);
98+
return getBuilder(builder, provider);
99+
}
100+
}
101+
return null;
102+
}
103+
104+
private static Builder getBuilder(String registrationId, String configuredProviderId,
105+
Map<String, Provider> providers) {
106+
String providerId = (configuredProviderId != null) ? configuredProviderId : registrationId;
107+
CommonOAuth2Provider provider = getCommonProvider(providerId);
108+
if (provider == null && !providers.containsKey(providerId)) {
109+
throw new IllegalStateException(getErrorMessage(configuredProviderId, registrationId));
110+
}
111+
Builder builder = (provider != null) ? provider.getBuilder(registrationId)
112+
: ClientRegistration.withRegistrationId(registrationId);
113+
if (providers.containsKey(providerId)) {
114+
return getBuilder(builder, providers.get(providerId));
115+
}
116+
return builder;
117+
}
118+
119+
private static String getErrorMessage(String configuredProviderId, String registrationId) {
120+
return ((configuredProviderId != null) ? "Unknown provider ID '" + configuredProviderId + "'"
121+
: "Provider ID must be specified for client registration '" + registrationId + "'");
122+
}
123+
124+
private static Builder getBuilder(Builder builder, Provider provider) {
125+
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
126+
map.from(provider::getAuthorizationUri).to(builder::authorizationUri);
127+
map.from(provider::getTokenUri).to(builder::tokenUri);
128+
map.from(provider::getUserInfoUri).to(builder::userInfoUri);
129+
map.from(provider::getUserInfoAuthenticationMethod)
130+
.as(AuthenticationMethod::new)
131+
.to(builder::userInfoAuthenticationMethod);
132+
map.from(provider::getJwkSetUri).to(builder::jwkSetUri);
133+
map.from(provider::getUserNameAttribute).to(builder::userNameAttributeName);
134+
return builder;
135+
}
136+
137+
private static CommonOAuth2Provider getCommonProvider(String providerId) {
138+
try {
139+
return ApplicationConversionService.getSharedInstance().convert(providerId, CommonOAuth2Provider.class);
140+
}
141+
catch (ConversionException ex) {
142+
return null;
143+
}
144+
}
145+
146+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2ClientPropertiesRegistrationAdapter.java

Lines changed: 4 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,9 @@
1616

1717
package org.springframework.boot.autoconfigure.security.oauth2.client;
1818

19-
import java.util.HashMap;
2019
import java.util.Map;
2120

22-
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider;
23-
import org.springframework.boot.context.properties.PropertyMapper;
24-
import org.springframework.boot.convert.ApplicationConversionService;
25-
import org.springframework.core.convert.ConversionException;
26-
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
2721
import org.springframework.security.oauth2.client.registration.ClientRegistration;
28-
import org.springframework.security.oauth2.client.registration.ClientRegistration.Builder;
29-
import org.springframework.security.oauth2.client.registration.ClientRegistrations;
30-
import org.springframework.security.oauth2.core.AuthenticationMethod;
31-
import org.springframework.security.oauth2.core.AuthorizationGrantType;
32-
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
33-
import org.springframework.util.StringUtils;
3422

3523
/**
3624
* Adapter class to convert {@link OAuth2ClientProperties} to a
@@ -41,95 +29,17 @@
4129
* @author Madhura Bhave
4230
* @author MyeongHyeon Lee
4331
* @since 2.1.0
32+
* @deprecated since 3.1.0 for removal in 3.3.0 in favor of
33+
* {@link OAuth2ClientPropertiesMapper}
4434
*/
35+
@Deprecated(since = "3.1.0", forRemoval = true)
4536
public final class OAuth2ClientPropertiesRegistrationAdapter {
4637

4738
private OAuth2ClientPropertiesRegistrationAdapter() {
4839
}
4940

5041
public static Map<String, ClientRegistration> getClientRegistrations(OAuth2ClientProperties properties) {
51-
Map<String, ClientRegistration> clientRegistrations = new HashMap<>();
52-
properties.getRegistration()
53-
.forEach((key, value) -> clientRegistrations.put(key,
54-
getClientRegistration(key, value, properties.getProvider())));
55-
return clientRegistrations;
56-
}
57-
58-
private static ClientRegistration getClientRegistration(String registrationId,
59-
OAuth2ClientProperties.Registration properties, Map<String, Provider> providers) {
60-
Builder builder = getBuilderFromIssuerIfPossible(registrationId, properties.getProvider(), providers);
61-
if (builder == null) {
62-
builder = getBuilder(registrationId, properties.getProvider(), providers);
63-
}
64-
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
65-
map.from(properties::getClientId).to(builder::clientId);
66-
map.from(properties::getClientSecret).to(builder::clientSecret);
67-
map.from(properties::getClientAuthenticationMethod)
68-
.as(ClientAuthenticationMethod::new)
69-
.to(builder::clientAuthenticationMethod);
70-
map.from(properties::getAuthorizationGrantType)
71-
.as(AuthorizationGrantType::new)
72-
.to(builder::authorizationGrantType);
73-
map.from(properties::getRedirectUri).to(builder::redirectUri);
74-
map.from(properties::getScope).as(StringUtils::toStringArray).to(builder::scope);
75-
map.from(properties::getClientName).to(builder::clientName);
76-
return builder.build();
77-
}
78-
79-
private static Builder getBuilderFromIssuerIfPossible(String registrationId, String configuredProviderId,
80-
Map<String, Provider> providers) {
81-
String providerId = (configuredProviderId != null) ? configuredProviderId : registrationId;
82-
if (providers.containsKey(providerId)) {
83-
Provider provider = providers.get(providerId);
84-
String issuer = provider.getIssuerUri();
85-
if (issuer != null) {
86-
Builder builder = ClientRegistrations.fromIssuerLocation(issuer).registrationId(registrationId);
87-
return getBuilder(builder, provider);
88-
}
89-
}
90-
return null;
91-
}
92-
93-
private static Builder getBuilder(String registrationId, String configuredProviderId,
94-
Map<String, Provider> providers) {
95-
String providerId = (configuredProviderId != null) ? configuredProviderId : registrationId;
96-
CommonOAuth2Provider provider = getCommonProvider(providerId);
97-
if (provider == null && !providers.containsKey(providerId)) {
98-
throw new IllegalStateException(getErrorMessage(configuredProviderId, registrationId));
99-
}
100-
Builder builder = (provider != null) ? provider.getBuilder(registrationId)
101-
: ClientRegistration.withRegistrationId(registrationId);
102-
if (providers.containsKey(providerId)) {
103-
return getBuilder(builder, providers.get(providerId));
104-
}
105-
return builder;
106-
}
107-
108-
private static String getErrorMessage(String configuredProviderId, String registrationId) {
109-
return ((configuredProviderId != null) ? "Unknown provider ID '" + configuredProviderId + "'"
110-
: "Provider ID must be specified for client registration '" + registrationId + "'");
111-
}
112-
113-
private static Builder getBuilder(Builder builder, Provider provider) {
114-
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
115-
map.from(provider::getAuthorizationUri).to(builder::authorizationUri);
116-
map.from(provider::getTokenUri).to(builder::tokenUri);
117-
map.from(provider::getUserInfoUri).to(builder::userInfoUri);
118-
map.from(provider::getUserInfoAuthenticationMethod)
119-
.as(AuthenticationMethod::new)
120-
.to(builder::userInfoAuthenticationMethod);
121-
map.from(provider::getJwkSetUri).to(builder::jwkSetUri);
122-
map.from(provider::getUserNameAttribute).to(builder::userNameAttributeName);
123-
return builder;
124-
}
125-
126-
private static CommonOAuth2Provider getCommonProvider(String providerId) {
127-
try {
128-
return ApplicationConversionService.getSharedInstance().convert(providerId, CommonOAuth2Provider.class);
129-
}
130-
catch (ConversionException ex) {
131-
return null;
132-
}
42+
return new OAuth2ClientPropertiesMapper(properties).asClientRegistrations();
13343
}
13444

13545
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/reactive/ReactiveOAuth2ClientConfigurations.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2525
import org.springframework.boot.autoconfigure.security.oauth2.client.ClientsConfiguredCondition;
2626
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties;
27-
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter;
27+
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesMapper;
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.Conditional;
3030
import org.springframework.context.annotation.Configuration;
@@ -55,7 +55,7 @@ static class ReactiveClientRegistrationRepositoryConfiguration {
5555
@Bean
5656
InMemoryReactiveClientRegistrationRepository clientRegistrationRepository(OAuth2ClientProperties properties) {
5757
List<ClientRegistration> registrations = new ArrayList<>(
58-
OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties).values());
58+
new OAuth2ClientPropertiesMapper(properties).asClientRegistrations().values());
5959
return new InMemoryReactiveClientRegistrationRepository(registrations);
6060
}
6161

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/client/servlet/OAuth2ClientRegistrationRepositoryConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2323
import org.springframework.boot.autoconfigure.security.oauth2.client.ClientsConfiguredCondition;
2424
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties;
25-
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesRegistrationAdapter;
25+
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientPropertiesMapper;
2626
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2727
import org.springframework.context.annotation.Bean;
2828
import org.springframework.context.annotation.Conditional;
@@ -46,7 +46,7 @@ class OAuth2ClientRegistrationRepositoryConfiguration {
4646
@ConditionalOnMissingBean(ClientRegistrationRepository.class)
4747
InMemoryClientRegistrationRepository clientRegistrationRepository(OAuth2ClientProperties properties) {
4848
List<ClientRegistration> registrations = new ArrayList<>(
49-
OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties).values());
49+
new OAuth2ClientPropertiesMapper(properties).asClientRegistrations().values());
5050
return new InMemoryClientRegistrationRepository(registrations);
5151
}
5252

0 commit comments

Comments
 (0)