-
-
Notifications
You must be signed in to change notification settings - Fork 116
Support PSR-20 (clock) #433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Jose\Component\Checker; | ||
|
||
use DateTimeImmutable; | ||
use Psr\Clock\ClockInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class InternalClock implements ClockInterface | ||
{ | ||
public function now(): DateTimeImmutable | ||
{ | ||
return new DateTimeImmutable(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
use function is_float; | ||
use function is_int; | ||
use Psr\Clock\ClockInterface; | ||
|
||
/** | ||
* 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 | |
{ | ||
private const NAME = 'iat'; | ||
|
||
private readonly ClockInterface $clock; | ||
|
||
public function __construct( | ||
private readonly int $allowedTimeDrift = 0, | ||
private readonly bool $protectedHeaderOnly = false | ||
private readonly bool $protectedHeaderOnly = false, | ||
?ClockInterface $clock = null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. having this argument as the last one means that you cannot actually remove the nullable type in 4.0 as you cannot make it mandatory without making all other arguments mandatory as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
) { | ||
if ($clock === null) { | ||
trigger_deprecation( | ||
'web-token/jwt-checker', | ||
'3.2.0', | ||
'The parameter "$clock" will become mandatory in 4.0.0. Please set a valid PSR Clock implementation instead of "null".' | ||
); | ||
$clock = new InternalClock(); | ||
} | ||
$this->clock = $clock; | ||
} | ||
|
||
/** | ||
|
@@ -28,7 +41,10 @@ public function checkClaim(mixed $value): void | |
if (! is_float($value) && ! is_int($value)) { | ||
throw new InvalidClaimException('"iat" must be an integer.', self::NAME, $value); | ||
} | ||
if (time() < $value - $this->allowedTimeDrift) { | ||
|
||
$now = $this->clock->now() | ||
->getTimestamp(); | ||
if ($now < $value - $this->allowedTimeDrift) { | ||
throw new InvalidClaimException('The JWT is issued in the future.', self::NAME, $value); | ||
} | ||
} | ||
|
@@ -43,7 +59,10 @@ public function checkHeader(mixed $value): void | |
if (! is_float($value) && ! is_int($value)) { | ||
throw new InvalidHeaderException('The header "iat" must be an integer.', self::NAME, $value); | ||
} | ||
if (time() < $value - $this->allowedTimeDrift) { | ||
|
||
$now = $this->clock->now() | ||
->getTimestamp(); | ||
if ($now < $value - $this->allowedTimeDrift) { | ||
throw new InvalidHeaderException('The JWT is issued in the future.', self::NAME, $value); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
}, | ||
"require": { | ||
"php": ">=8.1", | ||
"psr/clock": "^1.0", | ||
"web-token/jwt-core": "^3.0" | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.