|
| 1 | +/* |
| 2 | + * Copyright 2020-2024 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 java.security.cert.X509Certificate; |
| 19 | +import java.util.function.Consumer; |
| 20 | + |
| 21 | +import org.apache.commons.logging.Log; |
| 22 | +import org.apache.commons.logging.LogFactory; |
| 23 | + |
| 24 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 25 | +import org.springframework.security.core.Authentication; |
| 26 | +import org.springframework.security.core.AuthenticationException; |
| 27 | +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
| 28 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 29 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 30 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 31 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 32 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; |
| 33 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 34 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; |
| 35 | +import org.springframework.security.oauth2.server.authorization.settings.ClientSettings; |
| 36 | +import org.springframework.util.Assert; |
| 37 | +import org.springframework.util.StringUtils; |
| 38 | + |
| 39 | +/** |
| 40 | + * An {@link AuthenticationProvider} implementation used for OAuth 2.0 Client Authentication, |
| 41 | + * which authenticates the client {@code X509Certificate} received when the {@code tls_client_auth} authentication method is used. |
| 42 | + * |
| 43 | + * @author Joe Grandja |
| 44 | + * @since 1.3 |
| 45 | + * @see AuthenticationProvider |
| 46 | + * @see OAuth2ClientAuthenticationToken |
| 47 | + * @see RegisteredClientRepository |
| 48 | + * @see OAuth2AuthorizationService |
| 49 | + */ |
| 50 | +public final class X509ClientCertificateAuthenticationProvider implements AuthenticationProvider { |
| 51 | + private static final String ERROR_URI = "https://datatracker.ietf.org/doc/html/rfc6749#section-3.2.1"; |
| 52 | + private static final ClientAuthenticationMethod TLS_CLIENT_AUTH_AUTHENTICATION_METHOD = |
| 53 | + new ClientAuthenticationMethod("tls_client_auth"); |
| 54 | + private final Log logger = LogFactory.getLog(getClass()); |
| 55 | + private final RegisteredClientRepository registeredClientRepository; |
| 56 | + private final CodeVerifierAuthenticator codeVerifierAuthenticator; |
| 57 | + private Consumer<OAuth2ClientAuthenticationContext> certificateVerifier = this::verifyX509CertificateSubjectDN; |
| 58 | + |
| 59 | + /** |
| 60 | + * Constructs a {@code X509ClientCertificateAuthenticationProvider} using the provided parameters. |
| 61 | + * |
| 62 | + * @param registeredClientRepository the repository of registered clients |
| 63 | + * @param authorizationService the authorization service |
| 64 | + */ |
| 65 | + public X509ClientCertificateAuthenticationProvider(RegisteredClientRepository registeredClientRepository, |
| 66 | + OAuth2AuthorizationService authorizationService) { |
| 67 | + Assert.notNull(registeredClientRepository, "registeredClientRepository cannot be null"); |
| 68 | + Assert.notNull(authorizationService, "authorizationService cannot be null"); |
| 69 | + this.registeredClientRepository = registeredClientRepository; |
| 70 | + this.codeVerifierAuthenticator = new CodeVerifierAuthenticator(authorizationService); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
| 75 | + OAuth2ClientAuthenticationToken clientAuthentication = |
| 76 | + (OAuth2ClientAuthenticationToken) authentication; |
| 77 | + |
| 78 | + if (!TLS_CLIENT_AUTH_AUTHENTICATION_METHOD.equals(clientAuthentication.getClientAuthenticationMethod())) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + String clientId = clientAuthentication.getPrincipal().toString(); |
| 83 | + RegisteredClient registeredClient = this.registeredClientRepository.findByClientId(clientId); |
| 84 | + if (registeredClient == null) { |
| 85 | + throwInvalidClient(OAuth2ParameterNames.CLIENT_ID); |
| 86 | + } |
| 87 | + |
| 88 | + if (this.logger.isTraceEnabled()) { |
| 89 | + this.logger.trace("Retrieved registered client"); |
| 90 | + } |
| 91 | + |
| 92 | + if (!registeredClient.getClientAuthenticationMethods().contains( |
| 93 | + clientAuthentication.getClientAuthenticationMethod())) { |
| 94 | + throwInvalidClient("authentication_method"); |
| 95 | + } |
| 96 | + |
| 97 | + if (!(clientAuthentication.getCredentials() instanceof X509Certificate[])) { |
| 98 | + throwInvalidClient("credentials"); |
| 99 | + } |
| 100 | + |
| 101 | + OAuth2ClientAuthenticationContext authenticationContext = |
| 102 | + OAuth2ClientAuthenticationContext.with(clientAuthentication) |
| 103 | + .registeredClient(registeredClient) |
| 104 | + .build(); |
| 105 | + this.certificateVerifier.accept(authenticationContext); |
| 106 | + |
| 107 | + if (this.logger.isTraceEnabled()) { |
| 108 | + this.logger.trace("Validated client authentication parameters"); |
| 109 | + } |
| 110 | + |
| 111 | + // Validate the "code_verifier" parameter for the confidential client, if available |
| 112 | + this.codeVerifierAuthenticator.authenticateIfAvailable(clientAuthentication, registeredClient); |
| 113 | + |
| 114 | + if (this.logger.isTraceEnabled()) { |
| 115 | + this.logger.trace("Authenticated client X509Certificate"); |
| 116 | + } |
| 117 | + |
| 118 | + return new OAuth2ClientAuthenticationToken(registeredClient, |
| 119 | + clientAuthentication.getClientAuthenticationMethod(), clientAuthentication.getCredentials()); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean supports(Class<?> authentication) { |
| 124 | + return OAuth2ClientAuthenticationToken.class.isAssignableFrom(authentication); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Sets the {@code Consumer} providing access to the {@link OAuth2ClientAuthenticationContext} |
| 129 | + * and is responsible for verifying the client {@code X509Certificate} associated in the {@link OAuth2ClientAuthenticationToken}. |
| 130 | + * The default implementation verifies the {@link ClientSettings#getX509CertificateSubjectDN() expected subject distinguished name}. |
| 131 | + * |
| 132 | + * <p> |
| 133 | + * <b>NOTE:</b> If verification fails, an {@link OAuth2AuthenticationException} MUST be thrown. |
| 134 | + * |
| 135 | + * @param certificateVerifier the {@code Consumer} providing access to the {@link OAuth2ClientAuthenticationContext} and is responsible for verifying the client {@code X509Certificate} |
| 136 | + */ |
| 137 | + public void setCertificateVerifier(Consumer<OAuth2ClientAuthenticationContext> certificateVerifier) { |
| 138 | + Assert.notNull(certificateVerifier, "certificateVerifier cannot be null"); |
| 139 | + this.certificateVerifier = certificateVerifier; |
| 140 | + } |
| 141 | + |
| 142 | + private void verifyX509CertificateSubjectDN(OAuth2ClientAuthenticationContext clientAuthenticationContext) { |
| 143 | + OAuth2ClientAuthenticationToken clientAuthentication = clientAuthenticationContext.getAuthentication(); |
| 144 | + RegisteredClient registeredClient = clientAuthenticationContext.getRegisteredClient(); |
| 145 | + X509Certificate[] clientCertificateChain = (X509Certificate[]) clientAuthentication.getCredentials(); |
| 146 | + X509Certificate clientCertificate = clientCertificateChain[0]; |
| 147 | + String expectedSubjectDN = registeredClient.getClientSettings().getX509CertificateSubjectDN(); |
| 148 | + if (!StringUtils.hasText(expectedSubjectDN) || |
| 149 | + !clientCertificate.getSubjectX500Principal().getName().equals(expectedSubjectDN)) { |
| 150 | + throwInvalidClient("x509_certificate_subject_dn"); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static void throwInvalidClient(String parameterName) { |
| 155 | + throwInvalidClient(parameterName, null); |
| 156 | + } |
| 157 | + |
| 158 | + private static void throwInvalidClient(String parameterName, Throwable cause) { |
| 159 | + OAuth2Error error = new OAuth2Error( |
| 160 | + OAuth2ErrorCodes.INVALID_CLIENT, |
| 161 | + "Client authentication failed: " + parameterName, |
| 162 | + ERROR_URI |
| 163 | + ); |
| 164 | + throw new OAuth2AuthenticationException(error, error.toString(), cause); |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments