Skip to content

Commit cd49971

Browse files
committed
Merge branch 'master' of github.com:miladrahimi/php-jwt
2 parents 629d6a8 + c4de2a0 commit cd49971

File tree

1 file changed

+67
-23
lines changed

1 file changed

+67
-23
lines changed

README.md

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
[![Total Downloads](https://poser.pugx.org/miladrahimi/php-jwt/downloads)](https://packagist.org/packages/miladrahimi/php-jwt)
33
[![Build](https://github.com/miladrahimi/php-jwt/actions/workflows/ci.yml/badge.svg)](https://github.com/miladrahimi/php-jwt/actions/workflows/ci.yml)
44
[![codecov](https://codecov.io/gh/miladrahimi/php-jwt/graph/badge.svg?token=KctrYUweFd)](https://codecov.io/gh/miladrahimi/php-jwt)
5+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/miladrahimi/php-jwt/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/miladrahimi/php-jwt/?branch=master)
56
[![License](https://poser.pugx.org/miladrahimi/php-jwt/license)](https://packagist.org/packages/miladrahimi/php-jwt)
67

78
# PHP-JWT
@@ -178,7 +179,7 @@ $parser = new Parser($signer, $validator);
178179

179180
try {
180181
$claims = $parser->parse($jwt);
181-
echo $claims; // ['id' => 13, 'is-admin' => true]
182+
print_r($claims); // ['id' => 13, 'is-admin' => true]
182183
} catch (ValidationException $e) {
183184
// Handle error.
184185
}
@@ -208,7 +209,7 @@ You can access the built-in Rules within the `MiladRahimi\Jwt\Validator\Rules` n
208209
* [OlderThan](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/OlderThan.php)
209210
* [OlderThanOrSame](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/OlderThanOrSame.php)
210211

211-
Descriptions for each Rule can be found within their respective class doc-blocks.
212+
Descriptions for each Rule can be found within their respective class doc blocks.
212213

213214
#### Required and Optional Rules
214215

@@ -251,30 +252,73 @@ class Even implements Rule
251252
}
252253
```
253254

255+
### KID Header
256+
257+
The `kid` parameter within the JWT header plays a crucial role in managing multiple keys efficiently.
258+
By leveraging the "kid" header, you can assign a unique key identifier (kid) to each key that you use to sign JWTs.
259+
This enables seamless verification of JWTs by associating them with their respective key identifiers (kid).
260+
Check out this example:
261+
262+
```php
263+
use MiladRahimi\Jwt\Cryptography\Algorithms\Ecdsa\ES384Signer;
264+
use MiladRahimi\Jwt\Cryptography\Algorithms\Ecdsa\ES384Verifier;
265+
use MiladRahimi\Jwt\Cryptography\Algorithms\Rsa\RS256Signer;
266+
use MiladRahimi\Jwt\Cryptography\Algorithms\Rsa\RS256Verifier;
267+
use MiladRahimi\Jwt\Cryptography\Keys\EcdsaPrivateKey;
268+
use MiladRahimi\Jwt\Cryptography\Keys\EcdsaPublicKey;
269+
use MiladRahimi\Jwt\Cryptography\Keys\RsaPrivateKey;
270+
use MiladRahimi\Jwt\Cryptography\Keys\RsaPublicKey;
271+
use MiladRahimi\Jwt\Generator;
272+
use MiladRahimi\Jwt\Parser;
273+
274+
$privateKey1 = new RsaPrivateKey('/path/to/rsa-private.pem', '', 'key-1');
275+
$publicKey1 = new RsaPublicKey('/path/to/rsa-public.pem', 'key-1');
276+
277+
$privateKey2 = new EcdsaPrivateKey('/path/to/ecdsa384-private.pem', '', 'key-2');
278+
$publicKey2 = new EcdsaPublicKey('/path/to/ecdsa384-public.pem', 'key-2');
279+
280+
// Generate tokens
281+
282+
$signer1 = new RS256Signer($privateKey1);
283+
$generator1 = new Generator($signer1);
284+
$jwt1 = $generator1->generate(['id' => 13, 'is-admin' => true]);
285+
// $jwt1 header: {"alg": "RS256", "typ": "JWT", "kid": "key-1"}
286+
287+
$signer2 = new ES384Signer($privateKey2);
288+
$generator2 = new Generator($signer2);
289+
$jwt2 = $generator2->generate(['id' => 13, 'is-admin' => true]);
290+
// $jwt2 header: {"alg": "ES384", "typ": "JWT", "kid": "key-2"}
291+
292+
// Parse tokens
293+
294+
$verifierFactory = new VerifierFactory([
295+
new RS256Verifier($publicKey1),
296+
new ES384Verifier($publicKey2),
297+
]);
298+
299+
$verifier1 = $verifierFactory->getVerifier($jwt1); // instance of RS256Verifier
300+
$parser1 = new Parser($verifier1);
301+
$claims = $parser1->parse($jwt1);
302+
print_r($claims); // ['id' => 13, 'is-admin' => true]
303+
304+
$verifier2 = $verifierFactory->getVerifier($jwt2); // instance of ES384Verifier
305+
$parser2 = new Parser($verifier2);
306+
$claims = $parser2->parse($jwt2);
307+
print_r($claims); // ['id' => 13, 'is-admin' => true]
308+
```
309+
254310
### Error Handling
255311

256312
Here are the exceptions that the package might throw:
257-
* `InvalidKeyException`:
258-
* By `Generator` and `Parser` methods.
259-
* When the provided key is not valid.
260-
* `InvalidSignatureException`:
261-
* By `Parser::parse()`, `Parser::verify()`, and `Parser::validate()` methods.
262-
* When the JWT signature is not valid.
263-
* `InvalidTokenException`:
264-
* By `Parser::parse()`, `Parser::verify()`, and `Parser::validate()` methods.
265-
* When the JWT format is not valid (for example it has no payload).
266-
* `JsonDecodingException`:
267-
* By `Parser::parse()` and `Parser::validate()` methods.
268-
* When the JSON extracted from JWT is not valid.
269-
* `JsonEncodingException`:
270-
* By `Generator::generate()` method.
271-
* When cannot convert the provided claims to JSON.
272-
* `SigningException`:
273-
* By `Generator::generate()` method.
274-
* When cannot sign the token using the provided signer or key.
275-
* `ValidationException`:
276-
* By `Parser::parse()` and `Parser::validate()` methods.
277-
* When one of the validation rules fails.
313+
* `InvalidKeyException` when the provided key is not valid (encoding).
314+
* `InvalidSignatureException` when the JWT signature is not valid (decoding).
315+
* `InvalidTokenException` when the JWT format is not valid, for example, it has no payload (decoding).
316+
* `JsonDecodingException` when the JSON extracted from JWT is not valid (decoding).
317+
* `JsonEncodingException` when cannot convert the provided claims to JSON (encoding).
318+
* `SigningException` when cannot sign the token using the provided signer or key (encoding).
319+
* `ValidationException` when one of the validation rules fails (decoding).
320+
* `NoKidException` by `VerifierFactory` when there is no `kid` in the token header.
321+
* `VerifierNotFoundException` by `VerifierFactory` when no key/verifier matches the `kid` in the token header.
278322

279323
## License
280324
PHP-JWT is initially created by [Milad Rahimi](http://miladrahimi.com)

0 commit comments

Comments
 (0)