API Authentication (Passport)

Introduction

Laravel already makes it easy to perform authentication via traditional login forms, but what about APIs? APIs typically use tokens to authenticate users and do not maintain session state between requests. Laravel makes API authentication a breeze using Laravel Passport, which provides a full OAuth2 server implementation for your Laravel application in a matter of minutes. Passport is built on top of the League OAuth2 server that is maintained by Alex Bilbie.

{note} This documentation assumes you are already familiar with OAuth2. If you do not know anything about OAuth2, consider familiarizing yourself with the general terminology and features of OAuth2 before continuing.

Installation

To get started, install Passport via the Composer package manager:

  1. composer require laravel/passport=~4.0

The Passport service provider registers its own database migration directory with the framework, so you should migrate your database after registering the provider. The Passport migrations will create the tables your application needs to store clients and access tokens:

  1. php artisan migrate

{note} If you are not going to use Passport's default migrations, you should call the Passport::ignoreMigrations method in the register method of your AppServiceProvider. You may export the default migrations using php artisan vendor:publish —tag=passport-migrations.

Next, you should run the passport:install command. This command will create the encryption keys needed to generate secure access tokens. In addition, the command will create "personal access" and "password grant" clients which will be used to generate access tokens:

  1. php artisan passport:install

After running this command, add the Laravel\Passport\HasApiTokens trait to your App\User model. This trait will provide a few helper methods to your model which allow you to inspect the authenticated user's token and scopes:

  1. <?php
  2. namespace App;
  3. use Laravel\Passport\HasApiTokens;
  4. use Illuminate\Notifications\Notifiable;
  5. use Illuminate\Foundation\Auth\User as Authenticatable;
  6. class User extends Authenticatable
  7. {
  8. use HasApiTokens, Notifiable;
  9. }

Next, you should call the Passport::routes method within the boot method of your AuthServiceProvider. This method will register the routes necessary to issue access tokens and revoke access tokens, clients, and personal access tokens:

  1. <?php
  2. namespace App\Providers;
  3. use Laravel\Passport\Passport;
  4. use Illuminate\Support\Facades\Gate;
  5. use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
  6. class AuthServiceProvider extends ServiceProvider
  7. {
  8. /**
  9. * The policy mappings for the application.
  10. *
  11. * @var array
  12. */
  13. protected $policies = [
  14. 'App\Model' => 'App\Policies\ModelPolicy',
  15. ];
  16. /**
  17. * Register any authentication / authorization services.
  18. *
  19. * @return void
  20. */
  21. public function boot()
  22. {
  23. $this->registerPolicies();
  24. Passport::routes();
  25. }
  26. }

Finally, in your config/auth.php configuration file, you should set the driver option of the api authentication guard to passport. This will instruct your application to use Passport's TokenGuard when authenticating incoming API requests:

  1. 'guards' => [
  2. 'web' => [
  3. 'driver' => 'session',
  4. 'provider' => 'users',
  5. ],
  6. 'api' => [
  7. 'driver' => 'passport',
  8. 'provider' => 'users',
  9. ],
  10. ],

Frontend Quickstart

{note} In order to use the Passport Vue components, you must be using the Vue JavaScript framework. These components also use the Bootstrap CSS framework. However, even if you are not using these tools, the components serve as a valuable reference for your own frontend implementation.

Passport ships with a JSON API that you may use to allow your users to create clients and personal access tokens. However, it can be time consuming to code a frontend to interact with these APIs. So, Passport also includes pre-built Vue components you may use as an example implementation or starting point for your own implementation.

To publish the Passport Vue components, use the vendor:publish Artisan command:

  1. php artisan vendor:publish --tag=passport-components

The published components will be placed in your resources/assets/js/components directory. Once the components have been published, you should register them in your resources/assets/js/app.js file:

  1. Vue.component(
  2. 'passport-clients',
  3. require('./components/passport/Clients.vue')
  4. );
  5. Vue.component(
  6. 'passport-authorized-clients',
  7. require('./components/passport/AuthorizedClients.vue')
  8. );
  9. Vue.component(
  10. 'passport-personal-access-tokens',
  11. require('./components/passport/PersonalAccessTokens.vue')
  12. );

After registering the components, make sure to run npm run dev to recompile your assets. Once you have recompiled your assets, you may drop the components into one of your application's templates to get started creating clients and personal access tokens:

  1. <passport-clients></passport-clients>
  2. <passport-authorized-clients></passport-authorized-clients>
  3. <passport-personal-access-tokens></passport-personal-access-tokens>

Deploying Passport

When deploying Passport to your production servers for the first time, you will likely need to run the passport:keys command. This command generates the encryption keys Passport needs in order to generate access token. The generated keys are not typically kept in source control:

  1. php artisan passport:keys

Configuration

Token Lifetimes

By default, Passport issues long-lived access tokens that never need to be refreshed. If you would like to configure a shorter token lifetime, you may use the tokensExpireIn and refreshTokensExpireIn methods. These methods should be called from the boot method of your AuthServiceProvider:

  1. /**
  2. * Register any authentication / authorization services.
  3. *
  4. * @return void
  5. */
  6. public function boot()
  7. {
  8. $this->registerPolicies();
  9. Passport::routes();
  10. Passport::tokensExpireIn(now()->addDays(15));
  11. Passport::refreshTokensExpireIn(now()->addDays(30));
  12. }

Issuing Access Tokens

Using OAuth2 with authorization codes is how most developers are familiar with OAuth2. When using authorization codes, a client application will redirect a user to your server where they will either approve or deny the request to issue an access token to the client.

Managing Clients

First, developers building applications that need to interact with your application's API will need to register their application with yours by creating a "client". Typically, this consists of providing the name of their application and a URL that your application can redirect to after users approve their request for authorization.

The passport:client Command

The simplest way to create a client is using the passport:client Artisan command. This command may be used to create your own clients for testing your OAuth2 functionality. When you run the client command, Passport will prompt you for more information about your client and will provide you with a client ID and secret:

  1. php artisan passport:client

JSON API

Since your users will not be able to utilize the client command, Passport provides a JSON API that you may use to create clients. This saves you the trouble of having to manually code controllers for creating, updating, and deleting clients.

However, you will need to pair Passport's JSON API with your own frontend to provide a dashboard for your users to manage their clients. Below, we'll review all of the API endpoints for managing clients. For convenience, we'll use Axios to demonstrate making HTTP requests to the endpoints.

{tip} If you don't want to implement the entire client management frontend yourself, you can use the frontend quickstart to have a fully functional frontend in a matter of minutes.

GET /oauth/clients

This route returns all of the clients for the authenticated user. This is primarily useful for listing all of the user's clients so that they may edit or delete them:

  1. axios.get('/oauth/clients')
  2. .then(response => {
  3. console.log(response.data);
  4. });

POST /oauth/clients

This route is used to create new clients. It requires two pieces of data: the client's name and a redirect URL. The redirect URL is where the user will be redirected after approving or denying a request for authorization.

When a client is created, it will be issued a client ID and client secret. These values will be used when requesting access tokens from your application. The client creation route will return the new client instance:

  1. const data = {
  2. name: 'Client Name',
  3. redirect: 'http://example.com/callback'
  4. };
  5. axios.post('/oauth/clients', data)
  6. .then(response => {
  7. console.log(response.data);
  8. })
  9. .catch (response => {
  10. // List errors on response...
  11. });

PUT /oauth/clients/{client-id}

This route is used to update clients. It requires two pieces of data: the client's name and a redirect URL. The redirect URL is where the user will be redirected after approving or denying a request for authorization. The route will return the updated client instance:

  1. const data = {
  2. name: 'New Client Name',
  3. redirect: 'http://example.com/callback'
  4. };
  5. axios.put('/oauth/clients/' + clientId, data)
  6. .then(response => {
  7. console.log(response.data);
  8. })
  9. .catch (response => {
  10. // List errors on response...
  11. });

DELETE /oauth/clients/{client-id}

This route is used to delete clients:

  1. axios.delete('/oauth/clients/' + clientId)
  2. .then(response => {
  3. //
  4. });

Requesting Tokens

Redirecting For Authorization

Once a client has been created, developers may use their client ID and secret to request an authorization code and access token from your application. First, the consuming application should make a redirect request to your application's /oauth/authorize route like so:

  1. Route::get('/redirect', function () {
  2. $query = http_build_query([
  3. 'client_id' => 'client-id',
  4. 'redirect_uri' => 'http://example.com/callback',
  5. 'response_type' => 'code',
  6. 'scope' => '',
  7. ]);
  8. return redirect('http://your-app.com/oauth/authorize?'.$query);
  9. });

{tip} Remember, the /oauth/authorize route is already defined by the Passport::routes method. You do not need to manually define this route.

Approving The Request

When receiving authorization requests, Passport will automatically display a template to the user allowing them to approve or deny the authorization request. If they approve the request, they will be redirected back to the redirect_uri that was specified by the consuming application. The redirect_uri must match the redirect URL that was specified when the client was created.

If you would like to customize the authorization approval screen, you may publish Passport's views using the vendor:publish Artisan command. The published views will be placed in resources/views/vendor/passport:

  1. php artisan vendor:publish --tag=passport-views

Converting Authorization Codes To Access Tokens

If the user approves the authorization request, they will be redirected back to the consuming application. The consumer should then issue a POST request to your application to request an access token. The request should include the authorization code that was issued by your application when the user approved the authorization request. In this example, we'll use the Guzzle HTTP library to make the POST request:

  1. Route::get('/callback', function (Request $request) {
  2. $http = new GuzzleHttp\Client;
  3. $response = $http->post('http://your-app.com/oauth/token', [
  4. 'form_params' => [
  5. 'grant_type' => 'authorization_code',
  6. 'client_id' => 'client-id',
  7. 'client_secret' => 'client-secret',
  8. 'redirect_uri' => 'http://example.com/callback',
  9. 'code' => $request->code,
  10. ],
  11. ]);
  12. return json_decode((string) $response->getBody(), true);
  13. });

This /oauth/token route will return a JSON response containing access_token, refresh_token, and expires_in attributes. The expires_in attribute contains the number of seconds until the access token expires.

{tip} Like the /oauth/authorize route, the /oauth/token route is defined for you by the Passport::routes method. There is no need to manually define this route.

Refreshing Tokens

If your application issues short-lived access tokens, users will need to refresh their access tokens via the refresh token that was provided to them when the access token was issued. In this example, we'll use the Guzzle HTTP library to refresh the token:

  1. $http = new GuzzleHttp\Client;
  2. $response = $http->post('http://your-app.com/oauth/token', [
  3. 'form_params' => [
  4. 'grant_type' => 'refresh_token',
  5. 'refresh_token' => 'the-refresh-token',
  6. 'client_id' => 'client-id',
  7. 'client_secret' => 'client-secret',
  8. 'scope' => '',
  9. ],
  10. ]);
  11. return json_decode((string) $response->getBody(), true);

This /oauth/token route will return a JSON response containing access_token, refresh_token, and expires_in attributes. The expires_in attribute contains the number of seconds until the access token expires.

Password Grant Tokens

The OAuth2 password grant allows your other first-party clients, such as a mobile application, to obtain an access token using an e-mail address / username and password. This allows you to issue access tokens securely to your first-party clients without requiring your users to go through the entire OAuth2 authorization code redirect flow.

Creating A Password Grant Client

Before your application can issue tokens via the password grant, you will need to create a password grant client. You may do this using the passport:client command with the —password option. If you have already run the passport:install command, you do not need to run this command:

  1. php artisan passport:client --password

Requesting Tokens

Once you have created a password grant client, you may request an access token by issuing a POST request to the /oauth/token route with the user's email address and password. Remember, this route is already registered by the Passport::routes method so there is no need to define it manually. If the request is successful, you will receive an access_token and refresh_token in the JSON response from the server:

  1. $http = new GuzzleHttp\Client;
  2. $response = $http->post('http://your-app.com/oauth/token', [
  3. 'form_params' => [
  4. 'grant_type' => 'password',
  5. 'client_id' => 'client-id',
  6. 'client_secret' => 'client-secret',
  7. 'username' => '[email protected]',
  8. 'password' => 'my-password',
  9. 'scope' => '',
  10. ],
  11. ]);
  12. return json_decode((string) $response->getBody(), true);

{tip} Remember, access tokens are long-lived by default. However, you are free to configure your maximum access token lifetime if needed.

Requesting All Scopes

When using the password grant, you may wish to authorize the token for all of the scopes supported by your application. You can do this by requesting the scope. If you request the scope, the can method on the token instance will always return true. This scope may only be assigned to a token that is issued using the password grant:

  1. $response = $http->post('http://your-app.com/oauth/token', [
  2. 'form_params' => [
  3. 'grant_type' => 'password',
  4. 'client_id' => 'client-id',
  5. 'client_secret' => 'client-secret',
  6. 'username' => '[email protected]',
  7. 'password' => 'my-password',
  8. 'scope' => '*',
  9. ],
  10. ]);

Implicit Grant Tokens

The implicit grant is similar to the authorization code grant; however, the token is returned to the client without exchanging an authorization code. This grant is most commonly used for JavaScript or mobile applications where the client credentials can't be securely stored. To enable the grant, call the enableImplicitGrant method in your AuthServiceProvider:

  1. /**
  2. * Register any authentication / authorization services.
  3. *
  4. * @return void
  5. */
  6. public function boot()
  7. {
  8. $this->registerPolicies();
  9. Passport::routes();
  10. Passport::enableImplicitGrant();
  11. }

Once a grant has been enabled, developers may use their client ID to request an access token from your application. The consuming application should make a redirect request to your application's /oauth/authorize route like so:

  1. Route::get('/redirect', function () {
  2. $query = http_build_query([
  3. 'client_id' => 'client-id',
  4. 'redirect_uri' => 'http://example.com/callback',
  5. 'response_type' => 'token',
  6. 'scope' => '',
  7. ]);
  8. return redirect('http://your-app.com/oauth/authorize?'.$query);
  9. });

{tip} Remember, the /oauth/authorize route is already defined by the Passport::routes method. You do not need to manually define this route.

Client Credentials Grant Tokens

The client credentials grant is suitable for machine-to-machine authentication. For example, you might use this grant in a scheduled job which is performing maintenance tasks over an API. To use this method you first need to add new middleware to your $routeMiddleware in app/Http/Kernel.php:

  1. use Laravel\Passport\Http\Middleware\CheckClientCredentials;
  2. protected $routeMiddleware = [
  3. 'client' => CheckClientCredentials::class,
  4. ];

Then attach this middleware to a route:

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

To retrieve a token, make a request to the oauth/token endpoint:

  1. $guzzle = new GuzzleHttp\Client;
  2. $response = $guzzle->post('http://your-app.com/oauth/token', [
  3. 'form_params' => [
  4. 'grant_type' => 'client_credentials',
  5. 'client_id' => 'client-id',
  6. 'client_secret' => 'client-secret',
  7. 'scope' => 'your-scope',
  8. ],
  9. ]);
  10. return json_decode((string) $response->getBody(), true)['access_token'];

Personal Access Tokens

Sometimes, your users may want to issue access tokens to themselves without going through the typical authorization code redirect flow. Allowing users to issue tokens to themselves via your application's UI can be useful for allowing users to experiment with your API or may serve as a simpler approach to issuing access tokens in general.

{note} Personal access tokens are always long-lived. Their lifetime is not modified when using the tokensExpireIn or refreshTokensExpireIn methods.

Creating A Personal Access Client

Before your application can issue personal access tokens, you will need to create a personal access client. You may do this using the passport:client command with the —personal option. If you have already run the passport:install command, you do not need to run this command:

  1. php artisan passport:client --personal

Managing Personal Access Tokens

Once you have created a personal access client, you may issue tokens for a given user using the createToken method on the User model instance. The createToken method accepts the name of the token as its first argument and an optional array of scopes as its second argument:

  1. $user = App\User::find(1);
  2. // Creating a token without scopes...
  3. $token = $user->createToken('Token Name')->accessToken;
  4. // Creating a token with scopes...
  5. $token = $user->createToken('My Token', ['place-orders'])->accessToken;

JSON API

Passport also includes a JSON API for managing personal access tokens. You may pair this with your own frontend to offer your users a dashboard for managing personal access tokens. Below, we'll review all of the API endpoints for managing personal access tokens. For convenience, we'll use Axios to demonstrate making HTTP requests to the endpoints.

{tip} If you don't want to implement the personal access token frontend yourself, you can use the frontend quickstart to have a fully functional frontend in a matter of minutes.

GET /oauth/scopes

This route returns all of the scopes defined for your application. You may use this route to list the scopes a user may assign to a personal access token:

  1. axios.get('/oauth/scopes')
  2. .then(response => {
  3. console.log(response.data);
  4. });

GET /oauth/personal-access-tokens

This route returns all of the personal access tokens that the authenticated user has created. This is primarily useful for listing all of the user's tokens so that they may edit or delete them:

  1. axios.get('/oauth/personal-access-tokens')
  2. .then(response => {
  3. console.log(response.data);
  4. });

POST /oauth/personal-access-tokens

This route creates new personal access tokens. It requires two pieces of data: the token's name and the scopes that should be assigned to the token:

  1. const data = {
  2. name: 'Token Name',
  3. scopes: []
  4. };
  5. axios.post('/oauth/personal-access-tokens', data)
  6. .then(response => {
  7. console.log(response.data.accessToken);
  8. })
  9. .catch (response => {
  10. // List errors on response...
  11. });

DELETE /oauth/personal-access-tokens/{token-id}

This route may be used to delete personal access tokens:

  1. axios.delete('/oauth/personal-access-tokens/' + tokenId);

Protecting Routes

Via Middleware

Passport includes an authentication guard that will validate access tokens on incoming requests. Once you have configured the api guard to use the passport driver, you only need to specify the auth:api middleware on any routes that require a valid access token:

  1. Route::get('/user', function () {
  2. //
  3. })->middleware('auth:api');

Passing The Access Token

When calling routes that are protected by Passport, your application's API consumers should specify their access token as a Bearer token in the Authorization header of their request. For example, when using the Guzzle HTTP library:

  1. $response = $client->request('GET', '/api/user', [
  2. 'headers' => [
  3. 'Accept' => 'application/json',
  4. 'Authorization' => 'Bearer '.$accessToken,
  5. ],
  6. ]);

Token Scopes

Defining Scopes

Scopes allow your API clients to request a specific set of permissions when requesting authorization to access an account. For example, if you are building an e-commerce application, not all API consumers will need the ability to place orders. Instead, you may allow the consumers to only request authorization to access order shipment statuses. In other words, scopes allow your application's users to limit the actions a third-party application can perform on their behalf.

You may define your API's scopes using the Passport::tokensCan method in the boot method of your AuthServiceProvider. The tokensCan method accepts an array of scope names and scope descriptions. The scope description may be anything you wish and will be displayed to users on the authorization approval screen:

  1. use Laravel\Passport\Passport;
  2. Passport::tokensCan([
  3. 'place-orders' => 'Place orders',
  4. 'check-status' => 'Check order status',
  5. ]);

Assigning Scopes To Tokens

When Requesting Authorization Codes

When requesting an access token using the authorization code grant, consumers should specify their desired scopes as the scope query string parameter. The scope parameter should be a space-delimited list of scopes:

  1. Route::get('/redirect', function () {
  2. $query = http_build_query([
  3. 'client_id' => 'client-id',
  4. 'redirect_uri' => 'http://example.com/callback',
  5. 'response_type' => 'code',
  6. 'scope' => 'place-orders check-status',
  7. ]);
  8. return redirect('http://your-app.com/oauth/authorize?'.$query);
  9. });

When Issuing Personal Access Tokens

If you are issuing personal access tokens using the User model's createToken method, you may pass the array of desired scopes as the second argument to the method:

  1. $token = $user->createToken('My Token', ['place-orders'])->accessToken;

Checking Scopes

Passport includes two middleware that may be used to verify that an incoming request is authenticated with a token that has been granted a given scope. To get started, add the following middleware to the $routeMiddleware property of your app/Http/Kernel.php file:

  1. 'scopes' => \Laravel\Passport\Http\Middleware\CheckScopes::class,
  2. 'scope' => \Laravel\Passport\Http\Middleware\CheckForAnyScope::class,

Check For All Scopes

The scopes middleware may be assigned to a route to verify that the incoming request's access token has all of the listed scopes:

  1. Route::get('/orders', function () {
  2. // Access token has both "check-status" and "place-orders" scopes...
  3. })->middleware('scopes:check-status,place-orders');

Check For Any Scopes

The scope middleware may be assigned to a route to verify that the incoming request's access token has at least one of the listed scopes:

  1. Route::get('/orders', function () {
  2. // Access token has either "check-status" or "place-orders" scope...
  3. })->middleware('scope:check-status,place-orders');

Checking Scopes On A Token Instance

Once an access token authenticated request has entered your application, you may still check if the token has a given scope using the tokenCan method on the authenticated User instance:

  1. use Illuminate\Http\Request;
  2. Route::get('/orders', function (Request $request) {
  3. if ($request->user()->tokenCan('place-orders')) {
  4. //
  5. }
  6. });

Consuming Your API With JavaScript

When building an API, it can be extremely useful to be able to consume your own API from your JavaScript application. This approach to API development allows your own application to consume the same API that you are sharing with the world. The same API may be consumed by your web application, mobile applications, third-party applications, and any SDKs that you may publish on various package managers.

Typically, if you want to consume your API from your JavaScript application, you would need to manually send an access token to the application and pass it with each request to your application. However, Passport includes a middleware that can handle this for you. All you need to do is add the CreateFreshApiToken middleware to your web middleware group:

  1. 'web' => [
  2. // Other middleware...
  3. \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
  4. ],

This Passport middleware will attach a laravel_token cookie to your outgoing responses. This cookie contains an encrypted JWT that Passport will use to authenticate API requests from your JavaScript application. Now, you may make requests to your application's API without explicitly passing an access token:

  1. axios.get('/api/user')
  2. .then(response => {
  3. console.log(response.data);
  4. });

When using this method of authentication, the default Laravel JavaScript scaffolding instructs Axios to always send the X-CSRF-TOKEN and X-Requested-With headers. However, you should be sure to include your CSRF token in a HTML meta tag:

  1. window.axios.defaults.headers.common = {
  2. 'X-Requested-With': 'XMLHttpRequest',
  3. };

{note} If you are using a different JavaScript framework, you should make sure it is configured to send the X-CSRF-TOKEN and X-Requested-With headers with every outgoing request.

Events

Passport raises events when issuing access tokens and refresh tokens. You may use these events to prune or revoke other access tokens in your database. You may attach listeners to these events in your application's EventServiceProvider:

  1. /**
  2. * The event listener mappings for the application.
  3. *
  4. * @var array
  5. */
  6. protected $listen = [
  7. 'Laravel\Passport\Events\AccessTokenCreated' => [
  8. 'App\Listeners\RevokeOldTokens',
  9. ],
  10. 'Laravel\Passport\Events\RefreshTokenCreated' => [
  11. 'App\Listeners\PruneOldTokens',
  12. ],
  13. ];

Testing

Passport's actingAs method may be used to specify the currently authenticated user as well as its scopes. The first argument given to the actingAs method is the user instance and the second is an array of scopes that should be granted to the user's token:

  1. public function testServerCreation()
  2. {
  3. Passport::actingAs(
  4. factory(User::class)->create(),
  5. ['create-servers']
  6. );
  7. $response = $this->post('/api/create-server');
  8. $response->assertStatus(200);
  9. }