stoorm01's avatar

Laravel Passport Client middleware logic

I'm trying to create an auth server, so the auth will be centralized across systems and I don't have to implement it all over again in every project. I built the passport server using Jetstream (I need 2fa) and Passport packages. I created a client laravel app (resource server), what will have a Vue3 FE. The whole login process works perfectly (PKCE) i receive the access token.

There's only thing I just can't seem to figure it out: let's say my client laravel app is an invoicing microservice, after the login i want to protect the client microservice endpoints so i can't just make a request with a random token in header, and receive for example a list off invoices.

How can i prevent that? As far is i know my only way to be sure about the token is by using a middleware that will ask the auth server about the token's validity, but this can cause perfomance issues as the users grow, since everytime a route is protected we will make a request to the auth server...

What is the correct middleware logic for the client to protect it's resources after login?

0 likes
1 reply
LaryAI's avatar
Level 58

To protect your client microservice endpoints using Laravel Passport, you can implement a middleware that verifies the validity of the access token. Here's a step-by-step guide on how to achieve this:

  1. Use Passport's Token Middleware: Laravel Passport provides a middleware called auth:api that you can use to protect your routes. This middleware will automatically check the validity of the access token against the Passport server.

  2. Set Up Middleware in Your Routes: Apply the auth:api middleware to the routes you want to protect. This will ensure that only requests with a valid access token can access these routes.

    Route::middleware('auth:api')->group(function () {
        Route::get('/invoices', 'InvoiceController@index');
        // Add other protected routes here
    });
    
  3. Token Caching (Optional): To mitigate performance issues due to frequent token validation requests to the auth server, you can implement token caching. This involves storing the token validation result temporarily and reusing it for subsequent requests within a short time frame.

    Here's a basic example of how you might implement token caching:

    In this example, the token validity is cached for 60 seconds. Adjust the caching duration based on your application's needs.

  4. Register Middleware: Register your custom middleware in the app/Http/Kernel.php file if you choose to implement token caching or any custom logic.

    protected $routeMiddleware = [
        // Other middleware
        'check.token' => \App\Http\Middleware\CheckTokenMiddleware::class,
    ];
    
  5. Apply Custom Middleware: If you created a custom middleware, apply it to your routes:

    Route::middleware('check.token')->group(function () {
        Route::get('/invoices', 'InvoiceController@index');
        // Add other protected routes here
    });
    

By following these steps, you can ensure that your client microservice endpoints are protected and only accessible with valid access tokens, while also optimizing performance with token caching.

Please or to participate in this conversation.