Adding custom middleware to Passar Passport endpoints

I have a standard Passara Passport setting of 5.4 - everything works fine and generates tokens.

I protect the API routes using the auth: api middleware, as well as a custom middleware that checks that certain headers in the request are present and valid before any requests are processed. This middleware works great for the API route group.

Is there a way to wrap Passport routes created by laravel '... / oauth / token' in this middleware?

I have currently configured the routes in my AuthServiceProvider.php boot () method:

public function boot()
{
    $this->registerPolicies();

    // Passport/OAuth
    Passport::routes(function ($router) {
      $router->forAccessTokens();
      $router->forTransientTokens();
    });

    Passport::tokensExpireIn(Carbon::now()->addDays(7));

    Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
}

, oauth , .

+7
3

Passport, /oauth/token, :

  • , php artisan r:l
  • , , , AccessTokenController@issueToken
  • , AccessTokenController,
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Laravel\Passport\Http\Controllers\AccessTokenController;

class ApiTokenController extends AccessTokenController
{

}
  1. ( ):

Route::middleware('MyMiddleware')->post('/api-token', 'ApiTokenController@issueToken');

+3

: app/Providers/AuthServiceProvider function boot(). . - Passport::routes(). routes() . Passport.

Passport::routes(null, ['middleware' => 'api']);
+8

app/Providers/AuthServiceProvider , - :

use Illuminate\Support\Facades\Route;

boot() Passport:: routes() Route:: group() :

Route::group(['middleware'=>'MyFunkyCustomMiddleware'], function(){
    Passport::routes(); // <-- Replace this with your own version
});

, !

+3

Source: https://habr.com/ru/post/1682983/


All Articles