From b5ce9dc1e8e8a86c7c6d95da7c77437c3620ebb0 Mon Sep 17 00:00:00 2001 From: Vincent QUATREVIEUX Date: Fri, 4 Aug 2023 09:33:50 +0200 Subject: [PATCH] doc: Add readme --- README.md | 79 ++++++++++++++++++++++++++++++++++++++++++++++ src/JwtEncoder.php | 6 ++-- 2 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..c7a15b8 --- /dev/null +++ b/README.md @@ -0,0 +1,79 @@ +# JWT +[![build](https://github.com/b2pweb/jwt/actions/workflows/php.yml/badge.svg)](https://github.com/b2pweb/jwt/actions/workflows/php.yml) +[![Packagist Version](https://img.shields.io/packagist/v/b2pweb/jwt.svg)](https://packagist.org/packages/b2pweb/jwt) +[![Total Downloads](https://img.shields.io/packagist/dt/b2pweb/jwt.svg)](https://packagist.org/packages/b2pweb/jwt) +[![Type Coverage](https://shepherd.dev/github/b2pweb/jwt/coverage.svg)](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); +``` diff --git a/src/JwtEncoder.php b/src/JwtEncoder.php index 369b09c..1b43621 100644 --- a/src/JwtEncoder.php +++ b/src/JwtEncoder.php @@ -60,11 +60,11 @@ public function jwa(): JWA */ public function supportedAlgorithms(array $algorithms): self { - $decoder = clone $this; + $encoder = clone $this; - $decoder->jwa = $decoder->jwa->filter($algorithms); + $encoder->jwa = $encoder->jwa->filter($algorithms); - return $decoder; + return $encoder; } /**