Skip to content

Plopster-Software-Development/jwt-auth-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JWT Auth Library (PHP)

A modular and extensible JWT authentication library built on top of firebase/php-jwt.
It provides a clean structure for generating, validating, refreshing, and customizing tokens with support for custom payloads and claim validation.


Installation

composer require yourname/jwt-auth

Usage Example

use JwtAuth\Config\JwtConfig;
use JwtAuth\Claims\DefaultClaimsProvider;
use JwtAuth\Keys\LocalKeyProvider;
use JwtAuth\Services\TokenGenerator;
use JwtAuth\Services\TokenValidator;
use JwtAuth\Services\TokenRefresher;
use JwtAuth\Validation\DefaultClaimValidator;
use JwtAuth\Core\JwtManager;

$config = new JwtConfig(
    issuer: 'my-app',
    audience: 'my-users',
    algorithm: 'RS256',
    privateKeyPath: __DIR__ . '/keys/private.pem',
    publicKeyPath: __DIR__ . '/keys/public.pem',
    ttl: 3600
);

$keyProvider = new LocalKeyProvider($config->privateKeyPath, $config->publicKeyPath);
$claimsProvider = new DefaultClaimsProvider($config->issuer, $config->audience, $config->ttl);
$claimValidator = new DefaultClaimValidator();

$generator = new TokenGenerator($config, $claimsProvider, $keyProvider);
$validator = new TokenValidator($config, $keyProvider);
$refresher = new TokenRefresher($generator);

$jwtManager = new JwtManager($generator, $validator);

// Generate a token
$token = $jwtManager->createToken(['user_id' => 42]);

// Validate the token
$payload = $jwtManager->verifyToken($token);

// Refresh the token
$newToken = $refresher->refresh($payload);

Features

  • Generate JWT tokens with default or custom claims

  • Validate tokens and decode payloads securely

  • Refresh tokens using existing payload data

  • Extend or replace any component:

    • ClaimsProviderInterface for custom payload logic
    • ClaimValidatorInterface for custom validation rules
    • TokenPayloadInterface for custom payload objects
    • KeyProviderInterface for flexible key management (file, env, KMS, etc.)

Project Structure

src/
 ├── Claims/
 ├── Config/
 ├── Contracts/
 ├── Core/
 ├── Exceptions/
 ├── Keys/
 ├── Services/
 └── Validation/

Each folder is modular and follows the single-responsibility principle, making the library scalable and easy to maintain.


License

Proprietary License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages