|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 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 | + * http://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 | +package org.springframework.boot.autoconfigure.security.oauth2.client; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.function.Supplier; |
| 22 | + |
| 23 | +import org.springframework.boot.SpringApplication; |
| 24 | +import org.springframework.boot.context.config.ConfigFileApplicationListener; |
| 25 | +import org.springframework.boot.context.properties.bind.Bindable; |
| 26 | +import org.springframework.boot.context.properties.bind.Binder; |
| 27 | +import org.springframework.boot.context.properties.source.ConfigurationPropertyName; |
| 28 | +import org.springframework.boot.context.properties.source.ConfigurationPropertySource; |
| 29 | +import org.springframework.boot.context.properties.source.ConfigurationPropertySources; |
| 30 | +import org.springframework.boot.env.EnvironmentPostProcessor; |
| 31 | +import org.springframework.core.Ordered; |
| 32 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 33 | +import org.springframework.core.env.MapPropertySource; |
| 34 | + |
| 35 | +/** |
| 36 | + * {@link EnvironmentPostProcessor} that migrates legacy OAuth2 login client properties |
| 37 | + * under the `spring.security.oauth2.client.login` prefix. |
| 38 | + * |
| 39 | + * @author Madhura Bhave |
| 40 | + * @since 2.1.0 |
| 41 | + */ |
| 42 | +public class OAuth2ClientPropertiesEnvironmentPostProcessor |
| 43 | + implements EnvironmentPostProcessor, Ordered { |
| 44 | + |
| 45 | + private static final Bindable<Map<String, OAuth2ClientProperties.LoginClientRegistration>> STRING_LEGACY_REGISTRATION_MAP = Bindable |
| 46 | + .mapOf(String.class, OAuth2ClientProperties.LoginClientRegistration.class); |
| 47 | + |
| 48 | + private static final String PREFIX = "spring.security.oauth2.client.registration"; |
| 49 | + |
| 50 | + private static final String LOGIN_REGISTRATION_PREFIX = PREFIX + ".login."; |
| 51 | + |
| 52 | + private static final String UPDATED_PROPERTY_SOURCE_SUFFIX = "-updated-oauth-client"; |
| 53 | + |
| 54 | + private int order = ConfigFileApplicationListener.DEFAULT_ORDER + 1; |
| 55 | + |
| 56 | + @Override |
| 57 | + public void postProcessEnvironment(ConfigurableEnvironment environment, |
| 58 | + SpringApplication application) { |
| 59 | + environment.getPropertySources().forEach((propertySource) -> { |
| 60 | + String name = propertySource.getName(); |
| 61 | + Iterable<ConfigurationPropertySource> sources = ConfigurationPropertySources |
| 62 | + .from(propertySource); |
| 63 | + ConfigurationPropertySource source = sources.iterator().next(); |
| 64 | + Binder binder = new Binder(sources); |
| 65 | + Map<String, Object> map = new LinkedHashMap<>(); |
| 66 | + MapPropertySource updatedPropertySource = new MapPropertySource( |
| 67 | + name + UPDATED_PROPERTY_SOURCE_SUFFIX, map); |
| 68 | + Map<String, OAuth2ClientProperties.LoginClientRegistration> registrations = binder |
| 69 | + .bind(PREFIX, STRING_LEGACY_REGISTRATION_MAP) |
| 70 | + .orElse(Collections.emptyMap()); |
| 71 | + registrations.entrySet() |
| 72 | + .forEach((entry) -> addProperties(entry, source, map)); |
| 73 | + if (!map.isEmpty()) { |
| 74 | + environment.getPropertySources().addBefore(name, updatedPropertySource); |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + private void addProperties( |
| 80 | + Map.Entry<String, OAuth2ClientProperties.LoginClientRegistration> entry, |
| 81 | + ConfigurationPropertySource source, Map<String, Object> map) { |
| 82 | + OAuth2ClientProperties.LoginClientRegistration registration = entry.getValue(); |
| 83 | + String registrationId = entry.getKey(); |
| 84 | + addProperty(registrationId, "client-id", registration::getClientId, map, source); |
| 85 | + addProperty(registrationId, "client-secret", registration::getClientSecret, map, |
| 86 | + source); |
| 87 | + addProperty(registrationId, "client-name", registration::getClientName, map, |
| 88 | + source); |
| 89 | + addProperty(registrationId, "redirect-uri-template", |
| 90 | + registration::getRedirectUriTemplate, map, source); |
| 91 | + addProperty(registrationId, "authorization-grant-type", |
| 92 | + registration::getAuthorizationGrantType, map, source); |
| 93 | + addProperty(registrationId, "client-authentication-method", |
| 94 | + registration::getClientAuthenticationMethod, map, source); |
| 95 | + addProperty(registrationId, "provider", registration::getProvider, map, source); |
| 96 | + addProperty(registrationId, "scope", registration::getScope, map, source); |
| 97 | + } |
| 98 | + |
| 99 | + private void addProperty(String registrationId, String property, |
| 100 | + Supplier<Object> valueSupplier, Map<String, Object> map, |
| 101 | + ConfigurationPropertySource source) { |
| 102 | + String registrationKey = PREFIX + "." + registrationId + "."; |
| 103 | + String loginRegistrationKey = LOGIN_REGISTRATION_PREFIX + registrationId + "."; |
| 104 | + if (source.getConfigurationProperty( |
| 105 | + ConfigurationPropertyName.of(registrationKey + property)) != null) { |
| 106 | + map.put(loginRegistrationKey + property, valueSupplier.get()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public int getOrder() { |
| 112 | + return this.order; |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments