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

feat: expiration by seconds #485

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions config/sanctum.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
| considered expired. This will override any values set in the token's
| "expires_at" attribute, but first-party sessions are not affected.
|
| To set the number of seconds, use decimal.
|
*/

'expiration' => null,
Expand Down
4 changes: 2 additions & 2 deletions src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Guard
/**
* The number of minutes tokens should be allowed to remain valid.
*
* @var int
* @var int|float
*/
protected $expiration;

Expand Down Expand Up @@ -156,7 +156,7 @@ protected function isValidAccessToken($accessToken): bool
}

$isValid =
(! $this->expiration || $accessToken->created_at->gt(now()->subMinutes($this->expiration)))
(! $this->expiration || $accessToken->created_at->gt(now()->subSeconds($this->expiration * 60)))
&& (! $accessToken->expires_at || ! $accessToken->expires_at->isPast())
&& $this->hasValidProvider($accessToken->tokenable);

Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/GuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,39 @@ public function test_authentication_with_token_succeeds_if_expires_at_not_passed
$this->assertInstanceOf(DateTimeInterface::class, $returnedUser->currentAccessToken()->last_used_at);
}

public function test_authentication_with_token_succeeds_if_expires_at_not_passed_by_seconds()
{
$factory = Mockery::mock(AuthFactory::class);

$expiration = 1 / 60 * 10;
$guard = new Guard($factory, $expiration, 'users');

$webGuard = Mockery::mock(stdClass::class);

$factory->shouldReceive('guard')
->with('web')
->andReturn($webGuard);

$webGuard->shouldReceive('user')->once()->andReturn(null);

$request = Request::create('/', 'GET');
$request->headers->set('Authorization', 'Bearer test');

$token = PersonalAccessTokenFactory::new()->for(
$user = UserFactory::new()->create(),
'tokenable'
)->create([
'name' => 'Test',
'expires_at' => now()->addSeconds(3),
]);

$returnedUser = $guard->__invoke($request);

$this->assertEquals($user->id, $returnedUser->id);
$this->assertEquals($token->id, $returnedUser->currentAccessToken()->id);
$this->assertInstanceOf(DateTimeInterface::class, $returnedUser->currentAccessToken()->last_used_at);
}

public function test_authentication_is_successful_with_token_if_no_session_present()
{
$factory = Mockery::mock(AuthFactory::class);
Expand Down