|
| 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 | +} |
0 commit comments