Skip to content

Commit ea7a3c7

Browse files
authored
Update README.md
1 parent 0013c6a commit ea7a3c7

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ $parser = new Parser($signer, $validator);
179179

180180
try {
181181
$claims = $parser->parse($jwt);
182-
echo $claims; // ['id' => 13, 'is-admin' => true]
182+
print_r($claims); // ['id' => 13, 'is-admin' => true]
183183
} catch (ValidationException $e) {
184184
// Handle error.
185185
}
@@ -252,6 +252,61 @@ class Even implements Rule
252252
}
253253
```
254254

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+
// JWT 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+
// JWT 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); // ['id' => 13, 'is-admin' => true]
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+
255310
### Error Handling
256311

257312
Here are the exceptions that the package might throw:

0 commit comments

Comments
 (0)