https://github.com/b2pweb/jwt
Simple library for parse JWT token
https://github.com/b2pweb/jwt
Last synced: 10 months ago
JSON representation
Simple library for parse JWT token
- Host: GitHub
- URL: https://github.com/b2pweb/jwt
- Owner: b2pweb
- License: mit
- Created: 2023-08-01T14:44:52.000Z (almost 3 years ago)
- Default Branch: 1.1
- Last Pushed: 2025-06-17T13:22:40.000Z (about 1 year ago)
- Last Synced: 2025-07-20T11:47:52.718Z (11 months ago)
- Language: PHP
- Size: 38.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JWT
[](https://github.com/b2pweb/jwt/actions/workflows/php.yml)
[](https://packagist.org/packages/b2pweb/jwt)
[](https://packagist.org/packages/b2pweb/jwt)
[](https://shepherd.dev/github/b2pweb/jwt)
Library for parse and create JWT (JSON Web Token) in PHP, using [PHP JWT Framework](https://github.com/web-token/jwt-framework).
## Installation
Install with composer :
```bash
composer require b2pweb/jwt
```
## Simple usage
```php
filter(['HS256', 'HS512', 'RS256', 'RS512']); // Filter enabled algorithms
// Define your keys
$jwks = new \Jose\Component\Core\JWKSet([
\Jose\Component\KeyManagement\JWKFactory::createFromKeyFile($privKey, null, ['use' => 'sig', 'kid' => 'key-user']),
// ...
]);
// Encode a payload to JWT
$encoder = new \B2pweb\Jwt\JwtEncoder($jwa);
$jwt = $encoder->encode(
[
'iss' => 'https://example.com',
'aud' => 'https://example.com',
'iat' => time(),
'exp' => time() + 3600,
'sub' => '1234567890',
'name' => 'John Doe',
'admin' => true,
],
// You can configure encoding options here, like the key to use, the algorithm, ...
(new \B2pweb\Jwt\EncodingOptions($jwks))
->setAlgorithm('RS512')
->setKid('key-user')
);
// You can also use an object that implements \B2pweb\Jwt\ClaimsInterface
// allowing you to customize the claims serialization to JSON
// If you extends \B2pweb\Jwt\Claims, you can define Claims::$encodingFlags on the subclass to customize the JSON encoding flags
$claims = new \B2pweb\Jwt\Claims([
'iss' => 'https://example.com',
'aud' => 'https://example.com',
'iat' => time(),
'exp' => time() + 3600,
'sub' => '1234567890',
'name' => 'John Doe',
'admin' => true,
]);
$jwt = $encoder->encode(
$claims,
// You can use EncodingOptions::fromKey, which will automatically set the algorithm and the kid from the given key
\B2pweb\Jwt\EncodingOptions::fromKey(\Jose\Component\KeyManagement\JWKFactory::createFromSecret($secret, ['use' => 'sig', 'alg' => 'HS256']))
);
// Decode a JWT
$decoder = new \B2pweb\Jwt\JwtDecoder($jwa);
$token = $decoder->decode($jwt, $jwks); // Return a \B2pweb\Jwt\Claims object
$token->claim('iss'); // Return 'https://example.com'
// Yan can also define allowed algorithms using JwtDecoder::supportedAlgorithms()
$token = $decoder->supportedAlgorithms(['RS256', 'RS512'])->decode($jwt, $jwks);
// You can also decode a JWT without verifying the signature
$token = \B2pweb\Jwt\JWT::fromJwtUnsafe($jwt);
```