翻譯進度
0% 已翻譯
更新時間:
2024年6月30日 上午8:15:00 [世界標準時間]
翻譯人員:
  • cornch
幫我們翻譯此頁

Laravel Passport

Introduction

Laravel Passport 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 Andy Millington and Simon Hamp.

exclamation

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.

Passport Or Sanctum?

Before getting started, you may wish to determine if your application would be better served by Laravel Passport or Laravel Sanctum. If your application absolutely needs to support OAuth2, then you should use Laravel Passport.

However, if you are attempting to authenticate a single-page application, mobile application, or issue API tokens, you should use Laravel Sanctum. Laravel Sanctum does not support OAuth2; however, it provides a much simpler API authentication development experience.

Installation

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

1composer require laravel/passport
1composer require laravel/passport

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

1php artisan migrate
1php artisan migrate

Next, you should execute the passport:install Artisan 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:

1php artisan passport:install
1php artisan passport:install
lightbulb

If you would like to use UUIDs as the primary key value of the Passport Client model instead of auto-incrementing integers, please install Passport using the uuids option.

After running the passport:install command, add the Laravel\Passport\HasApiTokens trait to your App\Models\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. If your model is already using the Laravel\Sanctum\HasApiTokens trait, you may remove that trait:

1<?php
2 
3namespace App\Models;
4 
5use Illuminate\Database\Eloquent\Factories\HasFactory;
6use Illuminate\Foundation\Auth\User as Authenticatable;
7use Illuminate\Notifications\Notifiable;
8use Laravel\Passport\HasApiTokens;
9 
10class User extends Authenticatable
11{
12 use HasApiTokens, HasFactory, Notifiable;
13}
1<?php
2 
3namespace App\Models;
4 
5use Illuminate\Database\Eloquent\Factories\HasFactory;
6use Illuminate\Foundation\Auth\User as Authenticatable;
7use Illuminate\Notifications\Notifiable;
8use Laravel\Passport\HasApiTokens;
9 
10class User extends Authenticatable
11{
12 use HasApiTokens, HasFactory, Notifiable;
13}

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

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

Finally, in your application's 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 
7 'api' => [
8 'driver' => 'passport',
9 'provider' => 'users',
10 ],
11],
1'guards' => [
2 'web' => [
3 'driver' => 'session',
4 'provider' => 'users',
5 ],
6 
7 'api' => [
8 'driver' => 'passport',
9 'provider' => 'users',
10 ],
11],

Client UUIDs

You may also run the passport:install command with the --uuids option present. This option will instruct Passport that you would like to use UUIDs instead of auto-incrementing integers as the Passport Client model's primary key values. After running the passport:install command with the --uuids option, you will be given additional instructions regarding disabling Passport's default migrations:

1php artisan passport:install --uuids
1php artisan passport:install --uuids

Deploying Passport

When deploying Passport to your application's 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 tokens. The generated keys are not typically kept in source control:

1php artisan passport:keys
1php artisan passport:keys

If necessary, you may define the path where Passport's keys should be loaded from. You may use the Passport::loadKeysFrom method to accomplish this. Typically, this method should be called from the boot method of your application's App\Providers\AuthServiceProvider class:

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

Loading Keys From The Environment

Alternatively, you may publish Passport's configuration file using the vendor:publish Artisan command:

1php artisan vendor:publish --tag=passport-config
1php artisan vendor:publish --tag=passport-config

After the configuration file has been published, you may load your application's encryption keys by defining them as environment variables:

1PASSPORT_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
2<private key here>
3-----END RSA PRIVATE KEY-----"
4 
5PASSPORT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
6<public key here>
7-----END PUBLIC KEY-----"
1PASSPORT_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
2<private key here>
3-----END RSA PRIVATE KEY-----"
4 
5PASSPORT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
6<public key here>
7-----END PUBLIC KEY-----"

Migration Customization

If you are not going to use Passport's default migrations, you should call the Passport::ignoreMigrations method in the register method of your App\Providers\AppServiceProvider class. You may export the default migrations using the vendor:publish Artisan command:

1php artisan vendor:publish --tag=passport-migrations
1php artisan vendor:publish --tag=passport-migrations

Upgrading Passport

When upgrading to a new major version of Passport, it's important that you carefully review the upgrade guide.

Configuration

Client Secret Hashing

If you would like your client's secrets to be hashed when stored in your database, you should call the Passport::hashClientSecrets method in the boot method of your App\Providers\AuthServiceProvider class:

1use Laravel\Passport\Passport;
2 
3Passport::hashClientSecrets();
1use Laravel\Passport\Passport;
2 
3Passport::hashClientSecrets();

Once enabled, all of your client secrets will only be displayable to the user immediately after they are created. Since the plain-text client secret value is never stored in the database, it is not possible to recover the secret's value if it is lost.

Token Lifetimes

By default, Passport issues long-lived access tokens that expire after one year. If you would like to configure a longer / shorter token lifetime, you may use the tokensExpireIn, refreshTokensExpireIn, and personalAccessTokensExpireIn methods. These methods should be called from the boot method of your application's App\Providers\AuthServiceProvider class:

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

The expires_at columns on Passport's database tables are read-only and for display purposes only. When issuing tokens, Passport stores the expiration information within the signed and encrypted tokens. If you need to invalidate a token you should revoke it.

Overriding Default Models

You are free to extend the models used internally by Passport by defining your own model and extending the corresponding Passport model:

1use Laravel\Passport\Client as PassportClient;
2 
3class Client extends PassportClient
4{
5 // ...
6}
1use Laravel\Passport\Client as PassportClient;
2 
3class Client extends PassportClient
4{
5 // ...
6}

After defining your model, you may instruct Passport to use your custom model via the Laravel\Passport\Passport class. Typically, you should inform Passport about your custom models in the boot method of your application's App\Providers\AuthServiceProvider class:

1use App\Models\Passport\AuthCode;
2use App\Models\Passport\Client;
3use App\Models\Passport\PersonalAccessClient;
4use App\Models\Passport\Token;
5 
6/**
7 * Register any authentication / authorization services.
8 *
9 * @return void
10 */
11public function boot()
12{
13 $this->registerPolicies();
14 
15 Passport::routes();
16 
17 Passport::useTokenModel(Token::class);
18 Passport::useClientModel(Client::class);
19 Passport::useAuthCodeModel(AuthCode::class);
20 Passport::usePersonalAccessClientModel(PersonalAccessClient::class);
21}
1use App\Models\Passport\AuthCode;
2use App\Models\Passport\Client;
3use App\Models\Passport\PersonalAccessClient;
4use App\Models\Passport\Token;
5 
6/**
7 * Register any authentication / authorization services.
8 *
9 * @return void
10 */
11public function boot()
12{
13 $this->registerPolicies();
14 
15 Passport::routes();
16 
17 Passport::useTokenModel(Token::class);
18 Passport::useClientModel(Client::class);
19 Passport::useAuthCodeModel(AuthCode::class);
20 Passport::usePersonalAccessClientModel(PersonalAccessClient::class);
21}

Issuing Access Tokens

Using OAuth2 via 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:

1php artisan passport:client
1php artisan passport:client

Redirect URLs

If you would like to allow multiple redirect URLs for your client, you may specify them using a comma-delimited list when prompted for the URL by the passport:client command. Any URLs which contain commas should be URL encoded:

1http://example.com/callback,http://examplefoo.com/callback
1http://example.com/callback,http://examplefoo.com/callback

JSON API

Since your application's 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.

The JSON API is guarded by the web and auth middleware; therefore, it may only be called from your own application. It is not able to be called from an external source.

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:

1axios.get('/oauth/clients')
2 .then(response => {
3 console.log(response.data);
4 });
1axios.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:

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

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:

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

DELETE /oauth/clients/{client-id}

This route is used to delete clients:

1axios.delete('/oauth/clients/' + clientId)
2 .then(response => {
3 //
4 });
1axios.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:

1use Illuminate\Http\Request;
2use Illuminate\Support\Str;
3 
4Route::get('/redirect', function (Request $request) {
5 $request->session()->put('state', $state = Str::random(40));
6 
7 $query = http_build_query([
8 'client_id' => 'client-id',
9 'redirect_uri' => 'http://third-party-app.com/callback',
10 'response_type' => 'code',
11 'scope' => '',
12 'state' => $state,
13 ]);
14 
15 return redirect('http://passport-app.test/oauth/authorize?'.$query);
16});
1use Illuminate\Http\Request;
2use Illuminate\Support\Str;
3 
4Route::get('/redirect', function (Request $request) {
5 $request->session()->put('state', $state = Str::random(40));
6 
7 $query = http_build_query([
8 'client_id' => 'client-id',
9 'redirect_uri' => 'http://third-party-app.com/callback',
10 'response_type' => 'code',
11 'scope' => '',
12 'state' => $state,
13 ]);
14 
15 return redirect('http://passport-app.test/oauth/authorize?'.$query);
16});
lightbulb

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 the resources/views/vendor/passport directory:

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

Sometimes you may wish to skip the authorization prompt, such as when authorizing a first-party client. You may accomplish this by extending the Client model and defining a skipsAuthorization method. If skipsAuthorization returns true the client will be approved and the user will be redirected back to the redirect_uri immediately:

1<?php
2 
3namespace App\Models\Passport;
4 
5use Laravel\Passport\Client as BaseClient;
6 
7class Client extends BaseClient
8{
9 /**
10 * Determine if the client should skip the authorization prompt.
11 *
12 * @return bool
13 */
14 public function skipsAuthorization()
15 {
16 return $this->firstParty();
17 }
18}
1<?php
2 
3namespace App\Models\Passport;
4 
5use Laravel\Passport\Client as BaseClient;
6 
7class Client extends BaseClient
8{
9 /**
10 * Determine if the client should skip the authorization prompt.
11 *
12 * @return bool
13 */
14 public function skipsAuthorization()
15 {
16 return $this->firstParty();
17 }
18}

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 first verify the state parameter against the value that was stored prior to the redirect. If the state parameter matches then the consumer should 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:

1use Illuminate\Http\Request;
2use Illuminate\Support\Facades\Http;
3 
4Route::get('/callback', function (Request $request) {
5 $state = $request->session()->pull('state');
6 
7 throw_unless(
8 strlen($state) > 0 && $state === $request->state,
9 InvalidArgumentException::class
10 );
11 
12 $response = Http::asForm()->post('http://passport-app.test/oauth/token', [
13 'grant_type' => 'authorization_code',
14 'client_id' => 'client-id',
15 'client_secret' => 'client-secret',
16 'redirect_uri' => 'http://third-party-app.com/callback',
17 'code' => $request->code,
18 ]);
19 
20 return $response->json();
21});
1use Illuminate\Http\Request;
2use Illuminate\Support\Facades\Http;
3 
4Route::get('/callback', function (Request $request) {
5 $state = $request->session()->pull('state');
6 
7 throw_unless(
8 strlen($state) > 0 && $state === $request->state,
9 InvalidArgumentException::class
10 );
11 
12 $response = Http::asForm()->post('http://passport-app.test/oauth/token', [
13 'grant_type' => 'authorization_code',
14 'client_id' => 'client-id',
15 'client_secret' => 'client-secret',
16 'redirect_uri' => 'http://third-party-app.com/callback',
17 'code' => $request->code,
18 ]);
19 
20 return $response->json();
21});

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.

lightbulb

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.

JSON API

Passport also includes a JSON API for managing authorized access tokens. You may pair this with your own frontend to offer your users a dashboard for managing access tokens. For convenience, we'll use Axios to demonstrate making HTTP requests to the endpoints. The JSON API is guarded by the web and auth middleware; therefore, it may only be called from your own application.

GET /oauth/tokens

This route returns all of the authorized access tokens that the authenticated user has created. This is primarily useful for listing all of the user's tokens so that they can revoke them:

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

DELETE /oauth/tokens/{token-id}

This route may be used to revoke authorized access tokens and their related refresh tokens:

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

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:

1use Illuminate\Support\Facades\Http;
2 
3$response = Http::asForm()->post('http://passport-app.test/oauth/token', [
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 
11return $response->json();
1use Illuminate\Support\Facades\Http;
2 
3$response = Http::asForm()->post('http://passport-app.test/oauth/token', [
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 
11return $response->json();

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.

Revoking Tokens

You may revoke a token by using the revokeAccessToken method on the Laravel\Passport\TokenRepository. You may revoke a token's refresh tokens using the revokeRefreshTokensByAccessTokenId method on the Laravel\Passport\RefreshTokenRepository. These classes may be resolved using Laravel's service container:

1use Laravel\Passport\TokenRepository;
2use Laravel\Passport\RefreshTokenRepository;
3 
4$tokenRepository = app(TokenRepository::class);
5$refreshTokenRepository = app(RefreshTokenRepository::class);
6 
7// Revoke an access token...
8$tokenRepository->revokeAccessToken($tokenId);
9 
10// Revoke all of the token's refresh tokens...
11$refreshTokenRepository->revokeRefreshTokensByAccessTokenId($tokenId);
1use Laravel\Passport\TokenRepository;
2use Laravel\Passport\RefreshTokenRepository;
3 
4$tokenRepository = app(TokenRepository::class);
5$refreshTokenRepository = app(RefreshTokenRepository::class);
6 
7// Revoke an access token...
8$tokenRepository->revokeAccessToken($tokenId);
9 
10// Revoke all of the token's refresh tokens...
11$refreshTokenRepository->revokeRefreshTokensByAccessTokenId($tokenId);

Purging Tokens

When tokens have been revoked or expired, you might want to purge them from the database. Passport's included passport:purge Artisan command can do this for you:

1# Purge revoked and expired tokens and auth codes...
2php artisan passport:purge
3 
4# Only purge revoked tokens and auth codes...
5php artisan passport:purge --revoked
6 
7# Only purge expired tokens and auth codes...
8php artisan passport:purge --expired
1# Purge revoked and expired tokens and auth codes...
2php artisan passport:purge
3 
4# Only purge revoked tokens and auth codes...
5php artisan passport:purge --revoked
6 
7# Only purge expired tokens and auth codes...
8php artisan passport:purge --expired

You may also configure a scheduled job in your application's App\Console\Kernel class to automatically prune your tokens on a schedule:

1/**
2 * Define the application's command schedule.
3 *
4 * @param \Illuminate\Console\Scheduling\Schedule $schedule
5 * @return void
6 */
7protected function schedule(Schedule $schedule)
8{
9 $schedule->command('passport:purge')->hourly();
10}
1/**
2 * Define the application's command schedule.
3 *
4 * @param \Illuminate\Console\Scheduling\Schedule $schedule
5 * @return void
6 */
7protected function schedule(Schedule $schedule)
8{
9 $schedule->command('passport:purge')->hourly();
10}

Authorization Code Grant with PKCE

The Authorization Code grant with "Proof Key for Code Exchange" (PKCE) is a secure way to authenticate single page applications or native applications to access your API. This grant should be used when you can't guarantee that the client secret will be stored confidentially or in order to mitigate the threat of having the authorization code intercepted by an attacker. A combination of a "code verifier" and a "code challenge" replaces the client secret when exchanging the authorization code for an access token.

Creating The Client

Before your application can issue tokens via the authorization code grant with PKCE, you will need to create a PKCE-enabled client. You may do this using the passport:client Artisan command with the --public option:

1php artisan passport:client --public
1php artisan passport:client --public

Requesting Tokens

Code Verifier & Code Challenge

As this authorization grant does not provide a client secret, developers will need to generate a combination of a code verifier and a code challenge in order to request a token.

The code verifier should be a random string of between 43 and 128 characters containing letters, numbers, and "-", ".", "_", "~" characters, as defined in the RFC 7636 specification.

The code challenge should be a Base64 encoded string with URL and filename-safe characters. The trailing '=' characters should be removed and no line breaks, whitespace, or other additional characters should be present.

1$encoded = base64_encode(hash('sha256', $code_verifier, true));
2 
3$codeChallenge = strtr(rtrim($encoded, '='), '+/', '-_');
1$encoded = base64_encode(hash('sha256', $code_verifier, true));
2 
3$codeChallenge = strtr(rtrim($encoded, '='), '+/', '-_');

Redirecting For Authorization

Once a client has been created, you may use the client ID and the generated code verifier and code challenge 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:

1use Illuminate\Http\Request;
2use Illuminate\Support\Str;
3 
4Route::get('/redirect', function (Request $request) {
5 $request->session()->put('state', $state = Str::random(40));
6 
7 $request->session()->put(
8 'code_verifier', $code_verifier = Str::random(128)
9 );
10 
11 $codeChallenge = strtr(rtrim(
12 base64_encode(hash('sha256', $code_verifier, true))
13 , '='), '+/', '-_');
14 
15 $query = http_build_query([
16 'client_id' => 'client-id',
17 'redirect_uri' => 'http://third-party-app.com/callback',
18 'response_type' => 'code',
19 'scope' => '',
20 'state' => $state,
21 'code_challenge' => $codeChallenge,
22 'code_challenge_method' => 'S256',
23 ]);
24 
25 return redirect('http://passport-app.test/oauth/authorize?'.$query);
26});
1use Illuminate\Http\Request;
2use Illuminate\Support\Str;
3 
4Route::get('/redirect', function (Request $request) {
5 $request->session()->put('state', $state = Str::random(40));
6 
7 $request->session()->put(
8 'code_verifier', $code_verifier = Str::random(128)
9 );
10 
11 $codeChallenge = strtr(rtrim(
12 base64_encode(hash('sha256', $code_verifier, true))
13 , '='), '+/', '-_');
14 
15 $query = http_build_query([
16 'client_id' => 'client-id',
17 'redirect_uri' => 'http://third-party-app.com/callback',
18 'response_type' => 'code',
19 'scope' => '',
20 'state' => $state,
21 'code_challenge' => $codeChallenge,
22 'code_challenge_method' => 'S256',
23 ]);
24 
25 return redirect('http://passport-app.test/oauth/authorize?'.$query);
26});

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 verify the state parameter against the value that was stored prior to the redirect, as in the standard Authorization Code Grant.

If the state parameter matches, the consumer should 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 along with the originally generated code verifier:

1use Illuminate\Http\Request;
2use Illuminate\Support\Facades\Http;
3 
4Route::get('/callback', function (Request $request) {
5 $state = $request->session()->pull('state');
6 
7 $codeVerifier = $request->session()->pull('code_verifier');
8 
9 throw_unless(
10 strlen($state) > 0 && $state === $request->state,
11 InvalidArgumentException::class
12 );
13 
14 $response = Http::asForm()->post('http://passport-app.test/oauth/token', [
15 'grant_type' => 'authorization_code',
16 'client_id' => 'client-id',
17 'redirect_uri' => 'http://third-party-app.com/callback',
18 'code_verifier' => $codeVerifier,
19 'code' => $request->code,
20 ]);
21 
22 return $response->json();
23});
1use Illuminate\Http\Request;
2use Illuminate\Support\Facades\Http;
3 
4Route::get('/callback', function (Request $request) {
5 $state = $request->session()->pull('state');
6 
7 $codeVerifier = $request->session()->pull('code_verifier');
8 
9 throw_unless(
10 strlen($state) > 0 && $state === $request->state,
11 InvalidArgumentException::class
12 );
13 
14 $response = Http::asForm()->post('http://passport-app.test/oauth/token', [
15 'grant_type' => 'authorization_code',
16 'client_id' => 'client-id',
17 'redirect_uri' => 'http://third-party-app.com/callback',
18 'code_verifier' => $codeVerifier,
19 'code' => $request->code,
20 ]);
21 
22 return $response->json();
23});

Password Grant Tokens

exclamation

We no longer recommend using password grant tokens. Instead, you should choose a grant type that is currently recommended by OAuth2 Server.

The OAuth2 password grant allows your other first-party clients, such as a mobile application, to obtain an access token using an email 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 Artisan command with the --password option. If you have already run the passport:install command, you do not need to run this command:

1php artisan passport:client --password
1php 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:

1use Illuminate\Support\Facades\Http;
2 
3$response = Http::asForm()->post('http://passport-app.test/oauth/token', [
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 
12return $response->json();
1use Illuminate\Support\Facades\Http;
2 
3$response = Http::asForm()->post('http://passport-app.test/oauth/token', [
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 
12return $response->json();
lightbulb

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 or client credentials 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 or client_credentials grant:

1use Illuminate\Support\Facades\Http;
2 
3$response = Http::asForm()->post('http://passport-app.test/oauth/token', [
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]);
1use Illuminate\Support\Facades\Http;
2 
3$response = Http::asForm()->post('http://passport-app.test/oauth/token', [
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]);

Customizing The User Provider

If your application uses more than one authentication user provider, you may specify which user provider the password grant client uses by providing a --provider option when creating the client via the artisan passport:client --password command. The given provider name should match a valid provider defined in your application's config/auth.php configuration file. You can then protect your route using middleware to ensure that only users from the guard's specified provider are authorized.

Customizing The Username Field

When authenticating using the password grant, Passport will use the email attribute of your authenticatable model as the "username". However, you may customize this behavior by defining a findForPassport method on your model:

1<?php
2 
3namespace App\Models;
4 
5use Illuminate\Foundation\Auth\User as Authenticatable;
6use Illuminate\Notifications\Notifiable;
7use Laravel\Passport\HasApiTokens;
8 
9class User extends Authenticatable
10{
11 use HasApiTokens, Notifiable;
12 
13 /**
14 * Find the user instance for the given username.
15 *
16 * @param string $username
17 * @return \App\Models\User
18 */
19 public function findForPassport($username)
20 {
21 return $this->where('username', $username)->first();
22 }
23}
1<?php
2 
3namespace App\Models;
4 
5use Illuminate\Foundation\Auth\User as Authenticatable;
6use Illuminate\Notifications\Notifiable;
7use Laravel\Passport\HasApiTokens;
8 
9class User extends Authenticatable
10{
11 use HasApiTokens, Notifiable;
12 
13 /**
14 * Find the user instance for the given username.
15 *
16 * @param string $username
17 * @return \App\Models\User
18 */
19 public function findForPassport($username)
20 {
21 return $this->where('username', $username)->first();
22 }
23}

Customizing The Password Validation

When authenticating using the password grant, Passport will use the password attribute of your model to validate the given password. If your model does not have a password attribute or you wish to customize the password validation logic, you can define a validateForPassportPasswordGrant method on your model:

1<?php
2 
3namespace App\Models;
4 
5use Illuminate\Foundation\Auth\User as Authenticatable;
6use Illuminate\Notifications\Notifiable;
7use Illuminate\Support\Facades\Hash;
8use Laravel\Passport\HasApiTokens;
9 
10class User extends Authenticatable
11{
12 use HasApiTokens, Notifiable;
13 
14 /**
15 * Validate the password of the user for the Passport password grant.
16 *
17 * @param string $password
18 * @return bool
19 */
20 public function validateForPassportPasswordGrant($password)
21 {
22 return Hash::check($password, $this->password);
23 }
24}
1<?php
2 
3namespace App\Models;
4 
5use Illuminate\Foundation\Auth\User as Authenticatable;
6use Illuminate\Notifications\Notifiable;
7use Illuminate\Support\Facades\Hash;
8use Laravel\Passport\HasApiTokens;
9 
10class User extends Authenticatable
11{
12 use HasApiTokens, Notifiable;
13 
14 /**
15 * Validate the password of the user for the Passport password grant.
16 *
17 * @param string $password
18 * @return bool
19 */
20 public function validateForPassportPasswordGrant($password)
21 {
22 return Hash::check($password, $this->password);
23 }
24}

Implicit Grant Tokens

exclamation

We no longer recommend using implicit grant tokens. Instead, you should choose a grant type that is currently recommended by OAuth2 Server.

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 the boot method of your application's App\Providers\AuthServiceProvider class:

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

Once the 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:

1use Illuminate\Http\Request;
2 
3Route::get('/redirect', function (Request $request) {
4 $request->session()->put('state', $state = Str::random(40));
5 
6 $query = http_build_query([
7 'client_id' => 'client-id',
8 'redirect_uri' => 'http://third-party-app.com/callback',
9 'response_type' => 'token',
10 'scope' => '',
11 'state' => $state,
12 ]);
13 
14 return redirect('http://passport-app.test/oauth/authorize?'.$query);
15});
1use Illuminate\Http\Request;
2 
3Route::get('/redirect', function (Request $request) {
4 $request->session()->put('state', $state = Str::random(40));
5 
6 $query = http_build_query([
7 'client_id' => 'client-id',
8 'redirect_uri' => 'http://third-party-app.com/callback',
9 'response_type' => 'token',
10 'scope' => '',
11 'state' => $state,
12 ]);
13 
14 return redirect('http://passport-app.test/oauth/authorize?'.$query);
15});
lightbulb

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.

Before your application can issue tokens via the client credentials grant, you will need to create a client credentials grant client. You may do this using the --client option of the passport:client Artisan command:

1php artisan passport:client --client
1php artisan passport:client --client

Next, to use this grant type, you need to add the CheckClientCredentials middleware to the $routeMiddleware property of your app/Http/Kernel.php file:

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

Then, attach the middleware to a route:

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

To restrict access to the route to specific scopes, you may provide a comma-delimited list of the required scopes when attaching the client middleware to the route:

1Route::get('/orders', function (Request $request) {
2 ...
3})->middleware('client:check-status,your-scope');
1Route::get('/orders', function (Request $request) {
2 ...
3})->middleware('client:check-status,your-scope');

Retrieving Tokens

To retrieve a token using this grant type, make a request to the oauth/token endpoint:

1use Illuminate\Support\Facades\Http;
2 
3$response = Http::asForm()->post('http://passport-app.test/oauth/token', [
4 'grant_type' => 'client_credentials',
5 'client_id' => 'client-id',
6 'client_secret' => 'client-secret',
7 'scope' => 'your-scope',
8]);
9 
10return $response->json()['access_token'];
1use Illuminate\Support\Facades\Http;
2 
3$response = Http::asForm()->post('http://passport-app.test/oauth/token', [
4 'grant_type' => 'client_credentials',
5 'client_id' => 'client-id',
6 'client_secret' => 'client-secret',
7 'scope' => 'your-scope',
8]);
9 
10return $response->json()['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.

lightbulb

If your application is primarily using Passport to issue personal access tokens, consider using Laravel Sanctum, Laravel's light-weight first-party library for issuing API access tokens.

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 by executing the passport:client Artisan command with the --personal option. If you have already run the passport:install command, you do not need to run this command:

1php artisan passport:client --personal
1php artisan passport:client --personal

After creating your personal access client, place the client's ID and plain-text secret value in your application's .env file:

1PASSPORT_PERSONAL_ACCESS_CLIENT_ID="client-id-value"
2PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET="unhashed-client-secret-value"
1PASSPORT_PERSONAL_ACCESS_CLIENT_ID="client-id-value"
2PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET="unhashed-client-secret-value"

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 App\Models\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:

1use App\Models\User;
2 
3$user = User::find(1);
4 
5// Creating a token without scopes...
6$token = $user->createToken('Token Name')->accessToken;
7 
8// Creating a token with scopes...
9$token = $user->createToken('My Token', ['place-orders'])->accessToken;
1use App\Models\User;
2 
3$user = User::find(1);
4 
5// Creating a token without scopes...
6$token = $user->createToken('Token Name')->accessToken;
7 
8// Creating a token with scopes...
9$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.

The JSON API is guarded by the web and auth middleware; therefore, it may only be called from your own application. It is not able to be called from an external source.

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:

1axios.get('/oauth/scopes')
2 .then(response => {
3 console.log(response.data);
4 });
1axios.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 revoke them:

1axios.get('/oauth/personal-access-tokens')
2 .then(response => {
3 console.log(response.data);
4 });
1axios.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:

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

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

This route may be used to revoke personal access tokens:

1axios.delete('/oauth/personal-access-tokens/' + tokenId);
1axios.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 should require a valid access token:

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

If you are using the client credentials grant, you should use the client middleware to protect your routes instead of the auth:api middleware.

Multiple Authentication Guards

If your application authenticates different types of users that perhaps use entirely different Eloquent models, you will likely need to define a guard configuration for each user provider type in your application. This allows you to protect requests intended for specific user providers. For example, given the following guard configuration the config/auth.php configuration file:

1'api' => [
2 'driver' => 'passport',
3 'provider' => 'users',
4],
5 
6'api-customers' => [
7 'driver' => 'passport',
8 'provider' => 'customers',
9],
1'api' => [
2 'driver' => 'passport',
3 'provider' => 'users',
4],
5 
6'api-customers' => [
7 'driver' => 'passport',
8 'provider' => 'customers',
9],

The following route will utilize the api-customers guard, which uses the customers user provider, to authenticate incoming requests:

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

For more information on using multiple user providers with Passport, please consult the password grant documentation.

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:

1use Illuminate\Support\Facades\Http;
2 
3$response = Http::withHeaders([
4 'Accept' => 'application/json',
5 'Authorization' => 'Bearer '.$accessToken,
6])->get('https://passport-app.test/api/user');
7 
8return $response->json();
1use Illuminate\Support\Facades\Http;
2 
3$response = Http::withHeaders([
4 'Accept' => 'application/json',
5 'Authorization' => 'Bearer '.$accessToken,
6])->get('https://passport-app.test/api/user');
7 
8return $response->json();

Token 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.

Defining Scopes

You may define your API's scopes using the Passport::tokensCan method in the boot method of your application's App\Providers\AuthServiceProvider class. 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/**
2 * Register any authentication / authorization services.
3 *
4 * @return void
5 */
6public function boot()
7{
8 $this->registerPolicies();
9 
10 Passport::routes();
11 
12 Passport::tokensCan([
13 'place-orders' => 'Place orders',
14 'check-status' => 'Check order status',
15 ]);
16}
1/**
2 * Register any authentication / authorization services.
3 *
4 * @return void
5 */
6public function boot()
7{
8 $this->registerPolicies();
9 
10 Passport::routes();
11 
12 Passport::tokensCan([
13 'place-orders' => 'Place orders',
14 'check-status' => 'Check order status',
15 ]);
16}

Default Scope

If a client does not request any specific scopes, you may configure your Passport server to attach default scope(s) to the token using the setDefaultScope method. Typically, you should call this method from the boot method of your application's App\Providers\AuthServiceProvider class:

1use Laravel\Passport\Passport;
2 
3Passport::tokensCan([
4 'place-orders' => 'Place orders',
5 'check-status' => 'Check order status',
6]);
7 
8Passport::setDefaultScope([
9 'check-status',
10 'place-orders',
11]);
1use Laravel\Passport\Passport;
2 
3Passport::tokensCan([
4 'place-orders' => 'Place orders',
5 'check-status' => 'Check order status',
6]);
7 
8Passport::setDefaultScope([
9 'check-status',
10 'place-orders',
11]);

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:

1Route::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 
9 return redirect('http://passport-app.test/oauth/authorize?'.$query);
10});
1Route::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 
9 return redirect('http://passport-app.test/oauth/authorize?'.$query);
10});

When Issuing Personal Access Tokens

If you are issuing personal access tokens using the App\Models\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;
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,
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:

1Route::get('/orders', function () {
2 // Access token has both "check-status" and "place-orders" scopes...
3})->middleware(['auth:api', 'scopes:check-status,place-orders']);
1Route::get('/orders', function () {
2 // Access token has both "check-status" and "place-orders" scopes...
3})->middleware(['auth:api', '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:

1Route::get('/orders', function () {
2 // Access token has either "check-status" or "place-orders" scope...
3})->middleware(['auth:api', 'scope:check-status,place-orders']);
1Route::get('/orders', function () {
2 // Access token has either "check-status" or "place-orders" scope...
3})->middleware(['auth:api', '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 App\Models\User instance:

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

Additional Scope Methods

The scopeIds method will return an array of all defined IDs / names:

1use Laravel\Passport\Passport;
2 
3Passport::scopeIds();
1use Laravel\Passport\Passport;
2 
3Passport::scopeIds();

The scopes method will return an array of all defined scopes as instances of Laravel\Passport\Scope:

1Passport::scopes();
1Passport::scopes();

The scopesFor method will return an array of Laravel\Passport\Scope instances matching the given IDs / names:

1Passport::scopesFor(['place-orders', 'check-status']);
1Passport::scopesFor(['place-orders', 'check-status']);

You may determine if a given scope has been defined using the hasScope method:

1Passport::hasScope('place-orders');
1Passport::hasScope('place-orders');

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 in your app/Http/Kernel.php file:

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

You should ensure that the CreateFreshApiToken middleware is the last middleware listed in your middleware stack.

This 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. The JWT has a lifetime equal to your session.lifetime configuration value. Now, since the browser will automatically send the cookie with all subsequent requests, you may make requests to your application's API without explicitly passing an access token:

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

If needed, you can customize the laravel_token cookie's name using the Passport::cookie method. Typically, this method should be called from the boot method of your application's App\Providers\AuthServiceProvider class:

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

CSRF Protection

When using this method of authentication, you will need to ensure a valid CSRF token header is included in your requests. The default Laravel JavaScript scaffolding includes an Axios instance, which will automatically use the encrypted XSRF-TOKEN cookie value to send an X-XSRF-TOKEN header on same-origin requests.

lightbulb

If you choose to send the X-CSRF-TOKEN header instead of X-XSRF-TOKEN, you will need to use the unencrypted token provided by csrf_token().

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. If you would like, you may attach listeners to these events in your application's App\Providers\EventServiceProvider class:

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

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:

1use App\Models\User;
2use Laravel\Passport\Passport;
3 
4public function test_servers_can_be_created()
5{
6 Passport::actingAs(
7 User::factory()->create(),
8 ['create-servers']
9 );
10 
11 $response = $this->post('/api/create-server');
12 
13 $response->assertStatus(201);
14}
1use App\Models\User;
2use Laravel\Passport\Passport;
3 
4public function test_servers_can_be_created()
5{
6 Passport::actingAs(
7 User::factory()->create(),
8 ['create-servers']
9 );
10 
11 $response = $this->post('/api/create-server');
12 
13 $response->assertStatus(201);
14}

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

1use Laravel\Passport\Client;
2use Laravel\Passport\Passport;
3 
4public function test_orders_can_be_retrieved()
5{
6 Passport::actingAsClient(
7 Client::factory()->create(),
8 ['check-status']
9 );
10 
11 $response = $this->get('/api/orders');
12 
13 $response->assertStatus(200);
14}
1use Laravel\Passport\Client;
2use Laravel\Passport\Passport;
3 
4public function test_orders_can_be_retrieved()
5{
6 Passport::actingAsClient(
7 Client::factory()->create(),
8 ['check-status']
9 );
10 
11 $response = $this->get('/api/orders');
12 
13 $response->assertStatus(200);
14}
翻譯進度
0% 已翻譯
更新時間:
2024年6月30日 上午8:15:00 [世界標準時間]
翻譯人員:
  • cornch
幫我們翻譯此頁

留言

尚無留言

“Laravel” is a Trademark of Taylor Otwell.
The source documentation is released under MIT license. See laravel/docs on GitHub for details.
The translated documentations are released under MIT license. See cornch/laravel-docs-l10n on GitHub for details.