|
16 | 16 | package org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers; |
17 | 17 |
|
18 | 18 | import java.security.MessageDigest; |
19 | | -import java.security.PublicKey; |
20 | 19 | import java.security.cert.X509Certificate; |
21 | 20 | import java.util.Base64; |
22 | 21 | import java.util.Collections; |
23 | 22 | import java.util.HashMap; |
24 | 23 | import java.util.LinkedHashMap; |
25 | 24 | import java.util.Map; |
26 | 25 |
|
27 | | -import com.nimbusds.jose.jwk.AsymmetricJWK; |
28 | 26 | import com.nimbusds.jose.jwk.JWK; |
29 | 27 |
|
30 | 28 | import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
@@ -91,25 +89,22 @@ private static void customize(OAuth2TokenContext tokenContext, Map<String, Objec |
91 | 89 | // Add 'cnf' claim for OAuth 2.0 Demonstrating Proof of Possession (DPoP) |
92 | 90 | Jwt dPoPProofJwt = tokenContext.get(OAuth2TokenContext.DPOP_PROOF_KEY); |
93 | 91 | if (OAuth2TokenType.ACCESS_TOKEN.equals(tokenContext.getTokenType()) && dPoPProofJwt != null) { |
94 | | - PublicKey publicKey = null; |
| 92 | + JWK jwk = null; |
95 | 93 | @SuppressWarnings("unchecked") |
96 | 94 | Map<String, Object> jwkJson = (Map<String, Object>) dPoPProofJwt.getHeaders().get("jwk"); |
97 | 95 | try { |
98 | | - JWK jwk = JWK.parse(jwkJson); |
99 | | - if (jwk instanceof AsymmetricJWK asymmetricJWK) { |
100 | | - publicKey = asymmetricJWK.toPublicKey(); |
101 | | - } |
| 96 | + jwk = JWK.parse(jwkJson); |
102 | 97 | } |
103 | 98 | catch (Exception ignored) { |
104 | 99 | } |
105 | | - if (publicKey == null) { |
| 100 | + if (jwk == null) { |
106 | 101 | OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.INVALID_DPOP_PROOF, |
107 | 102 | "jwk header is missing or invalid.", null); |
108 | 103 | throw new OAuth2AuthenticationException(error); |
109 | 104 | } |
110 | 105 |
|
111 | 106 | try { |
112 | | - String sha256Thumbprint = computeSHA256Thumbprint(publicKey); |
| 107 | + String sha256Thumbprint = jwk.computeThumbprint().toString(); |
113 | 108 | if (cnfClaims == null) { |
114 | 109 | cnfClaims = new HashMap<>(); |
115 | 110 | } |
@@ -149,10 +144,4 @@ private static String computeSHA256Thumbprint(X509Certificate x509Certificate) t |
149 | 144 | return Base64.getUrlEncoder().withoutPadding().encodeToString(digest); |
150 | 145 | } |
151 | 146 |
|
152 | | - private static String computeSHA256Thumbprint(PublicKey publicKey) throws Exception { |
153 | | - MessageDigest md = MessageDigest.getInstance("SHA-256"); |
154 | | - byte[] digest = md.digest(publicKey.getEncoded()); |
155 | | - return Base64.getUrlEncoder().withoutPadding().encodeToString(digest); |
156 | | - } |
157 | | - |
158 | 147 | } |
0 commit comments