Skip to content

Commit b906044

Browse files
fix: use claim constructor to set leeway and consider it during initial validation
1 parent ab00f2d commit b906044

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

src/Claims/DatetimeTrait.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ trait DatetimeTrait
2525
*/
2626
protected $leeway = 0;
2727

28+
/**
29+
* @param mixed $value
30+
* @param int $leeway
31+
*
32+
* @return void
33+
*/
34+
public function __construct($value, $leeway = 0)
35+
{
36+
$this->leeway = $leeway;
37+
38+
parent::__construct($value);
39+
}
40+
2841
/**
2942
* Set the claim value, and call a validate method.
3043
*

src/Claims/Factory.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ public function __construct(Request $request)
7676
public function get($name, $value)
7777
{
7878
if ($this->has($name)) {
79-
$claim = new $this->classMap[$name]($value);
80-
81-
return method_exists($claim, 'setLeeway') ?
82-
$claim->setLeeway($this->leeway) :
83-
$claim;
79+
return new $this->classMap[$name]($value, $this->leeway);
8480
}
8581

8682
return new Custom($name, $value);

tests/Claims/DatetimeClaimTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Tymon\JWTAuth\Claims\JwtId;
2525
use Tymon\JWTAuth\Claims\NotBefore;
2626
use Tymon\JWTAuth\Claims\Subject;
27+
use Tymon\JWTAuth\Exceptions\JWTException;
2728
use Tymon\JWTAuth\Payload;
2829
use Tymon\JWTAuth\Test\AbstractTestCase;
2930
use Tymon\JWTAuth\Validators\PayloadValidator;
@@ -57,6 +58,37 @@ public function setUp(): void
5758
];
5859
}
5960

61+
/** @test */
62+
public function it_should_return_same_class_instance_when_setting_the_leeway()
63+
{
64+
$exp = new Expiration($this->testNowTimestamp + 3600);
65+
$nbf = new NotBefore($this->testNowTimestamp);
66+
$iat = new IssuedAt($this->testNowTimestamp);
67+
68+
$this->assertInstanceOf(Expiration::class, $exp->setLeeway(5));
69+
$this->assertInstanceOf(NotBefore::class, $nbf->setLeeway(5));
70+
$this->assertInstanceOf(IssuedAt::class, $iat->setLeeway(5));
71+
}
72+
73+
/** @test */
74+
public function it_should_consider_the_leeway_when_performing_validations()
75+
{
76+
$futureTimestamp = $this->testNowTimestamp + 5;
77+
$pastTimestmap = $this->testNowTimestamp - 5;
78+
79+
try {
80+
$exp = new Expiration($pastTimestmap, 10);
81+
$nbf = new NotBefore($futureTimestamp, 10);
82+
$iat = new IssuedAt($futureTimestamp, 10);
83+
84+
$exp->validatePayload();
85+
$nbf->validatePayload();
86+
$iat->validatePayload();
87+
} catch (JWTException $ignored) {
88+
$this->fail("Failed asserting that the leeway is considered when validating tokens");
89+
}
90+
}
91+
6092
/** @test */
6193
public function it_should_handle_carbon_claims()
6294
{

tests/Claims/FactoryTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ public function it_should_get_the_ttl()
5757
$this->assertSame($ttl, $this->factory->getTTL());
5858
}
5959

60+
/** @test */
61+
public function it_should_return_same_class_instance_when_setting_the_leeway()
62+
{
63+
$this->assertInstanceOf(Factory::class, $this->factory->setLeeway(5));
64+
}
65+
6066
/** @test */
6167
public function it_should_get_a_defined_claim_instance_when_passing_a_name_and_value()
6268
{

0 commit comments

Comments
 (0)