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

added experiration time after last valid request #349

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
15 changes: 15 additions & 0 deletions config/sanctum.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@

'expiration' => null,

/*
|--------------------------------------------------------------------------
| Expiration Minutes after last valid request
|--------------------------------------------------------------------------
|
| This value controls the number of minutes between validated requests until an issued token will be
| considered expired. If this value is null, personal access tokens do
| not expire. This won't tweak the lifetime of first-party sessions.
|
| It can works only if 'expiration' value is null
|
*/

'intermediate_expiration' => null,

/*
|--------------------------------------------------------------------------
| Sanctum Middleware
Expand Down
15 changes: 12 additions & 3 deletions src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class Guard
*/
protected $expiration;

/**
* The number of minutes tokens should be allowed to remain valid between requests.
*
* @var int
*/
protected $intermediateExpiration;

/**
* The provider name.
*
Expand All @@ -38,11 +45,12 @@ class Guard
* @param string $provider
* @return void
*/
public function __construct(AuthFactory $auth, $expiration = null, $provider = null)
public function __construct(AuthFactory $auth, $expiration = null, $provider = null, $intermediateExpiration = null)
{
$this->auth = $auth;
$this->expiration = $expiration;
$this->provider = $provider;
$this->intermediateExpiration = $intermediateExpiration;
}

/**
Expand Down Expand Up @@ -117,8 +125,9 @@ protected function isValidAccessToken($accessToken): bool
return false;
}

$isValid =
(! $this->expiration || $accessToken->created_at->gt(now()->subMinutes($this->expiration)))
$isValid = ($this->expiration
? (! $this->expiration || $accessToken->created_at->gt(now()->subMinutes($this->expiration)))
: (! $this->intermediateExpiration || ($accessToken->last_used_at ?: $accessToken->created_at)->gt(now()->subMinutes($this->intermediateExpiration))))
&& $this->hasValidProvider($accessToken->tokenable);

if (is_callable(Sanctum::$accessTokenAuthenticationCallback)) {
Expand Down
2 changes: 1 addition & 1 deletion src/SanctumServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected function configureGuard()
protected function createGuard($auth, $config)
{
return new RequestGuard(
new Guard($auth, config('sanctum.expiration'), $config['provider']),
new Guard($auth, config('sanctum.expiration'), $config['provider'], config('sanctum.intermediate_expiration')),
request(),
$auth->createUserProvider($config['provider'] ?? null)
);
Expand Down