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