Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I configure Laravel Pennant to work with Laravel Passport


How do I configure Laravel Pennant to work with Laravel Passport


To configure Laravel Pennant to work with Laravel Passport, you essentially integrate feature flags (via Pennant) into your OAuth2 authentication and API token management flow (via Passport). Here's a detailed step-by-step guide combining both packages:

1. Install and Configure Laravel Passport

- Install Passport via Composer:


  composer require laravel/passport
  

- Run migrations to create Passport's tables:

  php artisan migrate
  

- Generate encryption keys and clients:

  php artisan passport:install
  

- Add the `HasApiTokens` trait to your User model to enable token management:
php
  use Laravel\Passport\HasApiTokens;

  class User extends Authenticatable
  {
      use HasApiTokens, Notifiable;
      // ...
  }
  

- Set the API guard driver to `passport` in `config/auth.php`:
php
  'guards' => [
      'api' => [
          'driver' => 'passport',
          'provider' => 'users',
      ],
  ],
  

- Register Passport routes in your `AuthServiceProvider`'s `boot` method:
php
  use Laravel\Passport\Passport;

  public function boot()
  {
      $this->registerPolicies();
      Passport::routes();
  }
  

This sets up Passport to handle OAuth2 authentication and token issuance[2][6][11].

2. Install and Configure Laravel Pennant

- Install Pennant via Composer:


  composer require laravel/pennant
  

- Publish Pennant's config and migration files:

  php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider"
  

- Run migrations to create the `features` table:

  php artisan migrate
  

- Configure feature flags in `config/pennant.php` as needed, or define features programmatically[1][7][12].

3. Integrate Pennant Feature Flags with Passport Authentication

To use Pennant feature flags to control or customize Passport's OAuth2 authentication behavior, you can do the following:

- Conditional Authentication Logic: Use Pennant to enable or disable certain OAuth2 grant types or features dynamically. For example, in your `AuthServiceProvider` or middleware, check feature flags before allowing password grant token issuance.

- Protect API Routes Based on Feature Flags: Use Pennant's feature checks to restrict access to certain API routes that require Passport authentication. For example:

php
  use Illuminate\Support\Facades\Route;
  use Laravel\Pennant\Feature;

  Route::middleware(['auth:api'])->group(function () {
      Route::get('/premium-data', function () {
          if (!Feature::active('premium_access')) {
              abort(403, 'Feature not available');
          }
          // Return premium data
      });
  });
  

- Customize User Authentication Flow: In your User model or Auth controllers, check feature flags to enable or disable login methods or token issuance. For example, you might disable password grant authentication if a feature flag is off.

- Feature-Flagged Token Scopes: When issuing tokens with Passport, you can conditionally assign scopes based on feature flags:

php
  if (Feature::active('beta_feature')) {
      $token = $user->createToken('Token Name', ['beta-access'])->accessToken;
  } else {
      $token = $user->createToken('Token Name')->accessToken;
  }
  

Summary

- Set up Laravel Passport fully for OAuth2 authentication, including migrations, service provider, user model trait, and API guard configuration.
- Install Laravel Pennant, publish its config and migration, and migrate the database.
- Use Pennant's feature flag checks (`Feature::active('flag_name')`) in your authentication logic, route protection, and token issuance to control Passport behavior dynamically.

This approach allows you to leverage Laravel Pennant's feature flagging capabilities to enable or disable Passport authentication features or API access seamlessly without redeploying your app.

For detailed usage of each package, refer to the official Laravel documentation on Pennant and Passport[1][2][6][7].

Citations:
[1] https://laravel.com/docs/11.x/pennant
[2] https://laravel.com/docs/11.x/passport
[3] https://blog.logrocket.com/laravel-passport-a-tutorial-and-example-build/
[4] https://www.youtube.com/watch?v=K7RfBgoeg48
[5] https://laravel.com/docs/6.x/passport
[6] https://clouddevs.com/laravel/install-and-configure-passport/
[7] https://www.honeybadger.io/blog/a-guide-to-feature-flags-in-laravel/
[8] https://github.com/laravel/pennant
[9] https://laravel.com/docs/5.4/passport
[10] https://www.toptal.com/laravel/passport-tutorial-auth-user-access
[11] https://laravel.com/docs/7.x/passport
[12] https://wpwebinfotech.com/blog/laravel-pennant-guide/