Laravel Passport integration
Using Passport in the tenant application only
Section titled “Using Passport in the tenant application only”To use Passport inside the tenant part of your application, you may do the following.
-
Publish the Passport migrations by running
php artisan vendor:publish --tag=passport-migrationsand move them to your tenant migration directory (database/migrations/tenant/). -
Publish the Passport config by running
php artisan vendor:publish --tag=passport-config. If you’re using Passport 10.x, make Passport use the default database connection by setting the storage database connection tonull. Thepassport:keyscommand puts the keys in thestorage/directory by default – you can change that by setting the key path in the config.return ['storage' => [ // Needed only when using Passport 10.x'database' => ['connection' => null,],],'key_path' => env('OAUTH_KEY_PATH', 'storage') // This is optional]; -
Prevent Passport migrations from running in the central application by adding
Passport::ignoreMigrations()to theregister()method in yourAuthServiceProvider. -
If you’re using Passport 10.x, register the Passport routes in your
AuthServiceProviderby adding the following code to the provider’sboot()method:Passport::routes(null, ['middleware' => [InitializeTenancyByDomain::class, // Or other identification middleware of your choicePreventAccessFromCentralDomains::class,]]); -
If you’re using Passport 11.x, disable the automatic Passport route registering in your
AuthServiceProviderby addingPassport::ignoreRoutes();to theregister()method. Then, register the Passport routes manually by adding the following code to theboot()method:Route::group(['as' => 'passport.','middleware' => [InitializeTenancyByDomain::class, // Use tenancy initialization middleware of your choicePreventAccessFromCentralDomains::class,],'prefix' => config('passport.path', 'oauth'),'namespace' => 'Laravel\Passport\Http\Controllers',], function () {$this->loadRoutesFrom(__DIR__ . "/../../vendor/laravel/passport/src/../routes/web.php");}); -
Apply Passport migrations by running
php artisan tenants:migrate. -
Set up the encryption keys.
Using Passport in both the tenant and the central application
Section titled “Using Passport in both the tenant and the central application”To use Passport in both the tenant and the central application:
-
Follow the steps for using Passport in the tenant appliction.
-
Copy the Passport migrations to the central application, so that the Passport migrations are in both the central and the tenant application.
-
Remove
Passport::ignoreMigrations()from theregister()method in yourAuthServiceProvider(if it is there). -
In your
AuthServiceProvider’sboot()method (where you registered the Passport routes), add the'universal'middleware to the Passport routes, and remove thePreventAccessFromCentralDomains::classmiddleware. The related code in yourboot()method should look like this:
// Passport 10.xPassport::routes(null, ['middleware' => [ 'universal', InitializeTenancyByDomain::class]]);
// Passport 11.xRoute::group([ 'as' => 'passport.', 'middleware' => [ 'universal', InitializeTenancyByDomain::class ], 'prefix' => config('passport.path', 'oauth'), 'namespace' => 'Laravel\Passport\Http\Controllers',], function () { $this->loadRoutesFrom(__DIR__ . "/../../vendor/laravel/passport/src/../routes/web.php");});Passport encryption keys
Section titled “Passport encryption keys”Shared keys
Section titled “Shared keys”To generate a single Passport key pair for the whole app, create Passport clients for your tenants by adding the following code to your tenant database seeder.
public function run(){ $client = new ClientRepository();
$client->createPasswordGrantClient(null, 'Default password grant client', 'http://your.redirect.path'); $client->createPersonalAccessClient(null, 'Default personal access client', 'http://your.redirect.path');}You can set your tenant database seeder class in config/tenancy.php file at seeder_parameters key.
Then, seed the database and generate the key pair by running php artisan passport:keys.
Tenant-specific keys
Section titled “Tenant-specific keys”If you want to use a unique Passport key pair for each tenant, there are multiple ways to store and load tenant Passport keys. The most straightforward way is to store them in the Tenant model and load them into the Passport configuration using the Tenant Config feature. Then, you can access the keys like $tenant->passport_public_key.
To achieve that, enable the Tenant Config feature, and configure the storage-to-config mapping in the boot method of your TenancyServiceProvider this way:
\Stancl\Tenancy\Features\TenantConfig::$storageToConfigMap = [ 'passport_public_key' => 'passport.public_key', 'passport_private_key' => 'passport.private_key',];