Skip to content
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

Fix to refresh the IAT when a token is refreshed #256

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ You can find and compare releases at the GitHub release page.

## [Unreleased]
- SetSecret regenerates config with new secret in the Lcobucci provider
- Refresh iat claim when refreshing a token

### Added
- Support for lcobucci/jwt^5.0 (and dropped support for ^4.0)
Expand Down
3 changes: 2 additions & 1 deletion src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPOpenSourceSaver\JWTAuth\Exceptions\TokenBlacklistedException;
use PHPOpenSourceSaver\JWTAuth\Support\CustomClaims;
use PHPOpenSourceSaver\JWTAuth\Support\RefreshFlow;
use PHPOpenSourceSaver\JWTAuth\Support\Utils;

class Manager
{
Expand Down Expand Up @@ -181,7 +182,7 @@ protected function buildRefreshClaims(Payload $payload)
$persistentClaims,
[
'sub' => $payload['sub'],
'iat' => $payload['iat'],
'iat' => Utils::now()->timestamp,
]
);
}
Expand Down
37 changes: 37 additions & 0 deletions tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace PHPOpenSourceSaver\JWTAuth\Test;

use Illuminate\Support\Carbon;
use Mockery\LegacyMockInterface;
use PHPOpenSourceSaver\JWTAuth\Blacklist;
use PHPOpenSourceSaver\JWTAuth\Claims\Collection;
Expand Down Expand Up @@ -183,6 +184,42 @@ public function testItShouldRefreshAToken()
$this->assertEquals('baz.bar.foo', $token);
}

public function testBuildRefreshClaimsMethodWillRefreshTheIAT()
{
$claims = [
new Subject(1),
new Issuer('http://example.com'),
new Expiration($this->testNowTimestamp - 3600),
new NotBefore($this->testNowTimestamp),
new IssuedAt($this->testNowTimestamp),
new JwtId('foo'),
];
$collection = Collection::make($claims);

$this->validator->shouldReceive('setRefreshFlow->check')->andReturn($collection);
$payload = new Payload($collection, $this->validator);

$managerClass = new \ReflectionClass(Manager::class);
$buildRefreshClaimsMethod = $managerClass->getMethod('buildRefreshClaims');
$buildRefreshClaimsMethod->setAccessible(true);
$managerInstance = new Manager($this->jwt, $this->blacklist, $this->factory);

$firstResult = $buildRefreshClaimsMethod->invokeArgs($managerInstance, [$payload]);
Carbon::setTestNow(Carbon::now()->addMinutes(2));
$secondResult = $buildRefreshClaimsMethod->invokeArgs($managerInstance, [$payload]);

$this->assertIsInt($firstResult['iat']);
$this->assertIsInt($secondResult['iat']);

$carbonTimestamp = Carbon::createFromTimestamp($firstResult['iat']);
$this->assertInstanceOf(Carbon::class, $carbonTimestamp);

$carbonTimestamp = Carbon::createFromTimestamp($secondResult['iat']);
$this->assertInstanceOf(Carbon::class, $carbonTimestamp);

$this->assertNotEquals($firstResult['iat'], $secondResult['iat']);
}

/**
* @throws InvalidClaimException
*/
Expand Down