Skip to content

Commit f75fb0d

Browse files
committed
Fix JWK Thumbprint calculation to conform to RFC7638
Just used the nimbus JOSE library to do it, because it already has a compliant implementation. Signed-off-by: David Kowis <[email protected]>
1 parent 6273ba5 commit f75fb0d

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
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
@@ -193,25 +193,22 @@ public OAuth2TokenValidatorResult validate(Jwt jwt) {
193193
return OAuth2TokenValidatorResult.failure(error);
194194
}
195195

196-
PublicKey publicKey = null;
196+
JWK jwk = null;
197197
@SuppressWarnings("unchecked")
198198
Map<String, Object> jwkJson = (Map<String, Object>) jwt.getHeaders().get("jwk");
199199
try {
200-
JWK jwk = JWK.parse(jwkJson);
201-
if (jwk instanceof AsymmetricJWK) {
202-
publicKey = ((AsymmetricJWK) jwk).toPublicKey();
203-
}
200+
jwk = JWK.parse(jwkJson);
204201
}
205202
catch (Exception ignored) {
206203
}
207-
if (publicKey == null) {
204+
if (jwk == null) {
208205
OAuth2Error error = createOAuth2Error("jwk header is missing or invalid.");
209206
return OAuth2TokenValidatorResult.failure(error);
210207
}
211208

212209
String jwkThumbprint;
213210
try {
214-
jwkThumbprint = computeSHA256(publicKey);
211+
jwkThumbprint = jwk.computeThumbprint().toString();
215212
}
216213
catch (Exception ex) {
217214
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 & 6 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
}

0 commit comments

Comments
 (0)