n3xt0r/laravel-passport-authorization-core
Core components for building OAuth2 authorization flows with Laravel Passport. Provides reusable helpers and abstractions to standardize consent/approval handling and authorization logic, making it easier to implement custom Passport authorization endpoints and UI.
Installation
composer require n3xt0r/laravel-passport-authorization-core:^1.3.0
php artisan passport:install
php artisan vendor:publish --provider="N3xt0r\PassportAuthorizationCore\PassportAuthorizationCoreServiceProvider" --tag="migrations"
php artisan migrate
Configure Passport
Ensure HasApiTokens is added to your User model and Passport is bootstrapped in AuthServiceProvider.
Note: This package now supports Laravel 13.
Define a Resource
Create a resource class (e.g., app/Models/Post.php):
use N3xt0r\PassportAuthorizationCore\Contracts\AuthorizableResource;
class Post implements AuthorizableResource
{
public static function getAuthorizableActions(): array
{
return ['view', 'edit', 'delete'];
}
}
First Use Case: Scope-Based Authorization In a controller, enforce scope checks:
use N3xt0r\PassportAuthorizationCore\Facades\PassportAuthorization;
public function show(Post $post)
{
PassportAuthorization::authorize($post, 'view');
}
Resource-Action Mapping
getAuthorizableActions() for each resource.PassportAuthorization::authorize($resource, $action) in controllers/middleware.Dynamic Scopes Attach scopes to user roles dynamically:
// In a User model observer or service
$user->scopes()->attach('post:view');
Middleware Integration Protect routes globally:
Route::middleware(['auth:api', 'scope'])->group(function () {
// Routes requiring at least one scope
});
Policy Integration Combine with Laravel’s native policies:
public function update(Request $request, Post $post)
{
$this->authorize('update', $post);
PassportAuthorization::authorize($post, 'edit');
}
config/passport-authorization.php:
'scopes' => [
'post:view' => ['resource' => Post::class, 'action' => 'view'],
],
JsonResource to include scope metadata:
public function toArray($request)
{
return [
'id' => $this->id,
'actions' => PassportAuthorization::getAuthorizedActions($this, $request->user()),
];
}
AuthServiceProvider and User model are updated for Laravel 13 conventions if applicable.Scope Mismatch Errors
PassportAuthorization::authorize() fails silently or throws AuthorizationException.PassportAuthorization::getAuthorizedActions($resource, $user);
to debug allowed actions.Caching Conflicts
oauth_scopes table may not reflect real-time changes.php artisan passport:clear-scopes-cache
Resource Not Authorizable
Class 'App\Models\Post' does not implement AuthorizableResource.AuthorizableResource and define getAuthorizableActions().Laravel 13 Migration Issues
AuthServiceProvider and ensure the PassportAuthorizationCoreServiceProvider is registered correctly. If using Laravel 13's new authentication system, verify compatibility by checking the package's documentation or GitHub issues.Log Scopes: Enable debug mode in config:
'debug' => env('PASSPORT_AUTH_DEBUG', false),
Check Laravel logs for scope resolution details.
Test with Tinker:
php artisan tinker
>>> $user = App\Models\User::first();
>>> $post = App\Models\Post::first();
>>> \N3xt0r\PassportAuthorizationCore\Facades\PassportAuthorization::getAuthorizedActions($post, $user);
Custom Authorizers Override the default authorizer:
PassportAuthorization::setAuthorizer(new CustomAuthorizer());
Event Listeners Listen for scope attachment/detachment:
// In EventServiceProvider
protected $listen = [
'N3xt0r\PassportAuthorizationCore\Events\ScopeAttached' => [
\App\Listeners\LogScopeChange::class,
],
];
Database Schema
Extend the oauth_scopes table for custom metadata:
Schema::table('oauth_scopes', function (Blueprint $table) {
$table->string('metadata')->nullable();
});
Laravel 13 Customization
PassportAuthorizationCoreServiceProvider, ensure it is bootstrapped correctly in config/app.php under the providers array.HasApiTokens trait is correctly applied to your User model.How can I help you explore Laravel packages today?