|
2 | 2 | [](https://packagist.org/packages/miladrahimi/php-jwt) |
3 | 3 | [](https://github.com/miladrahimi/php-jwt/actions/workflows/ci.yml) |
4 | 4 | [](https://codecov.io/gh/miladrahimi/php-jwt) |
| 5 | +[](https://scrutinizer-ci.com/g/miladrahimi/php-jwt/?branch=master) |
5 | 6 | [](https://packagist.org/packages/miladrahimi/php-jwt) |
6 | 7 |
|
7 | 8 | # PHP-JWT |
@@ -178,7 +179,7 @@ $parser = new Parser($signer, $validator); |
178 | 179 |
|
179 | 180 | try { |
180 | 181 | $claims = $parser->parse($jwt); |
181 | | - echo $claims; // ['id' => 13, 'is-admin' => true] |
| 182 | + print_r($claims); // ['id' => 13, 'is-admin' => true] |
182 | 183 | } catch (ValidationException $e) { |
183 | 184 | // Handle error. |
184 | 185 | } |
@@ -208,7 +209,7 @@ You can access the built-in Rules within the `MiladRahimi\Jwt\Validator\Rules` n |
208 | 209 | * [OlderThan](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/OlderThan.php) |
209 | 210 | * [OlderThanOrSame](https://github.com/miladrahimi/php-jwt/blob/master/src/Validator/Rules/OlderThanOrSame.php) |
210 | 211 |
|
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. |
212 | 213 |
|
213 | 214 | #### Required and Optional Rules |
214 | 215 |
|
@@ -251,30 +252,73 @@ class Even implements Rule |
251 | 252 | } |
252 | 253 | ``` |
253 | 254 |
|
| 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 | + |
254 | 310 | ### Error Handling |
255 | 311 |
|
256 | 312 | 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. |
278 | 322 |
|
279 | 323 | ## License |
280 | 324 | PHP-JWT is initially created by [Milad Rahimi](http://miladrahimi.com) |
|
0 commit comments