emiljimenez21 / laravel-jwt-auth
A JWT client for applications behind an OIDC or OAuth2.0 identity provider
Requires
- php: ^8.0
- firebase/php-jwt: ^6.10
- illuminate/support: ^7.20|^8.19|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.0
README
This package introduces a JWT based authentication mechanism into your laravel application. It is designed for SPA's that use an OpenID Connect (OIDC) or OAuth 2.0 identity provider with public PKCE-enabled clients.
Installation
You can install the package via composer:
composer require emiljimenez21/laravel-jwt-auth
Basic usage
Step 1: Place the public key you use to sign your JWTs in the /storage
directory with the filename oauth_public.key
Step 2: Add the HasJWT
trait to your user model
use EmilJimenez21\LaravelJWTAuth\Traits\HasJWT; class User extends Authenticatable { use HasJWT; }
Step 3: Update your api guard in /config/auth.php
to use the jwt
driver
'guards' => [ 'api' => [ 'driver' => 'jwt', 'provider' => 'users' ] ]
Step 4: Specify the user resolver in AppServiceProvider
boot method. Do this when you don't have the user stored in the application.
The user resolver enables your application to use the bearer token to make requests to the IDP so you can create and populate the user if they don't exist.
NOTE: This feature is intended for applications that use a public PKCE flow to generate access tokens on the client from the Idp.
public function boot(): void { /** * This code will only be ran when the jwt subject does not exist in this system. * * The user resolver expects a callable that accepts a ?string $bearerToken and a * ?User response. It provides a great way for your application to quickly create * users that aren't in the system yet. * */ JWT::setUserResolver(function ($bearerToken) { // Call the IDP and get the user profile data $response = Http::withHeader('Authorization', "Bearer $bearerToken") ->get('http://localhost:8000/api/user'); // Retrieve the response body $contents = $response->getBody()->getContents(); // Convert the json response to an array $userData = json_decode($contents, true); // Return a new or existing user return User::query()->firstOrCreate([ 'id' => $userData['data']['id'] ]); }); }
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.