|
| 1 | +/* |
| 2 | + * Copyright 2020-2021 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.server.authorization.authentication; |
| 17 | + |
| 18 | +import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator; |
| 19 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 20 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 21 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 22 | +import org.springframework.security.oauth2.core.OAuth2TokenValidator; |
| 23 | +import org.springframework.security.oauth2.jose.jws.JwsAlgorithm; |
| 24 | +import org.springframework.security.oauth2.jose.jws.MacAlgorithm; |
| 25 | +import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm; |
| 26 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 27 | +import org.springframework.security.oauth2.jwt.JwtClaimValidator; |
| 28 | +import org.springframework.security.oauth2.jwt.JwtDecoder; |
| 29 | +import org.springframework.security.oauth2.jwt.JwtDecoderFactory; |
| 30 | +import org.springframework.security.oauth2.jwt.JwtTimestampValidator; |
| 31 | +import org.springframework.security.oauth2.jwt.NimbusJwtDecoder; |
| 32 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 33 | +import org.springframework.security.oauth2.server.authorization.config.ProviderSettings; |
| 34 | +import org.springframework.util.Assert; |
| 35 | +import org.springframework.util.StringUtils; |
| 36 | + |
| 37 | +import javax.crypto.spec.SecretKeySpec; |
| 38 | +import java.nio.charset.StandardCharsets; |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Map; |
| 43 | +import java.util.Objects; |
| 44 | +import java.util.concurrent.ConcurrentHashMap; |
| 45 | +import java.util.function.Function; |
| 46 | + |
| 47 | +/** |
| 48 | + * Creates JWT decoders for registered clients |
| 49 | + * |
| 50 | + * @author Rafal Lewczuk |
| 51 | + * @since 0.2.1 |
| 52 | + */ |
| 53 | +final class RegisteredClientJwtAssertionDecoderFactory implements JwtDecoderFactory<RegisteredClient> { |
| 54 | + |
| 55 | + private static final String CLIENT_ASSERTION_ERROR_URI = "https://datatracker.ietf.org/doc/html/rfc7523#section-3"; |
| 56 | + |
| 57 | + private static final Map<JwsAlgorithm, String> JCA_ALGORITHM_MAPPINGS; |
| 58 | + |
| 59 | + private final String tokenEndpointUri; |
| 60 | + |
| 61 | + static { |
| 62 | + Map<JwsAlgorithm, String> mappings = new HashMap<>(); |
| 63 | + mappings.put(MacAlgorithm.HS256, "HmacSHA256"); |
| 64 | + mappings.put(MacAlgorithm.HS384, "HmacSHA384"); |
| 65 | + mappings.put(MacAlgorithm.HS512, "HmacSHA512"); |
| 66 | + JCA_ALGORITHM_MAPPINGS = Collections.unmodifiableMap(mappings); |
| 67 | + } |
| 68 | + |
| 69 | + private final Function<RegisteredClient, JwsAlgorithm> jwsAlgorithmResolver = |
| 70 | + rc -> rc.getClientSettings().getTokenEndpointSigningAlgorithm(); |
| 71 | + |
| 72 | + private final Map<String, CachedJwtDecoder> cachedDecoders = new ConcurrentHashMap<>(); |
| 73 | + |
| 74 | + RegisteredClientJwtAssertionDecoderFactory(ProviderSettings providerSettings) { |
| 75 | + Assert.notNull(providerSettings, "providerSettings cannot be null"); |
| 76 | + this.tokenEndpointUri = providerSettings.getIssuer() + providerSettings.getTokenEndpoint(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public JwtDecoder createDecoder(RegisteredClient registeredClient) { |
| 81 | + Assert.notNull(registeredClient, "registeredClient cannot be null"); |
| 82 | + |
| 83 | + CachedJwtDecoder cachedDecoder = this.cachedDecoders.get(registeredClient.getClientId()); |
| 84 | + if (cachedDecoder != null && |
| 85 | + Objects.equals(registeredClient.getClientSettings().getTokenEndpointSigningAlgorithm(), |
| 86 | + cachedDecoder.registeredClient.getClientSettings().getTokenEndpointSigningAlgorithm()) && |
| 87 | + Objects.equals(cachedDecoder.registeredClient.getClientSecret(), registeredClient.getClientSecret()) && |
| 88 | + Objects.equals(registeredClient.getClientSettings().getJwkSetUrl(), |
| 89 | + cachedDecoder.registeredClient.getClientSettings().getJwkSetUrl())) { |
| 90 | + return cachedDecoder.jwtDecoder; |
| 91 | + } |
| 92 | + |
| 93 | + cachedDecoder = new CachedJwtDecoder(buildDecoder(registeredClient), registeredClient); |
| 94 | + cachedDecoder.jwtDecoder.setJwtValidator(createTokenValidator(registeredClient)); |
| 95 | + this.cachedDecoders.put(registeredClient.getClientId(), cachedDecoder); |
| 96 | + return cachedDecoder.jwtDecoder; |
| 97 | + } |
| 98 | + |
| 99 | + private NimbusJwtDecoder buildDecoder(RegisteredClient registeredClient) { |
| 100 | + JwsAlgorithm jwsAlgorithm = this.jwsAlgorithmResolver.apply(registeredClient); |
| 101 | + |
| 102 | + if (jwsAlgorithm != null && SignatureAlgorithm.class.isAssignableFrom(jwsAlgorithm.getClass())) { |
| 103 | + String jwkSetUrl = registeredClient.getClientSettings().getJwkSetUrl(); |
| 104 | + if (!StringUtils.hasText(jwkSetUrl)) { |
| 105 | + OAuth2Error oauth2Error = new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT, |
| 106 | + "misconfigured client", CLIENT_ASSERTION_ERROR_URI); |
| 107 | + throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString()); |
| 108 | + } |
| 109 | + return NimbusJwtDecoder.withJwkSetUri(jwkSetUrl).jwsAlgorithm((SignatureAlgorithm) jwsAlgorithm).build(); |
| 110 | + } |
| 111 | + |
| 112 | + if (jwsAlgorithm != null && MacAlgorithm.class.isAssignableFrom(jwsAlgorithm.getClass())) { |
| 113 | + String clientSecret = registeredClient.getClientSecret(); |
| 114 | + if (!StringUtils.hasText(clientSecret)) { |
| 115 | + OAuth2Error oauth2Error = new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT, |
| 116 | + "misconfigured client", CLIENT_ASSERTION_ERROR_URI); |
| 117 | + throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString()); |
| 118 | + } |
| 119 | + SecretKeySpec secretKeySpec = new SecretKeySpec(clientSecret.getBytes(StandardCharsets.UTF_8), |
| 120 | + JCA_ALGORITHM_MAPPINGS.get(jwsAlgorithm)); |
| 121 | + return NimbusJwtDecoder.withSecretKey(secretKeySpec).macAlgorithm((MacAlgorithm) jwsAlgorithm).build(); |
| 122 | + } |
| 123 | + |
| 124 | + OAuth2Error oauth2Error = new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT, |
| 125 | + "misconfigured client", CLIENT_ASSERTION_ERROR_URI); |
| 126 | + throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString()); |
| 127 | + } |
| 128 | + |
| 129 | + private OAuth2TokenValidator<Jwt> createTokenValidator(RegisteredClient registeredClient) { |
| 130 | + String clientId = registeredClient.getClientId(); |
| 131 | + return new DelegatingOAuth2TokenValidator<>( |
| 132 | + new JwtClaimValidator<String>("iss", clientId::equals), // RFC 7523 section 3 (iss) |
| 133 | + new JwtClaimValidator<String>("sub", clientId::equals), // RFC 7523 section 3 (sub) |
| 134 | + new JwtClaimValidator<List<String>>("aud", l -> l.contains(tokenEndpointUri)), // RFC 7523 section 3 (aud) |
| 135 | + new JwtClaimValidator<>("exp", Objects::nonNull), // RFC 7523 section 3 (exp != null) |
| 136 | + new JwtTimestampValidator() // RFC 7523 section 3 (exp, nbf) |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + private static class CachedJwtDecoder { |
| 141 | + private final NimbusJwtDecoder jwtDecoder; |
| 142 | + private final RegisteredClient registeredClient; |
| 143 | + |
| 144 | + CachedJwtDecoder(NimbusJwtDecoder jwtDecoder, RegisteredClient registeredClient) { |
| 145 | + this.jwtDecoder = jwtDecoder; |
| 146 | + this.registeredClient = registeredClient; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments