I can't use scopes as arrays with the passport client middleware

43 Views Asked by At

I can only use it like this:

Route::middleware('client:admin,view')->group(function () {

But this way the client needs to have all the scopes

I'm trying this way but it doesn't work:

Route::group(['prefix'=>'logs','as'=>'logs.','middleware' => 'client'], function(){

        Route::middleware(['scope:admin,store'])->group(function () {
            Route::post('/', [LogController::class, 'store'])->name('store');
        });

        Route::middleware(['scope:admin,view'])->group(function () {
            Route::get('/', [LogController::class, 'index'])->name('all');
            Route::get('/date/{date}', [LogController::class, 'findByDate'])->name('date');
            Route::get('/table/{table}/{id_table}', [LogController::class, 'findByTableAndId'])->name('table.id');
            Route::get('/action/{action}', [LogController::class, 'findByAction'])->name('action');
            Route::get('/message', [LogController::class, 'findByMessage'])->name('message');
            Route::get('/error', [LogController::class, 'findByError'])->name('error');
            Route::get('/locations', [LogsLocationController::class, 'index'])->name('locations');
        });
    });

always returns as unauthorized.

class AppServiceProvider:

/**
     
Bootstrap any application services.*/
  public function boot(): void{Passport::hashClientSecrets();Passport::tokensExpireIn(now()->addDays(15));Passport::refreshTokensExpireIn(now()->addDays(30));Passport::tokensCan(['admin' => 'Administrator','store' => 'Store logs','put' => 'Update logs','delete' => 'Delete logs','view' => 'View logs',]);}

class Kernel:

protected $middlewareAliases = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
        'precognitive' => \Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class,
        'signed' => \App\Http\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        'client' => CheckClientCredentials::class,
        'scopes' => \Laravel\Passport\Http\Middleware\CheckScopes::class,
        'scope' => \Laravel\Passport\Http\Middleware\CheckForAnyScope::class,
    ];

auth.php:

'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],

 'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
    ], 

I need the client to have that or another scope, not both.

1

There are 1 best solutions below

0
On

The issue you're encountering when using arrays to assign middlewares is likely that you're using a single string for all middlewares, rather than one per middleware.

So try changing: Route::middleware(['scope:admin,store']) to Route::middleware(['scope:admin', 'store'])

The difference being 'scope:admin,store' vs. 'scope:admin', 'store'