Laravel Passport
repository·13.x·Indexed 25 days ago
https://github.com/laravel/passportAn OAuth2 server implementation for Laravel that provides a robust way to handle API authentication and issue access tokens. It includes features for managing OAuth2 clients, defining custom scopes, configuring token expiration intervals, and supporting various grant types including implicit, password, and device code grants.
What's inside Laravel Passport
- Laravel Passport is an OAuth2 server and API authentication package designed for Laravel applications. It provides a complete implementation of the OAuth2 server specification, allowing you to issue access tokens to your clients to authenticate API requests.
Enable Client Credentials Secret Hashing in Passport 9.0
13.xPassport 9.0 allows storing client secrets using Bcrypt hashes. Warning: This process is irreversible.
1. Secure Personal Access Client Credentials
Before hashing, set your personal access client ID and unhashed secret in your
.envfile:PASSPORT_PERSONAL_ACCESS_CLIENT_ID=client-id-value PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET=unhashed-client-secret-valueThen, register them in the
bootmethod of yourAppServiceProvider:Passport::personalAccessClientId(config('passport.personal_access_client.id')); Passport::personalAccessClientSecret(config('passport.personal_access_client.secret'));2. Enable Hashing
Call
Passport::hashClientSecrets()in thebootmethod of yourAppServiceProvider.3. Hash Existing Secrets
Run the following Artisan command to hash all existing client secrets. Back up your database before running this command.
php artisan passport:hashPASSPORT_PERSONAL_ACCESS_CLIENT_ID=client-id-value PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET=unhashed-client-secret-valuePassport::personalAccessClientId(config('passport.personal_access_client.id')); Passport::personalAccessClientSecret(config('passport.personal_access_client.secret'));php artisan passport:hashUpgrade to Passport 8.0 from 7.x
13.xWhen upgrading to version 8.0, ensure your environment meets the following minimum requirements:
- Laravel: 6.0
- PHP: 7.2
- league/oauth2-server: v8
Enable Public Clients and PKCE
To support public clients and PKCE, update the
secretcolumn of theoauth_clientstable to benullable:Schema::table('oauth_clients', function (Blueprint $table) { $table->string('secret', 100)->nullable()->change(); });Handle OAuth Exceptions
OAuth exceptions are now rendered as Passport exceptions. If you explicitly handle
League\OAuth2\Server\Exception\OAuthServerExceptionin your exception handler'sreportmethod, you must now check forLaravel\Passport\Exceptions\OAuthServerExceptioninstead.Configure Multiple Guard Support in Passport 9.0
13.xPassport 9.0 supports multiple guard user providers. You must add a
providercolumn to theoauth_clientsdatabase table. If you have not published Passport migrations, add it manually:Schema::table('oauth_clients', function (Blueprint $table) { $table->string('provider')->after('secret')->nullable(); });Publish Passport migrations (Passport 12.0+)
13.xStarting with Passport 12.0, migrations are no longer automatically loaded from the package directory. You must publish them to your application using the following command:
php artisan vendor:publish --tag=passport-migrationsHash existing client secrets in Passport 13.0
13.xPassport 13.0 hashes client secrets by default using Laravel's
Hashfacade. If you are currently storing secrets in plain text, you must run the following Artisan command to hash them:php artisan passport:hashUpgrade to Passport 10.0 from 9.x
13.xWhen upgrading to version 10.0, ensure your environment meets the following minimum requirements:
- PHP: 7.3
- Laravel: 8.0
Removed Methods
The personal client configuration methods have been removed from the
Passportclass. You should remove any calls to these methods from your application's service providers.Upgrade to Passport 9.0 from 8.x
13.xWhen upgrading to version 9.0, follow these steps to support multiple guard user providers and client secret hashing.Migrate oauth_clients table to new schema in Passport 13.0
13.xPassport 13.0 introduces a new schema for
oauth_clientsthat is backward compatible, but it is highly recommended to migrate if you use integer-based client IDs. The new schema replacesuser_idwithowner_type/owner_id,redirectwithredirect_uris(array), and client type columns with agrant_types(array) column.// Example migration snippet for the new schema Schema::table('oauth_clients', function (Blueprint $table) { $table->nullableMorphs('owner', after: 'user_id'); $table->after('provider', function (Blueprint $table) { $table->text('redirect_uris')->nullable(); $table->text('grant_types')->nullable(); }); }); foreach (Passport::client()->cursor() as $client) { Model::withoutTimestamps(fn () => $client->forceFill([ 'owner_id' => $client->user_id, 'owner_type' => $client->user_id ? config('auth.providers.'.($client->provider ?: config('auth.guards.api.provider')).'.model') : null, 'redirect_uris' => $client->redirect_uris, 'grant_types' => $client->grant_types, ])->save()); } Schema::table('oauth_clients', function (Blueprint $table) { $table->dropColumn(['user_id', 'redirect', 'personal_access_client', 'password_client']); $table->text('redirect_uris')->nullable(false)->change(); $table->text('grant_types')->nullable(false)->change(); });Upgrade to Passport 13.0 from 12.x
13.xWhen upgrading to Passport 13.0, ensure your environment meets the following minimum requirements:
- PHP: 8.2 or higher
- Laravel: 11.35 or higher
- Internal Dependency:
league/oauth2-serverhas been updated to 9.0. Review its changelog for potential signature changes in methods you might override.
Implement OAuthenticatable interface in User model
13.xIn Passport 13.0, yourApp\\Models\\Usermodel must implement theLaravel\\Passport\\Contracts\\OAuthenticatableinterface alongside theHasApiTokenstrait.Upgrade to Passport 11.0 from 10.x
13.xWhen upgrading to version 11.0, ensure your environment meets the following minimum requirements:
- PHP: 8.0
- Laravel: 9.0
Key Changes
Database Connection Customization
Customizing model database connections via migration files has been reverted. To customize the database connection for a model, you must now override the models as described in the official documentation.
Token Model Timestamps
The
Tokenmodel now allows timestamps. If you require timestamps to be disabled for this model, you must override theTokenmodel.Route Refactoring
Passport routes have moved to a dedicated route file. You can now remove the
Passport::routes()call from your application's service provider. If you previously usedroutes($callback = null, array $options = [])to overwrite routes, you should now overwrite them directly in your application'sweb.phproute file.