Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Laravel sanctum expire tokens?

When I run $user->currentAccessToken()->delete(); the token expires, Auth::check() becomes false, what it is expected.

However, when I go to the personal_access_tokens table, the token is still there. There is no soft delete field. How does Sanctum now that the token is expired?

like image 403
Inigo EC Avatar asked Sep 29 '20 09:09

Inigo EC


People also ask

Do sanctum tokens expire?

These tokens typically have a very long expiration time (years), but may be manually revoked by the user at anytime. Laravel Sanctum offers this feature by storing user API tokens in a single database table and authenticating incoming HTTP requests via the Authorization header which should contain a valid API token.

How do you refresh a sanctum token?

The refresh() method also must be invoked inside the “auth:sanctum” middleware and used to clear all the user tokens by calling $user->tokens() relationship and calling $user->tokens()->delete() and return a new token.

How does laravel sanctum work?

How does Laravel Sanctum Work? Laravel Sanctum is a simple package that is used to issue API tokens to users without the complication of OAuth. These tokens have a long expiration time but can be revoked at any time manually.

Does laravel sanctum use JWT?

Laravel JWT authentication vs. Sanctum offers both session-based and token-based authentication and is good for single-page application (SPA) authentications. Passport uses JWT authentication as standard but also implements full OAuth 2.0 authorization.


3 Answers

You can set in config/sanctum.php array node expiration

/*
|--------------------------------------------------------------------------
| Expiration Minutes
|--------------------------------------------------------------------------
|
| This value controls the number of minutes 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.
|
*/

'expiration' => 60 * 24 * 7,
like image 135
Chatchai Siwilai Avatar answered Oct 09 '22 22:10

Chatchai Siwilai


As of writing this answer, the token now deletes from the database so that one is settled.

How Sanctum knows if a token is expired is pretty simple:

  • The token has a created date, call it C
  • The config data in config/sanctum.php has an expiration time, call it E
  • The current time you want to use the token is right now, call it N

To check for expiry, it subtracts N from C. If N - C is less than E, the token hasn't expired yet. If it is greater, the token is expired.

Example:

  • You created a token at 5:00 AM
  • The expiration time is set for 5 hours
  • You want to access data through the token at 8:00 AM

When you subtract 8 from 5, you get 3. That's just 3 hrs since you created the token. Not up to the 5 hrs you set.

If you were accessing data at say, 11:00 AM, then the time frame becomes 6 hrs, which is more than 5 hrs, meaning the token has expired.

like image 37
Joshua Etim Avatar answered Oct 09 '22 22:10

Joshua Etim


I looked in the source code of sanctumm and it seems like it's a guard that handles it.

      if (! $accessToken ||
                ($this->expiration &&
                 $accessToken->created_at->lte(now()->subMinutes($this->expiration))) ||
                ! $this->hasValidProvider($accessToken->tokenable)) {
                return;
            }

This means that the validating token proccess looks like this:

  • Check if token is present in database
  • Check if token creation_date hasnt surpassed the expiration time
  • Check if the tokenable model matches the provider's model type
  • Check if the tokenable model supports API tokens

And upon fail, it's simply rejecting the request. Not deleting the token.

Deleting the token is however the manual way to revoke a token.

You may "revoke" tokens by deleting them from your database using the tokens relationship that is provided by the HasApiTokens trait:

like image 37
PatricNox Avatar answered Oct 09 '22 22:10

PatricNox