|
| 1 | +/* |
| 2 | + * Copyright 2020-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 | +package sample.registration; |
| 17 | + |
| 18 | +import org.springframework.core.convert.converter.Converter; |
| 19 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 20 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 21 | +import org.springframework.security.oauth2.server.authorization.oidc.OidcClientRegistration; |
| 22 | +import org.springframework.security.oauth2.server.authorization.oidc.authentication.OidcClientConfigurationAuthenticationProvider; |
| 23 | +import org.springframework.security.oauth2.server.authorization.oidc.authentication.OidcClientRegistrationAuthenticationProvider; |
| 24 | +import org.springframework.security.oauth2.server.authorization.oidc.converter.OidcClientRegistrationRegisteredClientConverter; |
| 25 | +import org.springframework.security.oauth2.server.authorization.oidc.converter.RegisteredClientOidcClientRegistrationConverter; |
| 26 | +import org.springframework.security.oauth2.server.authorization.settings.ClientSettings; |
| 27 | +import org.springframework.util.CollectionUtils; |
| 28 | + |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.function.Consumer; |
| 33 | +import java.util.function.Function; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +public class CustomMetadataConfig { |
| 37 | + public static Consumer<List<AuthenticationProvider>> registeredClientConverters() { |
| 38 | + List<String> customClientMetadata = List.of("logo_uri", "contacts"); // <1> |
| 39 | + |
| 40 | + return authenticationProviders -> // <2> |
| 41 | + { |
| 42 | + CustomRegisteredClientConverter registeredClientConverter = new CustomRegisteredClientConverter(customClientMetadata); |
| 43 | + CustomClientRegistrationConverter clientRegistrationConverter = new CustomClientRegistrationConverter(customClientMetadata); |
| 44 | + |
| 45 | + authenticationProviders.forEach(authenticationProvider -> { |
| 46 | + if (authenticationProvider instanceof OidcClientRegistrationAuthenticationProvider provider) { // <3> |
| 47 | + provider.setRegisteredClientConverter(registeredClientConverter); // <4> |
| 48 | + provider.setClientRegistrationConverter(clientRegistrationConverter); // <5> |
| 49 | + } |
| 50 | + |
| 51 | + if (authenticationProvider instanceof OidcClientConfigurationAuthenticationProvider provider) { |
| 52 | + provider.setClientRegistrationConverter(clientRegistrationConverter); // <6> |
| 53 | + } |
| 54 | + }); |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + static class CustomRegisteredClientConverter implements Converter<OidcClientRegistration, RegisteredClient> { // <7> |
| 59 | + private final List<String> customMetadata; |
| 60 | + |
| 61 | + private final OidcClientRegistrationRegisteredClientConverter delegate; |
| 62 | + |
| 63 | + CustomRegisteredClientConverter(List<String> customMetadata) { |
| 64 | + this.customMetadata = customMetadata; |
| 65 | + this.delegate = new OidcClientRegistrationRegisteredClientConverter(); |
| 66 | + } |
| 67 | + |
| 68 | + public RegisteredClient convert(OidcClientRegistration clientRegistration) { |
| 69 | + RegisteredClient convertedClient = delegate.convert(clientRegistration); |
| 70 | + ClientSettings.Builder clientSettingsBuilder = ClientSettings |
| 71 | + .withSettings(convertedClient.getClientSettings().getSettings()); |
| 72 | + |
| 73 | + if (!CollectionUtils.isEmpty(this.customMetadata)) { |
| 74 | + clientRegistration.getClaims().forEach((claim, value) -> { |
| 75 | + if (this.customMetadata.contains(claim)) { |
| 76 | + clientSettingsBuilder.setting(claim, value); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + return RegisteredClient.from(convertedClient).clientSettings(clientSettingsBuilder.build()).build(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + static class CustomClientRegistrationConverter implements Converter<RegisteredClient, OidcClientRegistration> { // <8> |
| 86 | + private final List<String> customMetadata; |
| 87 | + |
| 88 | + private final RegisteredClientOidcClientRegistrationConverter delegate; |
| 89 | + |
| 90 | + CustomClientRegistrationConverter(List<String> customMetadata) { |
| 91 | + this.customMetadata = customMetadata; |
| 92 | + this.delegate = new RegisteredClientOidcClientRegistrationConverter(); |
| 93 | + } |
| 94 | + |
| 95 | + public OidcClientRegistration convert(RegisteredClient registeredClient) { |
| 96 | + var clientRegistration = delegate.convert(registeredClient); |
| 97 | + Map<String, Object> claims = new HashMap<>(clientRegistration.getClaims()); |
| 98 | + if (!CollectionUtils.isEmpty(customMetadata)) { |
| 99 | + ClientSettings clientSettings = registeredClient.getClientSettings(); |
| 100 | + |
| 101 | + claims.putAll(customMetadata.stream() |
| 102 | + .filter(metadatum -> clientSettings.getSetting(metadatum) != null) |
| 103 | + .collect(Collectors.toMap(Function.identity(), clientSettings::getSetting))); |
| 104 | + } |
| 105 | + return OidcClientRegistration.withClaims(claims).build(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments