|
| 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.ClientAuthenticationMethod; |
| 19 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 20 | +import org.springframework.security.oauth2.jose.jws.JwsAlgorithm; |
| 21 | +import org.springframework.security.oauth2.jose.jws.JwsAlgorithms; |
| 22 | +import org.springframework.security.oauth2.jose.jws.MacAlgorithm; |
| 23 | +import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm; |
| 24 | +import org.springframework.security.oauth2.jwt.JwtDecoder; |
| 25 | +import org.springframework.security.oauth2.jwt.JwtDecoderFactory; |
| 26 | +import org.springframework.security.oauth2.jwt.NimbusJwtDecoder; |
| 27 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 28 | +import org.springframework.security.oauth2.server.authorization.config.ClientSettings; |
| 29 | +import org.springframework.security.oauth2.server.authorization.config.ProviderSettings; |
| 30 | +import org.springframework.util.Assert; |
| 31 | + |
| 32 | +import javax.crypto.spec.SecretKeySpec; |
| 33 | +import java.nio.charset.StandardCharsets; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Objects; |
| 38 | +import java.util.concurrent.ConcurrentHashMap; |
| 39 | + |
| 40 | +/** |
| 41 | + * Creates JWT decoders for registered clients |
| 42 | + * |
| 43 | + * @author Rafal Lewczuk |
| 44 | + * @since 0.2.1 |
| 45 | + */ |
| 46 | +final class RegisteredClientJwtAssertionDecoderFactory implements JwtDecoderFactory<RegisteredClient> { |
| 47 | + |
| 48 | + private static final Map<String, String> JCA_ALGORITHM_MAPPINGS; |
| 49 | + |
| 50 | + static { |
| 51 | + Map<String, String> mappings = new HashMap<>(); |
| 52 | + mappings.put(JwsAlgorithms.HS256, "HmacSHA256"); |
| 53 | + mappings.put(JwsAlgorithms.HS384, "HmacSHA384"); |
| 54 | + mappings.put(JwsAlgorithms.HS512, "HmacSHA512"); |
| 55 | + JCA_ALGORITHM_MAPPINGS = Collections.unmodifiableMap(mappings); |
| 56 | + } |
| 57 | + |
| 58 | + private final Map<String, CachedJwtDecoder> cachedDecoders = new ConcurrentHashMap<>(); |
| 59 | + private final ProviderSettings providerSettings; |
| 60 | + |
| 61 | + RegisteredClientJwtAssertionDecoderFactory(ProviderSettings providerSettings) { |
| 62 | + Assert.notNull(providerSettings, "providerSettings cannot be null"); |
| 63 | + this.providerSettings = providerSettings; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public JwtDecoder createDecoder(RegisteredClient registeredClient) { |
| 68 | + Assert.notNull(registeredClient, "registeredClient cannot be null"); |
| 69 | + |
| 70 | + JwsAlgorithm signingAlgorithm = registeredClient.getClientSettings().getTokenEndpointSigningAlgorithm(); |
| 71 | + boolean isHmac = signingAlgorithm instanceof MacAlgorithm; |
| 72 | + |
| 73 | + // No fancy .computerIfAbsent() calls as we need to check if client configuration has changed |
| 74 | + // also this is idempotent operation so we don't have to care about exact synchronization |
| 75 | + CachedJwtDecoder cachedDecoder = this.cachedDecoders.get(registeredClient.getClientId()); |
| 76 | + if (cachedDecoder != null) { |
| 77 | + ClientSettings clientSettings = cachedDecoder.registeredClient.getClientSettings(); |
| 78 | + ClientSettings cachedSettings = registeredClient.getClientSettings(); |
| 79 | + if (isHmac) { |
| 80 | + if (Objects.equals(cachedSettings.getTokenEndpointSigningAlgorithm(), clientSettings.getTokenEndpointSigningAlgorithm()) |
| 81 | + && Objects.equals(cachedDecoder.registeredClient.getClientSecret(), registeredClient.getClientSecret())) { |
| 82 | + return cachedDecoder.jwtDecoder; |
| 83 | + } |
| 84 | + } else { |
| 85 | + if (Objects.equals(cachedSettings.getTokenEndpointSigningAlgorithm(), clientSettings.getTokenEndpointSigningAlgorithm()) |
| 86 | + && Objects.equals(cachedSettings.getJwkSetUrl(), clientSettings.getJwkSetUrl())) { |
| 87 | + return cachedDecoder.jwtDecoder; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + cachedDecoder = isHmac ? createClientSecretJwtDecoder(registeredClient, (MacAlgorithm) signingAlgorithm) |
| 93 | + : createPrivateKeyJwtDecoder(registeredClient, (SignatureAlgorithm) signingAlgorithm); |
| 94 | + |
| 95 | + this.providerSettings.getTokenEndpoint(); |
| 96 | + cachedDecoder.jwtDecoder.setJwtValidator( |
| 97 | + new RegisteredClientJwtAssertionValidator(registeredClient, this.providerSettings)); |
| 98 | + this.cachedDecoders.put(registeredClient.getClientId(), cachedDecoder); |
| 99 | + return cachedDecoder.jwtDecoder; |
| 100 | + } |
| 101 | + |
| 102 | + private CachedJwtDecoder createClientSecretJwtDecoder(RegisteredClient registeredClient, MacAlgorithm macAlgorithm) { |
| 103 | + NimbusJwtDecoder jwtDecoder = null; |
| 104 | + if (!registeredClient.getClientAuthenticationMethods().contains(ClientAuthenticationMethod.CLIENT_SECRET_JWT)) { |
| 105 | + OAuth2ClientAuthenticationProvider.throwInvalidClient(OAuth2ParameterNames.CLIENT_ASSERTION); |
| 106 | + } |
| 107 | + |
| 108 | + if (registeredClient.getClientSecret() == null || registeredClient.getClientSecret().isEmpty()) { |
| 109 | + OAuth2ClientAuthenticationProvider.throwInvalidClient(OAuth2ParameterNames.CLIENT_ASSERTION); |
| 110 | + } |
| 111 | + |
| 112 | + try { |
| 113 | + byte[] secret = registeredClient.getClientSecret().getBytes(StandardCharsets.UTF_8); |
| 114 | + String algorithmName = JCA_ALGORITHM_MAPPINGS.get(macAlgorithm.getName()); |
| 115 | + SecretKeySpec secretKey = new SecretKeySpec(secret, algorithmName); |
| 116 | + NimbusJwtDecoder.SecretKeyJwtDecoderBuilder jwtDecoderBuilder = NimbusJwtDecoder.withSecretKey(secretKey) |
| 117 | + .macAlgorithm(macAlgorithm); |
| 118 | + jwtDecoder = jwtDecoderBuilder.build(); |
| 119 | + } catch (Exception e) { |
| 120 | + OAuth2ClientAuthenticationProvider.throwInvalidClient(OAuth2ParameterNames.CLIENT_ASSERTION); |
| 121 | + } |
| 122 | + return new CachedJwtDecoder(jwtDecoder, registeredClient); |
| 123 | + } |
| 124 | + |
| 125 | + private CachedJwtDecoder createPrivateKeyJwtDecoder(RegisteredClient registeredClient, SignatureAlgorithm signatureAlgorithm) { |
| 126 | + NimbusJwtDecoder jwtDecoder = null; |
| 127 | + if (!registeredClient.getClientAuthenticationMethods().contains(ClientAuthenticationMethod.PRIVATE_KEY_JWT)) { |
| 128 | + OAuth2ClientAuthenticationProvider.throwInvalidClient(OAuth2ParameterNames.CLIENT_ASSERTION); |
| 129 | + } |
| 130 | + |
| 131 | + String jwksUrl = registeredClient.getClientSettings().getJwkSetUrl(); |
| 132 | + if (jwksUrl == null) { |
| 133 | + OAuth2ClientAuthenticationProvider.throwInvalidClient(OAuth2ParameterNames.CLIENT_ASSERTION); |
| 134 | + } |
| 135 | + |
| 136 | + try { |
| 137 | + NimbusJwtDecoder.JwkSetUriJwtDecoderBuilder jwtDecoderBuilder = NimbusJwtDecoder.withJwkSetUri(jwksUrl) |
| 138 | + .jwsAlgorithm(signatureAlgorithm); |
| 139 | + jwtDecoder = jwtDecoderBuilder.build(); |
| 140 | + } catch (Exception e) { |
| 141 | + OAuth2ClientAuthenticationProvider.throwInvalidClient(OAuth2ParameterNames.CLIENT_ASSERTION); |
| 142 | + } |
| 143 | + return new CachedJwtDecoder(jwtDecoder, registeredClient); |
| 144 | + } |
| 145 | + |
| 146 | + private static class CachedJwtDecoder { |
| 147 | + private final NimbusJwtDecoder jwtDecoder; |
| 148 | + private final RegisteredClient registeredClient; |
| 149 | + |
| 150 | + CachedJwtDecoder(NimbusJwtDecoder jwtDecoder, RegisteredClient registeredClient) { |
| 151 | + this.jwtDecoder = jwtDecoder; |
| 152 | + this.registeredClient = registeredClient; |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments