/
OAuth2ClientPropertiesRegistrationAdapter.java
128 lines (114 loc) · 5.11 KB
/
OAuth2ClientPropertiesRegistrationAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.security.oauth2.client;
import java.util.HashMap;
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.Registration;
import org.springframework.boot.context.properties.bind.convert.BinderConversionService;
import org.springframework.core.convert.ConversionException;
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistration.Builder;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
/**
* Adapter class to convert {@link OAuth2ClientProperties} to a
* {@link ClientRegistration}.
*
* @author Phillip Webb
* @author Thiago Hirata
* @since 2.0.0
*/
final class OAuth2ClientPropertiesRegistrationAdapter {
private OAuth2ClientPropertiesRegistrationAdapter() {
}
public static Map<String, ClientRegistration> getClientRegistrations(
OAuth2ClientProperties properties) {
Map<String, ClientRegistration> clientRegistrations = new HashMap<>();
properties.getRegistration().forEach((key, value) -> clientRegistrations.put(key,
getClientRegistration(key, value, properties.getProvider())));
return clientRegistrations;
}
private static ClientRegistration getClientRegistration(String registrationId,
Registration properties, Map<String, Provider> providers) {
Builder builder = getBuilder(registrationId, properties.getProvider(), providers);
copyIfNotNull(properties::getClientId, builder::clientId);
copyIfNotNull(properties::getClientSecret, builder::clientSecret);
copyIfNotNull(properties::getClientAuthenticationMethod,
builder::clientAuthenticationMethod, ClientAuthenticationMethod::new);
copyIfNotNull(properties::getAuthorizationGrantType,
builder::authorizationGrantType, AuthorizationGrantType::new);
copyIfNotNull(properties::getRedirectUriTemplate, builder::redirectUriTemplate);
copyIfNotNull(properties::getScope, builder::scope,
(scope) -> scope.toArray(new String[scope.size()]));
copyIfNotNull(properties::getClientName, builder::clientName);
return builder.build();
}
private static Builder getBuilder(String registrationId, String configuredProviderId,
Map<String, Provider> providers) {
String providerId = (configuredProviderId == null ? registrationId
: configuredProviderId);
CommonOAuth2Provider provider = getCommonProvider(providerId);
if (provider == null && !providers.containsKey(providerId)) {
throw new IllegalStateException(
getErrorMessage(configuredProviderId, registrationId));
}
Builder builder = (provider != null ? provider.getBuilder(registrationId)
: ClientRegistration.withRegistrationId(registrationId));
if (providers.containsKey(providerId)) {
return getBuilder(builder, providers.get(providerId));
}
return builder;
}
private static String getErrorMessage(String configuredProviderId,
String registrationId) {
return (configuredProviderId == null
? "Provider ID must be specified for client registration '"
+ registrationId + "'"
: "Unknown provider ID '" + configuredProviderId + "'");
}
private static Builder getBuilder(Builder builder, Provider provider) {
copyIfNotNull(provider::getAuthorizationUri, builder::authorizationUri);
copyIfNotNull(provider::getTokenUri, builder::tokenUri);
copyIfNotNull(provider::getUserInfoUri, builder::userInfoUri);
copyIfNotNull(provider::getJwkSetUri, builder::jwkSetUri);
copyIfNotNull(provider::getUserNameAttribute, builder::userNameAttributeName);
return builder;
}
private static CommonOAuth2Provider getCommonProvider(String providerId) {
try {
return new BinderConversionService(null).convert(providerId,
CommonOAuth2Provider.class);
}
catch (ConversionException ex) {
return null;
}
}
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));
}
}
}