Simple JWT Builder and Parser

v1.0.0 2022-03-16 20:56 UTC

This package is auto-updated.

Last update: 2025-07-18 17:51:19 UTC


README

A simple library to encode and decode JWT tokens in PHP (conforming to RFC 7519)

Dependencies

Installation

composer require lostinvlg/jwt

Creating jwt

use Lostinvlg\Jwt\Algorithm;
use Lostinvlg\Jwt\Jwt;
use Lostinvlg\Jwt\Key;

$jwtId = '1dA8dDQ5lE';
$time = time();
$jwt = new Jwt();

$token = $jwt
    ->getBuilder()
    ->setKey(new Key('YOUR_SECRET_KEY_STRING', Algorithm::HS256))
    ->setIssuedBy('https://example.com')
    ->setAudience('https://example.com')
    ->setIssuedAt($time)
    ->setNotBefore($time + 10)
    ->setExpiresAt($time + 3600)
    ->setIdentifiedBy($jwtId)
    ->setClaim('user_id', 1)
    ->setClaim('role_id', 'admin')
    ->getToken();

$encoded = (string) $token; // contains jwt encoded string

$token->getClaim('user_id'); // equals 1
$token->getClaim('role_id'); // equals "admin"
$token->getClaim('exp'); // returns expires timestamp

Parsing from string

use Lostinvlg\Jwt\Algorithm;
use Lostinvlg\Jwt\Jwt;
use Lostinvlg\Jwt\Key;

$jwt = new Jwt();
$token = $jwt->getParser(new Key('YOUR_SECRET_KEY_STRING', Algorithm::HS256))->parse($encoded);

$token->getClaim('user_id'); // equals 1
$token->getClaim('role_id'); // equals "admin"
$token->getClaim('exp'); // returns expires timestamp

License

MIT