|
| 1 | +/* |
| 2 | + * Copyright 2020 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 org.springframework.security.oauth2.core.http.converter; |
| 17 | + |
| 18 | +import org.springframework.core.ParameterizedTypeReference; |
| 19 | +import org.springframework.core.convert.TypeDescriptor; |
| 20 | +import org.springframework.core.convert.converter.Converter; |
| 21 | +import org.springframework.http.HttpInputMessage; |
| 22 | +import org.springframework.http.HttpOutputMessage; |
| 23 | +import org.springframework.http.MediaType; |
| 24 | +import org.springframework.http.converter.AbstractHttpMessageConverter; |
| 25 | +import org.springframework.http.converter.GenericHttpMessageConverter; |
| 26 | +import org.springframework.http.converter.HttpMessageConverter; |
| 27 | +import org.springframework.http.converter.HttpMessageNotReadableException; |
| 28 | +import org.springframework.http.converter.HttpMessageNotWritableException; |
| 29 | +import org.springframework.security.oauth2.core.converter.ClaimConversionService; |
| 30 | +import org.springframework.security.oauth2.core.converter.ClaimTypeConverter; |
| 31 | +import org.springframework.security.oauth2.core.converter.ObjectToSetStringConverter2; |
| 32 | +import org.springframework.security.oauth2.core.oidc.OidcProviderConfiguration; |
| 33 | +import org.springframework.security.oauth2.core.oidc.OidcProviderMetadataClaimNames; |
| 34 | +import org.springframework.util.Assert; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.net.URL; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Set; |
| 41 | + |
| 42 | + |
| 43 | +/** |
| 44 | + * A {@link HttpMessageConverter} for an {@link OidcProviderConfiguration OpenID Provider Configuration Metadata}. |
| 45 | + * |
| 46 | + * @author Daniel Garnier-Moiroux |
| 47 | + * @since 0.1.0 |
| 48 | + * @see AbstractHttpMessageConverter |
| 49 | + * @see OidcProviderConfiguration |
| 50 | + */ |
| 51 | +public class OidcProviderConfigurationHttpMessageConverter |
| 52 | + extends AbstractHttpMessageConverter<OidcProviderConfiguration> { |
| 53 | + private static final ParameterizedTypeReference<Map<String, Object>> STRING_OBJECT_MAP = |
| 54 | + new ParameterizedTypeReference<Map<String, Object>>() { |
| 55 | + }; |
| 56 | + |
| 57 | + private final GenericHttpMessageConverter<Object> jsonMessageConverter = HttpMessageConverters.getJsonMessageConverter(); |
| 58 | + |
| 59 | + private Converter<Map<String, Object>, OidcProviderConfiguration> providerConfigurationConverter = new OidcProviderConfigurationConverter(); |
| 60 | + private Converter<OidcProviderConfiguration, Map<String, Object>> providerConfigurationParametersConverter = OidcProviderConfiguration::getClaims; |
| 61 | + |
| 62 | + public OidcProviderConfigurationHttpMessageConverter() { |
| 63 | + super(MediaType.APPLICATION_JSON, new MediaType("application", "*+json")); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected boolean supports(Class<?> clazz) { |
| 68 | + return OidcProviderConfiguration.class.isAssignableFrom(clazz); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + @SuppressWarnings("unchecked") |
| 73 | + protected OidcProviderConfiguration readInternal(Class<? extends OidcProviderConfiguration> clazz, HttpInputMessage inputMessage) |
| 74 | + throws HttpMessageNotReadableException { |
| 75 | + try { |
| 76 | + Map<String, Object> providerConfigurationParameters = (Map<String, Object>) this.jsonMessageConverter.read(STRING_OBJECT_MAP.getType(), null, inputMessage); |
| 77 | + return this.providerConfigurationConverter.convert(providerConfigurationParameters); |
| 78 | + } catch (Exception ex) { |
| 79 | + throw new HttpMessageNotReadableException( |
| 80 | + "An error occurred reading the OpenID Provider Configuration: " + ex.getMessage(), ex, inputMessage); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected void writeInternal(OidcProviderConfiguration providerConfiguration, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { |
| 86 | + try { |
| 87 | + Map<String, Object> providerConfigurationResponseParameters = |
| 88 | + this.providerConfigurationParametersConverter.convert(providerConfiguration); |
| 89 | + this.jsonMessageConverter.write( |
| 90 | + providerConfigurationResponseParameters, |
| 91 | + STRING_OBJECT_MAP.getType(), |
| 92 | + MediaType.APPLICATION_JSON, |
| 93 | + outputMessage |
| 94 | + ); |
| 95 | + } catch (Exception ex) { |
| 96 | + throw new HttpMessageNotWritableException( |
| 97 | + "An error occurred writing the OpenID Provider Configuration: " + ex.getMessage(), ex); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Sets the {@link Converter} used for converting the {@link OidcProviderConfiguration} to a |
| 103 | + * {@code Map} representation of the OpenID Provider Configuration. |
| 104 | + * |
| 105 | + * @param providerConfigurationParametersConverter the {@link Converter} used for converting to a |
| 106 | + * {@code Map} representation of the OpenID Provider Configuration |
| 107 | + */ |
| 108 | + public final void setProviderConfigurationParametersConverter( |
| 109 | + Converter<OidcProviderConfiguration, Map<String, Object>> providerConfigurationParametersConverter) { |
| 110 | + Assert.notNull(providerConfigurationParametersConverter, "providerConfigurationParametersConverter cannot be null"); |
| 111 | + this.providerConfigurationParametersConverter = providerConfigurationParametersConverter; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Sets the {@link Converter} used for converting the OpenID Provider Configuration parameters |
| 116 | + * to an {@link OidcProviderConfiguration}. |
| 117 | + * |
| 118 | + * @param providerConfigurationConverter the {@link Converter} used for converting to an |
| 119 | + * {@link OidcProviderConfiguration} |
| 120 | + */ |
| 121 | + public final void setProviderConfigurationConverter(Converter<Map<String, Object>, OidcProviderConfiguration> providerConfigurationConverter) { |
| 122 | + Assert.notNull(providerConfigurationConverter, "providerConfigurationConverter cannot be null"); |
| 123 | + this.providerConfigurationConverter = providerConfigurationConverter; |
| 124 | + } |
| 125 | + |
| 126 | + private static final class OidcProviderConfigurationConverter implements Converter<Map<String, Object>, OidcProviderConfiguration> { |
| 127 | + private static final ClaimConversionService CLAIM_CONVERSION_SERVICE = ClaimConversionService.getSharedInstance(); |
| 128 | + private static final TypeDescriptor OBJECT_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Object.class); |
| 129 | + private static final TypeDescriptor STRING_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(String.class); |
| 130 | + private static final TypeDescriptor URL_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(URL.class); |
| 131 | + private final ClaimTypeConverter claimTypeConverter; |
| 132 | + |
| 133 | + OidcProviderConfigurationConverter() { |
| 134 | + CLAIM_CONVERSION_SERVICE.addConverter(new ObjectToSetStringConverter2()); |
| 135 | + Map<String, Converter<Object, ?>> claimNameToConverter = new HashMap<>(); |
| 136 | + Converter<Object, ?> setStringConverter = getConverter(TypeDescriptor.collection(Set.class, STRING_TYPE_DESCRIPTOR)); |
| 137 | + Converter<Object, ?> urlConverter = getConverter(URL_TYPE_DESCRIPTOR); |
| 138 | + |
| 139 | + claimNameToConverter.put(OidcProviderMetadataClaimNames.ISSUER, urlConverter); |
| 140 | + claimNameToConverter.put(OidcProviderMetadataClaimNames.AUTHORIZATION_ENDPOINT, urlConverter); |
| 141 | + claimNameToConverter.put(OidcProviderMetadataClaimNames.TOKEN_ENDPOINT, urlConverter); |
| 142 | + claimNameToConverter.put(OidcProviderMetadataClaimNames.JWKS_URI, urlConverter); |
| 143 | + claimNameToConverter.put(OidcProviderMetadataClaimNames.GRANT_TYPES_SUPPORTED, setStringConverter); |
| 144 | + claimNameToConverter.put(OidcProviderMetadataClaimNames.TOKEN_ENDPOINT_AUTH_METHODS_SUPPORTED, setStringConverter); |
| 145 | + claimNameToConverter.put(OidcProviderMetadataClaimNames.SUBJECT_TYPES_SUPPORTED, setStringConverter); |
| 146 | + claimNameToConverter.put(OidcProviderMetadataClaimNames.SCOPES_SUPPORTED, setStringConverter); |
| 147 | + claimNameToConverter.put(OidcProviderMetadataClaimNames.RESPONSE_TYPES_SUPPORTED, setStringConverter); |
| 148 | + this.claimTypeConverter = new ClaimTypeConverter(claimNameToConverter); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public OidcProviderConfiguration convert(Map<String, Object> source) { |
| 153 | + Map<String, Object> parsedClaims = this.claimTypeConverter.convert(source); |
| 154 | + return OidcProviderConfiguration.withClaims(parsedClaims).build(); |
| 155 | + } |
| 156 | + |
| 157 | + private static Converter<Object, ?> getConverter(TypeDescriptor targetDescriptor) { |
| 158 | + return (source) -> CLAIM_CONVERSION_SERVICE.convert(source, OBJECT_TYPE_DESCRIPTOR, targetDescriptor); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments