Laravel Sanctum

Introduction

Laravel Sanctum provides a featherweight authentication system for SPAs (single page applications), mobile applications, and simple, token based APIs. Sanctum allows each user of your application to generate multiple API tokens for their account. These tokens may be granted abilities / scopes which specify which actions the tokens are allowed to perform.

How It Works

Laravel Sanctum exists to solve two separate problems.

API Tokens

First, it is a simple package to issue API tokens to your users without the complication of OAuth. This feature is inspired by GitHub “access tokens”. For example, imagine the “account settings” of your application has a screen where a user may generate an API token for their account. You may use Sanctum to generate and manage those tokens. 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 requests via the Authorization header which should contain a valid API token.

SPA Authentication

Second, Sanctum exists to offer a simple way to authenticate single page applications (SPAs) that need to communicate with a Laravel powered API. These SPAs might exist in the same repository as your Laravel application or might be an entirely separate repository, such as a SPA created using Vue CLI.

For this feature, Sanctum does not use tokens of any kind. Instead, Sanctum uses Laravel’s built-in cookie based session authentication services. This provides the benefits of CSRF protection, session authentication, as well as protects against leakage of the authentication credentials via XSS. Sanctum will only attempt to authenticate using cookies when the incoming request originates from your own SPA frontend.

{tip} It is perfectly fine to use Sanctum only for API token authentication or only for SPA authentication. Just because you use Sanctum does not mean you are required to use both features it offers.

Installation

You may install Laravel Sanctum via Composer:

  1. composer require laravel/sanctum

Next, you should publish the Sanctum configuration and migration files using the vendor:publish Artisan command. The sanctum configuration file will be placed in your config directory:

  1. php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"

Finally, you should run your database migrations. Sanctum will create one database table in which to store API tokens:

  1. php artisan migrate

Next, if you plan to utilize Sanctum to authenticate an SPA, you should add Sanctum’s middleware to your api middleware group within your app/Http/Kernel.php file:

  1. use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;
  2. 'api' => [
  3. EnsureFrontendRequestsAreStateful::class,
  4. 'throttle:60,1',
  5. \Illuminate\Routing\Middleware\SubstituteBindings::class,
  6. ],

Migration Customization

If you are not going to use Sanctum’s default migrations, you should call the Sanctum::ignoreMigrations method in the register method of your AppServiceProvider. You may export the default migrations using php artisan vendor:publish --tag=sanctum-migrations.

API Token Authentication

{tip} You should not use API tokens to authenticate your own first-party SPA. Instead, use Sanctum’s built-in SPA authentication.

Issuing API Tokens

Sanctum allows you to issue API tokens / personal access tokens that may be used to authenticate API requests. When making requests using API tokens, the token should be included in the Authorization header as a Bearer token.

To begin issuing tokens for users, your User model should use the HasApiTokens trait:

  1. use Laravel\Sanctum\HasApiTokens;
  2. class User extends Authenticatable
  3. {
  4. use HasApiTokens, Notifiable;
  5. }

To issue a token, you may use the createToken method. The createToken method returns a Laravel\Sanctum\NewAccessToken instance. API tokens are hashed using SHA-256 hashing before being stored in your database, but you may access the plain-text value of the token using the plainTextToken property of the NewAccessToken instance. You should display this value to the user immediately after the token has been created:

  1. $token = $user->createToken('token-name');
  2. return $token->plainTextToken;

You may access all of the user’s tokens using the tokens Eloquent relationship provided by the HasApiTokens trait:

  1. foreach ($user->tokens as $token) {
  2. //
  3. }

Token Abilities

Sanctum allows you to assign “abilities” to tokens, similar to OAuth “scopes”. You may pass an array of string abilities as the second argument to the createToken method:

  1. return $user->createToken('token-name', ['server:update'])->plainTextToken;

When handling an incoming request authenticated by Sanctum, you may determine if the token has a given ability using the tokenCan method:

  1. if ($user->tokenCan('server:update')) {
  2. //
  3. }

{tip} For convenience, the tokenCan method will always return true if the incoming authenticated request was from your first-party SPA and you are using Sanctum’s built-in SPA authentication.

Protecting Routes

To protect routes so that all incoming requests must be authenticated, you should attach the sanctum authentication guard to your API routes within your routes/api.php file. This guard will ensure that incoming requests are authenticated as either a stateful authenticated requests from your SPA or contain a valid API token header if the request is from a third party:

  1. Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
  2. return $request->user();
  3. });

Revoking Tokens

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

  1. // Revoke all tokens...
  2. $user->tokens()->delete();
  3. // Revoke the user's current token...
  4. $request->user()->currentAccessToken()->delete();
  5. // Revoke a specific token...
  6. $user->tokens()->where('id', $id)->delete();

SPA Authentication

Sanctum exists to offer a simple way to authenticate single page applications (SPAs) that need to communicate with a Laravel powered API. These SPAs might exist in the same repository as your Laravel application or might be an entirely separate repository, such as a SPA created using Vue CLI.

For this feature, Sanctum does not use tokens of any kind. Instead, Sanctum uses Laravel’s built-in cookie based session authentication services. This provides the benefits of CSRF protection, session authentication, as well as protects against leakage of the authentication credentials via XSS. Sanctum will only attempt to authenticate using cookies when the incoming request originates from your own SPA frontend.

{note} In order to authenticate, your SPA and API must share the same top-level domain. However, they may be placed on different subdomains.

Configuration

Configuring Your First-Party Domains

First, you should configure which domains your SPA will be making requests from. You may configure these domains using the stateful configuration option in your sanctum configuration file. This configuration setting determines which domains will maintain “stateful” authentication using Laravel session cookies when making requests to your API.

{note} If you are accessing your application via a URL that includes the port (127.0.0.1:8000), you should ensure that you include the port number with the domain.

Sanctum Middleware

Next, you should add Sanctum’s middleware to your api middleware group within your app/Http/Kernel.php file. This middleware is responsible for ensuring that incoming requests from your SPA can authenticate using Laravel’s session cookies, while still allowing requests from third parties or mobile applications to authenticate using API tokens:

  1. use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;
  2. 'api' => [
  3. EnsureFrontendRequestsAreStateful::class,
  4. 'throttle:60,1',
  5. \Illuminate\Routing\Middleware\SubstituteBindings::class,
  6. ],

CORS & Cookies

If you are having trouble authenticating with your application from an SPA that executes on a separate subdomain, you have likely misconfigured your CORS (Cross-Origin Resource Sharing) or session cookie settings.

You should ensure that your application’s CORS configuration is returning the Access-Control-Allow-Credentials header with a value of True by setting the supports_credentials option within your application’s cors configuration file to true.

In addition, you should enable the withCredentials option on your global axios instance. Typically, this should be performed in your resources/js/bootstrap.js file:

  1. axios.defaults.withCredentials = true;

Finally, you should ensure your application’s session cookie domain configuration supports any subdomain of your root domain. You may do this by prefixing the domain with a leading . within your session configuration file:

  1. 'domain' => '.domain.com',

Authenticating

To authenticate your SPA, your SPA’s login page should first make a request to the /sanctum/csrf-cookie route to initialize CSRF protection for the application:

  1. axios.get('/sanctum/csrf-cookie').then(response => {
  2. // Login...
  3. });

During this request Laravel will set an XSRF-TOKEN cookie containing the current CSRF token. This token should then be passed in an X-XSRF-TOKEN header on subsequent requests, which libraries like Axios and the Angular HttpClient will do automatically for you.

Once CSRF protection has been initialized, you should make a POST request to the typical Laravel /login route. This /login route may be provided by the laravel/jetstream authentication scaffolding package.

If the login request is successful, you will be authenticated and subsequent requests to your API routes will automatically be authenticated via the session cookie that the Laravel backend issued to your client.

{tip} You are free to write your own /login endpoint; however, you should ensure that it authenticates the user using the standard, session based authentication services that Laravel provides.

Protecting Routes

To protect routes so that all incoming requests must be authenticated, you should attach the sanctum authentication guard to your API routes within your routes/api.php file. This guard will ensure that incoming requests are authenticated as either a stateful authenticated requests from your SPA or contain a valid API token header if the request is from a third party:

  1. Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
  2. return $request->user();
  3. });

Authorizing Private Broadcast Channels

If your SPA needs to authenticate with private / presence broadcast channels, you should place the Broadcast::routes method call within your routes/api.php file:

  1. Broadcast::routes(['middleware' => ['auth:sanctum']]);

Next, in order for Pusher’s authorization requests to succeed, you will need to provide a custom Pusher authorizer when initializing Laravel Echo. This allows your application to configure Pusher to use the axios instance that is properly configured for cross-domain requests:

  1. window.Echo = new Echo({
  2. broadcaster: "pusher",
  3. cluster: process.env.MIX_PUSHER_APP_CLUSTER,
  4. encrypted: true,
  5. key: process.env.MIX_PUSHER_APP_KEY,
  6. authorizer: (channel, options) => {
  7. return {
  8. authorize: (socketId, callback) => {
  9. axios.post('/api/broadcasting/auth', {
  10. socket_id: socketId,
  11. channel_name: channel.name
  12. })
  13. .then(response => {
  14. callback(false, response.data);
  15. })
  16. .catch(error => {
  17. callback(true, error);
  18. });
  19. }
  20. };
  21. },
  22. })

Mobile Application Authentication

You may use Sanctum tokens to authenticate your mobile application’s requests to your API. The process for authenticating mobile application requests is similar to authenticating third-party API requests; however, there are small differences in how you will issue the API tokens.

Issuing API Tokens

To get started, create a route that accepts the user’s email / username, password, and device name, then exchanges those credentials for a new Sanctum token. The endpoint will return the plain-text Sanctum token which may then be stored on the mobile device and used to make additional API requests:

  1. use App\Models\User;
  2. use Illuminate\Http\Request;
  3. use Illuminate\Support\Facades\Hash;
  4. use Illuminate\Validation\ValidationException;
  5. Route::post('/sanctum/token', function (Request $request) {
  6. $request->validate([
  7. 'email' => 'required|email',
  8. 'password' => 'required',
  9. 'device_name' => 'required',
  10. ]);
  11. $user = User::where('email', $request->email)->first();
  12. if (! $user || ! Hash::check($request->password, $user->password)) {
  13. throw ValidationException::withMessages([
  14. 'email' => ['The provided credentials are incorrect.'],
  15. ]);
  16. }
  17. return $user->createToken($request->device_name)->plainTextToken;
  18. });

When the mobile device uses the token to make an API request to your application, it should pass the token in the Authorization header as a Bearer token.

{tip} When issuing tokens for a mobile application, you are also free to specify token abilities

Protecting Routes

As previously documented, you may protect routes so that all incoming requests must be authenticated by attaching the sanctum authentication guard to the routes. Typically, you will attach this guard to the routes defined within your routes/api.php file:

  1. Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
  2. return $request->user();
  3. });

Revoking Tokens

To allow users to revoke API tokens issued to mobile devices, you may list them by name, along with a “Revoke” button, within an “account settings” portion of your web application’s UI. When the user clicks the “Revoke” button, you can delete the token from the database. Remember, you can access a user’s API tokens via the tokens relationship provided by the HasApiTokens trait:

  1. // Revoke all tokens...
  2. $user->tokens()->delete();
  3. // Revoke a specific token...
  4. $user->tokens()->where('id', $id)->delete();

Testing

While testing, the Sanctum::actingAs method may be used to authenticate a user and specify which abilities are granted to their token:

  1. use App\Models\User;
  2. use Laravel\Sanctum\Sanctum;
  3. public function test_task_list_can_be_retrieved()
  4. {
  5. Sanctum::actingAs(
  6. User::factory()->create(),
  7. ['view-tasks']
  8. );
  9. $response = $this->get('/api/task');
  10. $response->assertOk();
  11. }

If you would like to grant all abilities to the token, you should include * in the ability list provided to the actingAs method:

  1. Sanctum::actingAs(
  2. User::factory()->create(),
  3. ['*']
  4. );