baywa-re-lusy/jwt-authentication

BayWa r.e. LUSY JWT Authentication

2.2.0 2023-10-24 13:29 UTC

This package is auto-updated.

Last update: 2024-03-24 14:16:32 UTC


README

CircleCI

Installation

To install the JWT Authentication service, you will need Composer in your project:

composer require baywa-re-lusy/jwt-authentication

Usage example code for Laminas projects

In Module.php:

use BayWaReLusy\JwtAuthentication\TokenService;
use Laminas\Cache\Psr\CacheItemPool\CacheItemPoolDecorator;
use BayWaReLusy\JwtAuthentication\Token;

public function onAuthentication(MvcAuthEvent $e): IdentityInterface
{
    $jwt                          = ...; // The JSON Web Token
    $laminasCacheStorageInterface = ...; // Instance of Laminas\Cache\Storage\StorageInterface
    $jwksUrl                      = ...; // URL from where to get JWKs
    $cache                        = new CacheItemPoolDecorator($laminasCacheStorageInterface);
    
    $tokenService = new TokenService();
    
    try {
        $token = $tokenService->validateToken($jwt, $cache, $jwksUrl);
    } catch (\BayWaReLusy\JwtAuthentication\InvalidTokenException $e) {
        return new GuestIdentity();
    }
    
    ...
}