Laravel Passport

repository·13.x·Indexed 25 days ago

https://github.com/laravel/passport

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

Tokens
6.2K
Snippets
15
Records
50
Agent score
84%

What's inside Laravel Passport

  1. Introduction to Laravel Passport

    13.x
    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.
  2. Enable Client Credentials Secret Hashing in Passport 9.0

    13.x

    Passport 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 .env file:

    PASSPORT_PERSONAL_ACCESS_CLIENT_ID=client-id-value
    PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET=unhashed-client-secret-value

    Then, register them in the boot method of your AppServiceProvider:

    Passport::personalAccessClientId(config('passport.personal_access_client.id'));
    Passport::personalAccessClientSecret(config('passport.personal_access_client.secret'));

    2. Enable Hashing

    Call Passport::hashClientSecrets() in the boot method of your AppServiceProvider.

    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:hash
    PASSPORT_PERSONAL_ACCESS_CLIENT_ID=client-id-value
    PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET=unhashed-client-secret-value
    Passport::personalAccessClientId(config('passport.personal_access_client.id'));
    Passport::personalAccessClientSecret(config('passport.personal_access_client.secret'));
    php artisan passport:hash
  3. Upgrade to Passport 8.0 from 7.x

    13.x

    When 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 secret column of the oauth_clients table to be nullable:

    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\OAuthServerException in your exception handler's report method, you must now check for Laravel\Passport\Exceptions\OAuthServerException instead.

  4. Configure Multiple Guard Support in Passport 9.0

    13.x

    Passport 9.0 supports multiple guard user providers. You must add a provider column to the oauth_clients database table. If you have not published Passport migrations, add it manually:

    Schema::table('oauth_clients', function (Blueprint $table) {
        $table->string('provider')->after('secret')->nullable();
    });
  5. Publish Passport migrations (Passport 12.0+)

    13.x

    Starting 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-migrations
  6. Hash existing client secrets in Passport 13.0

    13.x

    Passport 13.0 hashes client secrets by default using Laravel's Hash facade. If you are currently storing secrets in plain text, you must run the following Artisan command to hash them:

    php artisan passport:hash
  7. Upgrade to Passport 10.0 from 9.x

    13.x

    When 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 Passport class. You should remove any calls to these methods from your application's service providers.

  8. Migrate oauth_clients table to new schema in Passport 13.0

    13.x

    Passport 13.0 introduces a new schema for oauth_clients that is backward compatible, but it is highly recommended to migrate if you use integer-based client IDs. The new schema replaces user_id with owner_type/owner_id, redirect with redirect_uris (array), and client type columns with a grant_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();
    });
  9. Upgrade to Passport 13.0 from 12.x

    13.x

    When 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-server has been updated to 9.0. Review its changelog for potential signature changes in methods you might override.
  10. Upgrade to Passport 11.0 from 10.x

    13.x

    When 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 Token model now allows timestamps. If you require timestamps to be disabled for this model, you must override the Token model.

    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 used routes($callback = null, array $options = []) to overwrite routes, you should now overwrite them directly in your application's web.php route file.