|
| 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.token; |
| 17 | + |
| 18 | +import java.security.MessageDigest; |
| 19 | +import java.security.cert.X509Certificate; |
| 20 | +import java.util.Base64; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
| 26 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 27 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 28 | +import org.springframework.security.oauth2.core.OAuth2ErrorCodes; |
| 29 | +import org.springframework.security.oauth2.server.authorization.OAuth2TokenType; |
| 30 | +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2ClientAuthenticationToken; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Joe Grandja |
| 34 | + * @since 1.3 |
| 35 | + */ |
| 36 | +final class DefaultOAuth2TokenClaimsConsumer implements Consumer<Map<String, Object>> { |
| 37 | + private static final ClientAuthenticationMethod TLS_CLIENT_AUTH_AUTHENTICATION_METHOD = |
| 38 | + new ClientAuthenticationMethod("tls_client_auth"); |
| 39 | + private static final ClientAuthenticationMethod SELF_SIGNED_TLS_CLIENT_AUTH_AUTHENTICATION_METHOD = |
| 40 | + new ClientAuthenticationMethod("self_signed_tls_client_auth"); |
| 41 | + private final OAuth2TokenContext context; |
| 42 | + |
| 43 | + DefaultOAuth2TokenClaimsConsumer(OAuth2TokenContext context) { |
| 44 | + this.context = context; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void accept(Map<String, Object> claims) { |
| 49 | + // Add 'cnf' claim for Mutual-TLS Client Certificate-Bound Access Tokens |
| 50 | + if (OAuth2TokenType.ACCESS_TOKEN.equals(this.context.getTokenType()) && |
| 51 | + this.context.getAuthorizationGrant() != null && |
| 52 | + this.context.getAuthorizationGrant().getPrincipal() instanceof OAuth2ClientAuthenticationToken clientAuthentication) { |
| 53 | + |
| 54 | + if ((TLS_CLIENT_AUTH_AUTHENTICATION_METHOD.equals(clientAuthentication.getClientAuthenticationMethod()) || |
| 55 | + SELF_SIGNED_TLS_CLIENT_AUTH_AUTHENTICATION_METHOD.equals(clientAuthentication.getClientAuthenticationMethod())) && |
| 56 | + this.context.getRegisteredClient().getTokenSettings().isX509CertificateBoundAccessTokens()) { |
| 57 | + |
| 58 | + X509Certificate[] clientCertificateChain = (X509Certificate[]) clientAuthentication.getCredentials(); |
| 59 | + try { |
| 60 | + String sha256Thumbprint = computeSHA256Thumbprint(clientCertificateChain[0]); |
| 61 | + Map<String, Object> x5tClaim = new HashMap<>(); |
| 62 | + x5tClaim.put("x5t#S256", sha256Thumbprint); |
| 63 | + claims.put("cnf", x5tClaim); |
| 64 | + } catch (Exception ex) { |
| 65 | + OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, |
| 66 | + "Failed to compute SHA-256 Thumbprint for client X509Certificate.", null); |
| 67 | + throw new OAuth2AuthenticationException(error, ex); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static String computeSHA256Thumbprint(X509Certificate x509Certificate) throws Exception { |
| 74 | + MessageDigest md = MessageDigest.getInstance("SHA-256"); |
| 75 | + byte[] digest = md.digest(x509Certificate.getEncoded()); |
| 76 | + return Base64.getUrlEncoder().withoutPadding().encodeToString(digest); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments