Skip to content

Commit 2090f44

Browse files
dkowisjgrandja
authored andcommitted
Fix DPoP jkt claim to be JWK SHA-256 thumbprint
Just used the nimbus JOSE library to do it, because it already has a compliant implementation. Closes gh-17080 Signed-off-by: David Kowis <[email protected]>
1 parent eee7e5e commit 2090f44

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/DPoPAuthenticationProvider.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,25 +210,22 @@ public OAuth2TokenValidatorResult validate(Jwt jwt) {
210210
return OAuth2TokenValidatorResult.failure(error);
211211
}
212212

213-
PublicKey publicKey = null;
213+
JWK jwk = null;
214214
@SuppressWarnings("unchecked")
215215
Map<String, Object> jwkJson = (Map<String, Object>) jwt.getHeaders().get("jwk");
216216
try {
217-
JWK jwk = JWK.parse(jwkJson);
218-
if (jwk instanceof AsymmetricJWK) {
219-
publicKey = ((AsymmetricJWK) jwk).toPublicKey();
220-
}
217+
jwk = JWK.parse(jwkJson);
221218
}
222219
catch (Exception ignored) {
223220
}
224-
if (publicKey == null) {
221+
if (jwk == null) {
225222
OAuth2Error error = createOAuth2Error("jwk header is missing or invalid.");
226223
return OAuth2TokenValidatorResult.failure(error);
227224
}
228225

229226
String jwkThumbprint;
230227
try {
231-
jwkThumbprint = computeSHA256(publicKey);
228+
jwkThumbprint = jwk.computeThumbprint().toString();
232229
}
233230
catch (Exception ex) {
234231
OAuth2Error error = createOAuth2Error("Failed to compute SHA-256 Thumbprint for jwk.");

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/authentication/DPoPAuthenticationProviderTests.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Map;
2727
import java.util.UUID;
2828

29+
import com.nimbusds.jose.jwk.JWK;
2930
import com.nimbusds.jose.jwk.JWKSet;
3031
import com.nimbusds.jose.jwk.source.JWKSource;
3132
import com.nimbusds.jose.proc.SecurityContext;
@@ -218,8 +219,8 @@ public void authenticateWhenJktMissingThenThrowOAuth2AuthenticationException() t
218219

219220
@Test
220221
public void authenticateWhenJktDoesNotMatchThenThrowOAuth2AuthenticationException() throws Exception {
221-
// Use different client public key
222-
Jwt accessToken = generateAccessToken(TestKeys.DEFAULT_EC_KEY_PAIR.getPublic());
222+
// Use different jwk to make it not match
223+
Jwt accessToken = generateAccessToken(TestJwks.DEFAULT_EC_JWK);
223224
JwtAuthenticationToken jwtAuthenticationToken = new JwtAuthenticationToken(accessToken);
224225
given(this.tokenAuthenticationManager.authenticate(any())).willReturn(jwtAuthenticationToken);
225226

@@ -285,14 +286,14 @@ public void authenticateWhenDPoPProofValidThenSuccess() throws Exception {
285286
}
286287

287288
private Jwt generateAccessToken() {
288-
return generateAccessToken(TestKeys.DEFAULT_PUBLIC_KEY);
289+
return generateAccessToken(TestJwks.DEFAULT_RSA_JWK);
289290
}
290291

291-
private Jwt generateAccessToken(PublicKey clientPublicKey) {
292+
private Jwt generateAccessToken(JWK clientJwk) {
292293
Map<String, Object> jktClaim = null;
293-
if (clientPublicKey != null) {
294+
if (clientJwk != null) {
294295
try {
295-
String sha256Thumbprint = computeSHA256(clientPublicKey);
296+
String sha256Thumbprint = clientJwk.computeThumbprint().toString();
296297
jktClaim = new HashMap<>();
297298
jktClaim.put("jkt", sha256Thumbprint);
298299
}
@@ -321,11 +322,4 @@ private static String computeSHA256(String value) throws Exception {
321322
byte[] digest = md.digest(value.getBytes(StandardCharsets.UTF_8));
322323
return Base64.getUrlEncoder().withoutPadding().encodeToString(digest);
323324
}
324-
325-
private static String computeSHA256(PublicKey publicKey) throws Exception {
326-
MessageDigest md = MessageDigest.getInstance("SHA-256");
327-
byte[] digest = md.digest(publicKey.getEncoded());
328-
return Base64.getUrlEncoder().withoutPadding().encodeToString(digest);
329-
}
330-
331325
}

0 commit comments

Comments
 (0)