|
| 1 | +/* |
| 2 | + * Copyright 2026-2026 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.springaicommunity.mcp.security.authorizationserver.config; |
| 18 | + |
| 19 | +import java.net.URL; |
| 20 | +import java.time.Instant; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import org.jspecify.annotations.Nullable; |
| 27 | + |
| 28 | +import org.springframework.core.convert.TypeDescriptor; |
| 29 | +import org.springframework.core.convert.converter.Converter; |
| 30 | +import org.springframework.security.oauth2.core.converter.ClaimConversionService; |
| 31 | +import org.springframework.security.oauth2.core.converter.ClaimTypeConverter; |
| 32 | +import org.springframework.security.oauth2.server.authorization.OAuth2ClientMetadataClaimNames; |
| 33 | +import org.springframework.security.oauth2.server.authorization.OAuth2ClientRegistration; |
| 34 | +import org.springframework.security.oauth2.server.authorization.http.converter.OAuth2ClientRegistrationHttpMessageConverter; |
| 35 | +import org.springframework.util.StringUtils; |
| 36 | + |
| 37 | +/** |
| 38 | + * Converts dynamic client registration parameters into an |
| 39 | + * {@link OAuth2ClientRegistration}, ignoring an empty {@code scope} parameter, e.g. |
| 40 | + * {@code "scope": ""}. OAuth2 specifies that the "scope" claim should have at least 1 |
| 41 | + * character, but some clients, including the MCP Inspector 2.7.0, sends an empty scope |
| 42 | + * string. |
| 43 | + * <p> |
| 44 | + * It is lifted from |
| 45 | + * {@code OAuth2ClientRegistrationHttpMessageConverter.MapOAuth2ClientRegistrationConverter}. |
| 46 | + * <p> |
| 47 | + * For internal use only. |
| 48 | + * |
| 49 | + * @author Daniel Garnier-Moiroux |
| 50 | + * @see OAuth2ClientRegistrationHttpMessageConverter |
| 51 | + * @see <a href="https://github.com/spring-projects/spring-security/pull/19765">Spring |
| 52 | + * Security #19765</a> |
| 53 | + */ |
| 54 | +class McpEmptyScopeClientRegistrationConverter implements Converter<Map<String, Object>, OAuth2ClientRegistration> { |
| 55 | + |
| 56 | + private static final ClaimConversionService CLAIM_CONVERSION_SERVICE = ClaimConversionService.getSharedInstance(); |
| 57 | + |
| 58 | + private static final TypeDescriptor OBJECT_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Object.class); |
| 59 | + |
| 60 | + private static final TypeDescriptor STRING_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(String.class); |
| 61 | + |
| 62 | + private static final TypeDescriptor INSTANT_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(Instant.class); |
| 63 | + |
| 64 | + private static final TypeDescriptor URL_TYPE_DESCRIPTOR = TypeDescriptor.valueOf(URL.class); |
| 65 | + |
| 66 | + private static final Converter<Object, ?> INSTANT_CONVERTER = getConverter(INSTANT_TYPE_DESCRIPTOR); |
| 67 | + |
| 68 | + private final ClaimTypeConverter claimTypeConverter; |
| 69 | + |
| 70 | + @SuppressWarnings("NullAway") |
| 71 | + McpEmptyScopeClientRegistrationConverter() { |
| 72 | + Converter<Object, ?> stringConverter = getConverter(STRING_TYPE_DESCRIPTOR); |
| 73 | + Converter<Object, ?> collectionStringConverter = getConverter( |
| 74 | + TypeDescriptor.collection(Collection.class, STRING_TYPE_DESCRIPTOR)); |
| 75 | + Converter<Object, ?> urlConverter = getConverter(URL_TYPE_DESCRIPTOR); |
| 76 | + |
| 77 | + Map<String, Converter<Object, ?>> claimConverters = new HashMap<>(); |
| 78 | + claimConverters.put(OAuth2ClientMetadataClaimNames.CLIENT_ID, stringConverter); |
| 79 | + claimConverters.put(OAuth2ClientMetadataClaimNames.CLIENT_ID_ISSUED_AT, INSTANT_CONVERTER); |
| 80 | + claimConverters.put(OAuth2ClientMetadataClaimNames.CLIENT_SECRET, stringConverter); |
| 81 | + claimConverters.put(OAuth2ClientMetadataClaimNames.CLIENT_SECRET_EXPIRES_AT, |
| 82 | + McpEmptyScopeClientRegistrationConverter::convertClientSecretExpiresAt); |
| 83 | + claimConverters.put(OAuth2ClientMetadataClaimNames.CLIENT_NAME, stringConverter); |
| 84 | + claimConverters.put(OAuth2ClientMetadataClaimNames.REDIRECT_URIS, collectionStringConverter); |
| 85 | + claimConverters.put(OAuth2ClientMetadataClaimNames.TOKEN_ENDPOINT_AUTH_METHOD, stringConverter); |
| 86 | + claimConverters.put(OAuth2ClientMetadataClaimNames.GRANT_TYPES, collectionStringConverter); |
| 87 | + claimConverters.put(OAuth2ClientMetadataClaimNames.RESPONSE_TYPES, collectionStringConverter); |
| 88 | + claimConverters.put(OAuth2ClientMetadataClaimNames.SCOPE, |
| 89 | + McpEmptyScopeClientRegistrationConverter::convertScope); |
| 90 | + claimConverters.put(OAuth2ClientMetadataClaimNames.JWKS_URI, urlConverter); |
| 91 | + this.claimTypeConverter = new ClaimTypeConverter(claimConverters); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public OAuth2ClientRegistration convert(Map<String, Object> source) { |
| 96 | + Map<String, Object> parsedClaims = this.claimTypeConverter.convert(source); |
| 97 | + Object clientSecretExpiresAt = parsedClaims.get(OAuth2ClientMetadataClaimNames.CLIENT_SECRET_EXPIRES_AT); |
| 98 | + if (clientSecretExpiresAt instanceof Number && clientSecretExpiresAt.equals(0)) { |
| 99 | + parsedClaims.remove(OAuth2ClientMetadataClaimNames.CLIENT_SECRET_EXPIRES_AT); |
| 100 | + } |
| 101 | + // Update matching https://github.com/spring-projects/spring-security/pull/19765 |
| 102 | + Object scope = parsedClaims.get(OAuth2ClientMetadataClaimNames.SCOPE); |
| 103 | + if (scope instanceof Collection<?> scopes && scopes.isEmpty()) { |
| 104 | + parsedClaims.remove(OAuth2ClientMetadataClaimNames.SCOPE); |
| 105 | + } |
| 106 | + return OAuth2ClientRegistration.withClaims(parsedClaims).build(); |
| 107 | + } |
| 108 | + |
| 109 | + @SuppressWarnings("NullAway") |
| 110 | + private static Converter<Object, ?> getConverter(TypeDescriptor targetDescriptor) { |
| 111 | + return (source) -> CLAIM_CONVERSION_SERVICE.convert(source, OBJECT_TYPE_DESCRIPTOR, targetDescriptor); |
| 112 | + } |
| 113 | + |
| 114 | + private static @Nullable Instant convertClientSecretExpiresAt(Object clientSecretExpiresAt) { |
| 115 | + if (clientSecretExpiresAt != null && String.valueOf(clientSecretExpiresAt).equals("0")) { |
| 116 | + // 0 indicates that client_secret_expires_at does not expire |
| 117 | + return null; |
| 118 | + } |
| 119 | + return (Instant) INSTANT_CONVERTER.convert(clientSecretExpiresAt); |
| 120 | + } |
| 121 | + |
| 122 | + private static List<String> convertScope(@Nullable Object scope) { |
| 123 | + if (scope == null) { |
| 124 | + return List.of(); |
| 125 | + } |
| 126 | + return List.of(StringUtils.delimitedListToStringArray(scope.toString(), " ")); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments