1616
1717package org .springframework .security .oauth2 .jwt ;
1818
19+ import java .time .Clock ;
1920import java .time .Duration ;
2021import java .time .Instant ;
22+ import java .time .ZoneId ;
2123import java .util .Collections ;
2224import java .util .Map ;
2325import java .util .UUID ;
26+ import java .util .function .Function ;
2427
2528import com .nimbusds .jose .jwk .RSAKey ;
2629import com .nimbusds .jose .jwk .source .JWKSource ;
2730import com .nimbusds .jose .proc .SecurityContext ;
2831import org .junit .jupiter .api .BeforeEach ;
2932import org .junit .jupiter .api .Test ;
3033
34+ import org .springframework .security .oauth2 .core .OAuth2TokenValidator ;
3135import org .springframework .security .oauth2 .jose .TestJwks ;
3236import org .springframework .security .oauth2 .jose .jws .SignatureAlgorithm ;
3337
@@ -343,13 +347,20 @@ public void decodeWhenIatBeforeTimeWindowThenThrowBadJwtException() throws Excep
343347 String method = "GET" ;
344348 String targetUri = "https://resource1" ;
345349
350+ Clock clock = Clock .fixed (Instant .now (), ZoneId .systemDefault ());
351+ JwtIssuedAtValidator issuedAtValidator = new JwtIssuedAtValidator (true );
352+ issuedAtValidator .setClock (clock );
353+ Function <DPoPProofContext , OAuth2TokenValidator <Jwt >> validatorFactory = (context ) -> issuedAtValidator ;
354+ DPoPProofJwtDecoderFactory jwtDecoderFactory = new DPoPProofJwtDecoderFactory ();
355+ jwtDecoderFactory .setJwtValidatorFactory (validatorFactory );
356+
346357 // @formatter:off
347358 Map <String , Object > publicJwk = rsaJwk .toPublicJWK ().toJSONObject ();
348359 JwsHeader jwsHeader = JwsHeader .with (SignatureAlgorithm .RS256 )
349360 .type ("dpop+jwt" )
350361 .jwk (publicJwk )
351362 .build ();
352- Instant issuedAt = Instant .now ().minus (Duration .ofSeconds (65 )); // now minus 65 seconds
363+ Instant issuedAt = Instant .now (clock ).minus (Duration .ofSeconds (65 )); // now minus 65 seconds
353364 JwtClaimsSet claims = JwtClaimsSet .builder ()
354365 .issuedAt (issuedAt )
355366 .claim ("htm" , method )
@@ -367,7 +378,7 @@ public void decodeWhenIatBeforeTimeWindowThenThrowBadJwtException() throws Excep
367378 .build ();
368379 // @formatter:on
369380
370- JwtDecoder jwtDecoder = this . jwtDecoderFactory .createDecoder (dPoPProofContext );
381+ JwtDecoder jwtDecoder = jwtDecoderFactory .createDecoder (dPoPProofContext );
371382
372383 assertThatExceptionOfType (BadJwtException .class )
373384 .isThrownBy (() -> jwtDecoder .decode (dPoPProofContext .getDPoPProof ()))
@@ -382,13 +393,20 @@ public void decodeWhenIatAfterTimeWindowThenThrowBadJwtException() throws Except
382393 String method = "GET" ;
383394 String targetUri = "https://resource1" ;
384395
396+ Clock clock = Clock .fixed (Instant .now (), ZoneId .systemDefault ());
397+ JwtIssuedAtValidator issuedAtValidator = new JwtIssuedAtValidator (true );
398+ issuedAtValidator .setClock (clock );
399+ Function <DPoPProofContext , OAuth2TokenValidator <Jwt >> validatorFactory = (context ) -> issuedAtValidator ;
400+ DPoPProofJwtDecoderFactory jwtDecoderFactory = new DPoPProofJwtDecoderFactory ();
401+ jwtDecoderFactory .setJwtValidatorFactory (validatorFactory );
402+
385403 // @formatter:off
386404 Map <String , Object > publicJwk = rsaJwk .toPublicJWK ().toJSONObject ();
387405 JwsHeader jwsHeader = JwsHeader .with (SignatureAlgorithm .RS256 )
388406 .type ("dpop+jwt" )
389407 .jwk (publicJwk )
390408 .build ();
391- Instant issuedAt = Instant .now ().plus (Duration .ofSeconds (65 )); // now plus 65 seconds
409+ Instant issuedAt = Instant .now (clock ).plus (Duration .ofSeconds (65 )); // now plus 65 seconds
392410 JwtClaimsSet claims = JwtClaimsSet .builder ()
393411 .issuedAt (issuedAt )
394412 .claim ("htm" , method )
@@ -406,7 +424,7 @@ public void decodeWhenIatAfterTimeWindowThenThrowBadJwtException() throws Except
406424 .build ();
407425 // @formatter:on
408426
409- JwtDecoder jwtDecoder = this . jwtDecoderFactory .createDecoder (dPoPProofContext );
427+ JwtDecoder jwtDecoder = jwtDecoderFactory .createDecoder (dPoPProofContext );
410428
411429 assertThatExceptionOfType (BadJwtException .class )
412430 .isThrownBy (() -> jwtDecoder .decode (dPoPProofContext .getDPoPProof ()))
0 commit comments