66
77use function is_float ;
88use function is_int ;
9+ use Psr \Clock \ClockInterface ;
910
1011/**
1112 * This class is a claim checker. When the "iat" is present, it will compare the value with the current timestamp.
@@ -14,10 +15,22 @@ final class IssuedAtChecker implements ClaimChecker, HeaderChecker
1415{
1516 private const NAME = 'iat ' ;
1617
18+ private readonly ClockInterface $ clock ;
19+
1720 public function __construct (
1821 private readonly int $ allowedTimeDrift = 0 ,
19- private readonly bool $ protectedHeaderOnly = false
22+ private readonly bool $ protectedHeaderOnly = false ,
23+ ?ClockInterface $ clock = null ,
2024 ) {
25+ if ($ clock === null ) {
26+ trigger_deprecation (
27+ 'web-token/jwt-checker ' ,
28+ '3.2.0 ' ,
29+ 'The parameter "$clock" will become mandatory in 4.0.0. Please set a valid PSR Clock implementation instead of "null". '
30+ );
31+ $ clock = new InternalClock ();
32+ }
33+ $ this ->clock = $ clock ;
2134 }
2235
2336 /**
@@ -28,7 +41,10 @@ public function checkClaim(mixed $value): void
2841 if (! is_float ($ value ) && ! is_int ($ value )) {
2942 throw new InvalidClaimException ('"iat" must be an integer. ' , self ::NAME , $ value );
3043 }
31- if (time () < $ value - $ this ->allowedTimeDrift ) {
44+
45+ $ now = $ this ->clock ->now ()
46+ ->getTimestamp ();
47+ if ($ now < $ value - $ this ->allowedTimeDrift ) {
3248 throw new InvalidClaimException ('The JWT is issued in the future. ' , self ::NAME , $ value );
3349 }
3450 }
@@ -43,7 +59,10 @@ public function checkHeader(mixed $value): void
4359 if (! is_float ($ value ) && ! is_int ($ value )) {
4460 throw new InvalidHeaderException ('The header "iat" must be an integer. ' , self ::NAME , $ value );
4561 }
46- if (time () < $ value - $ this ->allowedTimeDrift ) {
62+
63+ $ now = $ this ->clock ->now ()
64+ ->getTimestamp ();
65+ if ($ now < $ value - $ this ->allowedTimeDrift ) {
4766 throw new InvalidHeaderException ('The JWT is issued in the future. ' , self ::NAME , $ value );
4867 }
4968 }
0 commit comments