Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Support Laravel Passport #1521

Draft
wants to merge 22 commits into
base: 5.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"require-dev": {
"inertiajs/inertia-laravel": "^1.0",
"laravel/passport": "13.x-dev",
"laravel/sanctum": "^4.0",
"livewire/livewire": "^3.3",
"mockery/mockery": "^1.0",
Expand Down
29 changes: 24 additions & 5 deletions routes/inertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

use Illuminate\Support\Facades\Route;
use Laravel\Jetstream\Http\Controllers\CurrentTeamController;
use Laravel\Jetstream\Http\Controllers\Inertia\ApiTokenController;
use Laravel\Jetstream\Http\Controllers\Inertia\ApiTokenController as SanctumApiTokenController;
use Laravel\Jetstream\Http\Controllers\Inertia\CurrentUserController;
use Laravel\Jetstream\Http\Controllers\Inertia\OAuthAppController;
use Laravel\Jetstream\Http\Controllers\Inertia\OAuthConnectionController;
use Laravel\Jetstream\Http\Controllers\Inertia\OtherBrowserSessionsController;
use Laravel\Jetstream\Http\Controllers\Inertia\PassportApiTokenController;
use Laravel\Jetstream\Http\Controllers\Inertia\PrivacyPolicyController;
use Laravel\Jetstream\Http\Controllers\Inertia\ProfilePhotoController;
use Laravel\Jetstream\Http\Controllers\Inertia\TeamController;
Expand Down Expand Up @@ -47,10 +50,26 @@
Route::group(['middleware' => 'verified'], function () {
// API...
if (Jetstream::hasApiFeatures()) {
Route::get('/user/api-tokens', [ApiTokenController::class, 'index'])->name('api-tokens.index');
Route::post('/user/api-tokens', [ApiTokenController::class, 'store'])->name('api-tokens.store');
Route::put('/user/api-tokens/{token}', [ApiTokenController::class, 'update'])->name('api-tokens.update');
Route::delete('/user/api-tokens/{token}', [ApiTokenController::class, 'destroy'])->name('api-tokens.destroy');
if (Jetstream::hasOAuthFeatures()) {
Route::get('/user/api-tokens', [PassportApiTokenController::class, 'index'])->name('api-tokens.index');
Route::post('/user/api-tokens', [PassportApiTokenController::class, 'store'])->name('api-tokens.store');
Route::delete('/user/api-tokens/{token}', [PassportApiTokenController::class, 'destroy'])->name('api-tokens.destroy');
} else {
Route::get('/user/api-tokens', [SanctumApiTokenController::class, 'index'])->name('api-tokens.index');
Route::post('/user/api-tokens', [SanctumApiTokenController::class, 'store'])->name('api-tokens.store');
Route::put('/user/api-tokens/{token}', [SanctumApiTokenController::class, 'update'])->name('api-tokens.update');
Route::delete('/user/api-tokens/{token}', [SanctumApiTokenController::class, 'destroy'])->name('api-tokens.destroy');
}
}

// OAuth...
if (Jetstream::hasOAuthFeatures()) {
Route::get('/user/oauth-apps', [OAuthAppController::class, 'index'])->name('oauth-apps.index');
Route::post('/user/oauth-apps', [OAuthAppController::class, 'store'])->name('oauth-apps.store');
Route::put('/user/oauth-apps/{app}', [OAuthAppController::class, 'update'])->name('oauth-apps.update');
Route::delete('/user/oauth-apps/{app}', [OAuthAppController::class, 'destroy'])->name('oauth-apps.destroy');

Route::delete('/user/oauth-connections/{app}', [OAuthConnectionController::class, 'destroy'])->name('oauth-connections.destroy');
}

// Teams...
Expand Down
6 changes: 6 additions & 0 deletions routes/livewire.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Support\Facades\Route;
use Laravel\Jetstream\Http\Controllers\CurrentTeamController;
use Laravel\Jetstream\Http\Controllers\Livewire\ApiTokenController;
use Laravel\Jetstream\Http\Controllers\Livewire\OAuthAppController;
use Laravel\Jetstream\Http\Controllers\Livewire\PrivacyPolicyController;
use Laravel\Jetstream\Http\Controllers\Livewire\TeamController;
use Laravel\Jetstream\Http\Controllers\Livewire\TermsOfServiceController;
Expand Down Expand Up @@ -34,6 +35,11 @@
Route::get('/user/api-tokens', [ApiTokenController::class, 'index'])->name('api-tokens.index');
}

// OAuth...
if (Jetstream::hasOAuthFeatures()) {
Route::get('/user/oauth-apps', [OAuthAppController::class, 'index'])->name('oauth-apps.index');
}

// Teams...
if (Jetstream::hasTeamFeatures()) {
Route::get('/teams/create', [TeamController::class, 'create'])->name('teams.create');
Expand Down
58 changes: 55 additions & 3 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class InstallCommand extends Command implements PromptsForMissingInput
{--dark : Indicate that dark mode support should be installed}
{--teams : Indicates if team support should be installed}
{--api : Indicates if API support should be installed}
{--oauth : Indicates if OAuth support via Laravel Passport should be installed}
{--verification : Indicates if email verification support should be installed}
{--pest : Indicates if Pest should be installed}
{--ssr : Indicates if Inertia SSR support should be installed}
Expand Down Expand Up @@ -87,6 +88,12 @@ public function handle()
$this->replaceInFile('// Features::api(),', 'Features::api(),', config_path('jetstream.php'));
}

// Configure OAuth...
if ($this->option('oauth')) {
$this->replaceInFile('// Features::oauth(),', 'Features::oauth(),', config_path('jetstream.php'));
$this->replaceInFile('sanctum', 'web', config_path('jetstream.php'));
}

// Configure Email Verification...
if ($this->option('verification')) {
$this->replaceInFile('// Features::emailVerification(),', 'Features::emailVerification(),', config_path('fortify.php'));
Expand Down Expand Up @@ -156,6 +163,7 @@ protected function installLivewireStack()

$this->call('install:api', [
'--without-migration-prompt' => true,
'--passport' => $this->option('oauth'),
]);

// Update Configuration...
Expand Down Expand Up @@ -189,6 +197,10 @@ protected function installLivewireStack()
(new Filesystem)->ensureDirectoryExists(resource_path('views/layouts'));
(new Filesystem)->ensureDirectoryExists(resource_path('views/profile'));

if ($this->option('oauth')) {
(new Filesystem)->ensureDirectoryExists(app_path('Actions/Passport'));
}

(new Filesystem)->deleteDirectory(resource_path('sass'));

// Terms Of Service / Privacy Policy...
Expand All @@ -208,6 +220,10 @@ protected function installLivewireStack()
// Models...
copy(__DIR__.'/../../stubs/app/Models/User.php', app_path('Models/User.php'));

if ($this->option('oauth')) {
$this->replaceInFile('Laravel\Sanctum\HasApiTokens', 'Laravel\Passport\HasApiTokens', app_path('Models/User.php'));
}

// Factories...
copy(__DIR__.'/../../database/factories/UserFactory.php', base_path('database/factories/UserFactory.php'));

Expand All @@ -216,6 +232,11 @@ protected function installLivewireStack()
copy(__DIR__.'/../../stubs/app/Actions/Fortify/UpdateUserProfileInformation.php', app_path('Actions/Fortify/UpdateUserProfileInformation.php'));
copy(__DIR__.'/../../stubs/app/Actions/Jetstream/DeleteUser.php', app_path('Actions/Jetstream/DeleteUser.php'));

if ($this->option('oauth')) {
copy(__DIR__.'/../../stubs/app/Actions/Passport/CreateClient.php', app_path('Actions/Passport/CreateClient.php'));
copy(__DIR__.'/../../stubs/app/Actions/Passport/UpdateClient.php', app_path('Actions/Passport/UpdateClient.php'));
}

// Components...
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/livewire/resources/views/components', resource_path('views/components'));

Expand All @@ -233,9 +254,15 @@ protected function installLivewireStack()
copy(__DIR__.'/../../stubs/livewire/resources/views/policy.blade.php', resource_path('views/policy.blade.php'));

// Other Views...
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/livewire/resources/views/api', resource_path('views/api'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/livewire/resources/views/profile', resource_path('views/profile'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/livewire/resources/views/auth', resource_path('views/auth'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/livewire/resources/views/oauth', resource_path('views/oauth'));

if ($this->option('oauth')) {
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/livewire/resources/views/passport-api', resource_path('views/api'));
} else {
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/livewire/resources/views/api', resource_path('views/api'));
}

if (! Str::contains(file_get_contents(base_path('routes/web.php')), "'/dashboard'")) {
(new Filesystem)->append(base_path('routes/web.php'), $this->livewireRouteDefinition());
Expand Down Expand Up @@ -322,7 +349,7 @@ protected function livewireRouteDefinition()
return <<<'EOF'

Route::middleware([
'auth:sanctum',
config('jetstream.guard') ? 'auth:'.config('jetstream.guard') : 'auth',
config('jetstream.auth_session'),
'verified',
])->group(function () {
Expand All @@ -348,6 +375,7 @@ protected function installInertiaStack()

$this->call('install:api', [
'--without-migration-prompt' => true,
'--passport' => $this->option('oauth'),
]);

// Install NPM packages...
Expand Down Expand Up @@ -385,6 +413,10 @@ protected function installInertiaStack()
(new Filesystem)->ensureDirectoryExists(resource_path('views'));
(new Filesystem)->ensureDirectoryExists(resource_path('markdown'));

if ($this->option('oauth')) {
(new Filesystem)->ensureDirectoryExists(app_path('Actions/Passport'));
}

(new Filesystem)->deleteDirectory(resource_path('sass'));

// Terms Of Service / Privacy Policy...
Expand All @@ -411,6 +443,10 @@ protected function installInertiaStack()
// Models...
copy(__DIR__.'/../../stubs/app/Models/User.php', app_path('Models/User.php'));

if ($this->option('oauth')) {
$this->replaceInFile('Laravel\Sanctum\HasApiTokens', 'Laravel\Passport\HasApiTokens', app_path('Models/User.php'));
}

// Factories...
copy(__DIR__.'/../../database/factories/UserFactory.php', base_path('database/factories/UserFactory.php'));

Expand All @@ -419,6 +455,11 @@ protected function installInertiaStack()
copy(__DIR__.'/../../stubs/app/Actions/Fortify/UpdateUserProfileInformation.php', app_path('Actions/Fortify/UpdateUserProfileInformation.php'));
copy(__DIR__.'/../../stubs/app/Actions/Jetstream/DeleteUser.php', app_path('Actions/Jetstream/DeleteUser.php'));

if ($this->option('oauth')) {
copy(__DIR__.'/../../stubs/app/Actions/Passport/CreateClient.php', app_path('Actions/Passport/CreateClient.php'));
copy(__DIR__.'/../../stubs/app/Actions/Passport/UpdateClient.php', app_path('Actions/Passport/UpdateClient.php'));
}

// Blade Views...
copy(__DIR__.'/../../stubs/inertia/resources/views/app.blade.php', resource_path('views/app.blade.php'));

Expand All @@ -434,9 +475,15 @@ protected function installInertiaStack()

(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Components', resource_path('js/Components'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Layouts', resource_path('js/Layouts'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Pages/API', resource_path('js/Pages/API'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Pages/Auth', resource_path('js/Pages/Auth'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Pages/Profile', resource_path('js/Pages/Profile'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Pages/OAuth', resource_path('js/Pages/OAuth'));

if ($this->option('oauth')) {
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Pages/PassportAPI', resource_path('js/Pages/API'));
} else {
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/inertia/resources/js/Pages/API', resource_path('js/Pages/API'));
}

copy(__DIR__.'/../../stubs/inertia/routes/web.php', base_path('routes/web.php'));

Expand Down Expand Up @@ -543,6 +590,10 @@ protected function ensureApplicationIsTeamCompatible()
copy(__DIR__.'/../../stubs/app/Models/TeamInvitation.php', app_path('Models/TeamInvitation.php'));
copy(__DIR__.'/../../stubs/app/Models/UserWithTeams.php', app_path('Models/User.php'));

if ($this->option('oauth')) {
$this->replaceInFile('Laravel\Sanctum\HasApiTokens', 'Laravel\Passport\HasApiTokens', app_path('Models/User.php'));
}

// Actions...
copy(__DIR__.'/../../stubs/app/Actions/Jetstream/AddTeamMember.php', app_path('Actions/Jetstream/AddTeamMember.php'));
copy(__DIR__.'/../../stubs/app/Actions/Jetstream/CreateTeam.php', app_path('Actions/Jetstream/CreateTeam.php'));
Expand Down Expand Up @@ -860,6 +911,7 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp
options: collect([
'teams' => 'Team support',
'api' => 'API support',
'oauth' => 'OAuth support via Laravel Passport',
'verification' => 'Email verification',
'dark' => 'Dark mode',
])->when(
Expand Down
11 changes: 11 additions & 0 deletions src/Contracts/CreatesOAuthClients.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Laravel\Jetstream\Contracts;

/**
* @method \Laravel\Passport\Client create(\Illuminate\Foundation\Auth\User $user, array $input)
*/
interface CreatesOAuthClients
{
//
}
11 changes: 11 additions & 0 deletions src/Contracts/UpdatesOAuthClients.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Laravel\Jetstream\Contracts;

/**
* @method void update(\Illuminate\Foundation\Auth\User $user, \Laravel\Passport\Client $client, array $input)
*/
interface UpdatesOAuthClients
{
//
}
20 changes: 20 additions & 0 deletions src/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public static function hasApiFeatures()
return static::enabled(static::api());
}

/**
* Determine if the application is using any OAuth features.
*
* @return bool
*/
public static function hasOAuthFeatures()
{
return static::enabled(static::oauth());
}

/**
* Determine if the application is using any team features.
*
Expand Down Expand Up @@ -108,6 +118,16 @@ public static function api()
return 'api';
}

/**
* Enable the OAuth feature.
*
* @return string
*/
public static function oauth()
{
return 'oauth';
}

/**
* Enable the teams feature.
*
Expand Down
6 changes: 4 additions & 2 deletions src/HasTeams.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Laravel\Jetstream;

use Illuminate\Support\Str;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Passport\HasApiTokens as PassportHasApiTokens;
use Laravel\Sanctum\HasApiTokens as SanctumHasApiTokens;

trait HasTeams
{
Expand Down Expand Up @@ -207,7 +208,8 @@ public function hasTeamPermission($team, string $permission)
return false;
}

if (in_array(HasApiTokens::class, class_uses_recursive($this)) &&
if ((in_array(SanctumHasApiTokens::class, $traits = class_uses_recursive($this)) ||
in_array(PassportHasApiTokens::class, $traits)) &&
! $this->tokenCan($permission) &&
$this->currentAccessToken() !== null) {
return false;
Expand Down
Loading
Loading