16
16
17
17
package org .springframework .security .oauth2 .jwt ;
18
18
19
+ import java .time .Clock ;
19
20
import java .time .Duration ;
20
21
import java .time .Instant ;
22
+ import java .time .ZoneId ;
21
23
import java .util .Collections ;
22
24
import java .util .Map ;
23
25
import java .util .UUID ;
26
+ import java .util .function .Function ;
24
27
25
28
import com .nimbusds .jose .jwk .RSAKey ;
26
29
import com .nimbusds .jose .jwk .source .JWKSource ;
27
30
import com .nimbusds .jose .proc .SecurityContext ;
28
31
import org .junit .jupiter .api .BeforeEach ;
29
32
import org .junit .jupiter .api .Test ;
30
33
34
+ import org .springframework .security .oauth2 .core .OAuth2TokenValidator ;
31
35
import org .springframework .security .oauth2 .jose .TestJwks ;
32
36
import org .springframework .security .oauth2 .jose .jws .SignatureAlgorithm ;
33
37
@@ -343,13 +347,20 @@ public void decodeWhenIatBeforeTimeWindowThenThrowBadJwtException() throws Excep
343
347
String method = "GET" ;
344
348
String targetUri = "https://resource1" ;
345
349
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
+
346
357
// @formatter:off
347
358
Map <String , Object > publicJwk = rsaJwk .toPublicJWK ().toJSONObject ();
348
359
JwsHeader jwsHeader = JwsHeader .with (SignatureAlgorithm .RS256 )
349
360
.type ("dpop+jwt" )
350
361
.jwk (publicJwk )
351
362
.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
353
364
JwtClaimsSet claims = JwtClaimsSet .builder ()
354
365
.issuedAt (issuedAt )
355
366
.claim ("htm" , method )
@@ -367,7 +378,7 @@ public void decodeWhenIatBeforeTimeWindowThenThrowBadJwtException() throws Excep
367
378
.build ();
368
379
// @formatter:on
369
380
370
- JwtDecoder jwtDecoder = this . jwtDecoderFactory .createDecoder (dPoPProofContext );
381
+ JwtDecoder jwtDecoder = jwtDecoderFactory .createDecoder (dPoPProofContext );
371
382
372
383
assertThatExceptionOfType (BadJwtException .class )
373
384
.isThrownBy (() -> jwtDecoder .decode (dPoPProofContext .getDPoPProof ()))
@@ -382,13 +393,20 @@ public void decodeWhenIatAfterTimeWindowThenThrowBadJwtException() throws Except
382
393
String method = "GET" ;
383
394
String targetUri = "https://resource1" ;
384
395
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
+
385
403
// @formatter:off
386
404
Map <String , Object > publicJwk = rsaJwk .toPublicJWK ().toJSONObject ();
387
405
JwsHeader jwsHeader = JwsHeader .with (SignatureAlgorithm .RS256 )
388
406
.type ("dpop+jwt" )
389
407
.jwk (publicJwk )
390
408
.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
392
410
JwtClaimsSet claims = JwtClaimsSet .builder ()
393
411
.issuedAt (issuedAt )
394
412
.claim ("htm" , method )
@@ -406,7 +424,7 @@ public void decodeWhenIatAfterTimeWindowThenThrowBadJwtException() throws Except
406
424
.build ();
407
425
// @formatter:on
408
426
409
- JwtDecoder jwtDecoder = this . jwtDecoderFactory .createDecoder (dPoPProofContext );
427
+ JwtDecoder jwtDecoder = jwtDecoderFactory .createDecoder (dPoPProofContext );
410
428
411
429
assertThatExceptionOfType (BadJwtException .class )
412
430
.isThrownBy (() -> jwtDecoder .decode (dPoPProofContext .getDPoPProof ()))
0 commit comments