@@ -23,16 +23,16 @@ php env does not have libsodium installed:
2323composer require paragonie/sodium_compat
2424```
2525
26- Example
27- -------
26+ ## Example
27+
2828``` php
2929use Firebase\JWT\JWT;
3030use Firebase\JWT\Key;
3131
32- $key = 'example_key ';
32+ $key = 'example_key_of_sufficient_length ';
3333$payload = [
34- 'iss' => 'http:// example.org',
35- 'aud' => 'http:// example.com',
34+ 'iss' => 'example.org',
35+ 'aud' => 'example.com',
3636 'iat' => 1356999524,
3737 'nbf' => 1357000000
3838];
@@ -69,8 +69,9 @@ $decoded_array = (array) $decoded;
6969JWT::$leeway = 60; // $leeway in seconds
7070$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
7171```
72- Example encode/decode headers
73- -------
72+
73+ ## Example encode/decode headers
74+
7475Decoding the JWT headers without verifying the JWT first is NOT recommended, and is not supported by
7576this library. This is because without verifying the JWT, the header values could have been tampered with.
7677Any value pulled from an unverified header should be treated as if it could be any string sent in from an
@@ -80,10 +81,10 @@ header part:
8081``` php
8182use Firebase\JWT\JWT;
8283
83- $key = 'example_key ';
84+ $key = 'example_key_of_sufficient_length ';
8485$payload = [
85- 'iss' => 'http:// example.org',
86- 'aud' => 'http:// example.com',
86+ 'iss' => 'example.org',
87+ 'aud' => 'example.com',
8788 'iat' => 1356999524,
8889 'nbf' => 1357000000
8990];
@@ -103,8 +104,9 @@ $decoded = json_decode(base64_decode($headersB64), true);
103104
104105print_r($decoded);
105106```
106- Example with RS256 (openssl)
107- ----------------------------
107+
108+ ## Example with RS256 (openssl)
109+
108110``` php
109111use Firebase\JWT\JWT;
110112use Firebase\JWT\Key;
@@ -172,8 +174,7 @@ $decoded_array = (array) $decoded;
172174echo "Decode:\n" . print_r($decoded_array, true) . "\n";
173175```
174176
175- Example with a passphrase
176- -------------------------
177+ ## Example with a passphrase
177178
178179``` php
179180use Firebase\JWT\JWT;
@@ -209,8 +210,8 @@ $decoded = JWT::decode($jwt, new Key($publicKey, 'RS256'));
209210echo "Decode:\n" . print_r((array) $decoded, true) . "\n";
210211```
211212
212- Example with EdDSA (libsodium and Ed25519 signature)
213- ----------------------------
213+ ## Example with EdDSA (libsodium and Ed25519 signature)
214+
214215``` php
215216use Firebase\JWT\JWT;
216217use Firebase\JWT\Key;
@@ -238,21 +239,21 @@ echo "Encode:\n" . print_r($jwt, true) . "\n";
238239
239240$decoded = JWT::decode($jwt, new Key($publicKey, 'EdDSA'));
240241echo "Decode:\n" . print_r((array) $decoded, true) . "\n";
241- ````
242+ ```
243+
244+ ## Example with multiple keys
242245
243- Example with multiple keys
244- --------------------------
245246``` php
246247use Firebase\JWT\JWT;
247248use Firebase\JWT\Key;
248249
249250// Example RSA keys from previous example
250- // $privateKey1 = '...';
251- // $publicKey1 = '...';
251+ // $privateRsKey = '...';
252+ // $publicRsKey = '...';
252253
253254// Example EdDSA keys from previous example
254- // $privateKey2 = '...';
255- // $publicKey2 = '...';
255+ // $privateEcKey = '...';
256+ // $publicEcKey = '...';
256257
257258$payload = [
258259 'iss' => 'example.org',
@@ -261,14 +262,14 @@ $payload = [
261262 'nbf' => 1357000000
262263];
263264
264- $jwt1 = JWT::encode($payload, $privateKey1 , 'RS256', 'kid1');
265- $jwt2 = JWT::encode($payload, $privateKey2 , 'EdDSA', 'kid2');
265+ $jwt1 = JWT::encode($payload, $privateRsKey , 'RS256', 'kid1');
266+ $jwt2 = JWT::encode($payload, $privateEcKey , 'EdDSA', 'kid2');
266267echo "Encode 1:\n" . print_r($jwt1, true) . "\n";
267268echo "Encode 2:\n" . print_r($jwt2, true) . "\n";
268269
269270$keys = [
270- 'kid1' => new Key($publicKey1 , 'RS256'),
271- 'kid2' => new Key($publicKey2 , 'EdDSA'),
271+ 'kid1' => new Key($publicRsKey , 'RS256'),
272+ 'kid2' => new Key($publicEcKey , 'EdDSA'),
272273];
273274
274275$decoded1 = JWT::decode($jwt1, $keys);
@@ -278,8 +279,7 @@ echo "Decode 1:\n" . print_r((array) $decoded1, true) . "\n";
278279echo "Decode 2:\n" . print_r((array) $decoded2, true) . "\n";
279280```
280281
281- Using JWKs
282- ----------
282+ ## Using JWKs
283283
284284``` php
285285use Firebase\JWT\JWK;
@@ -291,11 +291,11 @@ $jwks = ['keys' => []];
291291
292292// JWK::parseKeySet($jwks) returns an associative array of **kid** to Firebase\JWT\Key
293293// objects. Pass this as the second parameter to JWT::decode.
294- JWT::decode($jwt, JWK::parseKeySet($jwks));
294+ $decoded = JWT::decode($jwt, JWK::parseKeySet($jwks));
295+ print_r($decoded);
295296```
296297
297- Using Cached Key Sets
298- ---------------------
298+ ## Using Cached Key Sets
299299
300300The ` CachedKeySet ` class can be used to fetch and cache JWKS (JSON Web Key Sets) from a public URI.
301301This has the following advantages:
@@ -315,7 +315,7 @@ $jwksUri = 'https://www.gstatic.com/iap/verify/public_key-jwk';
315315$httpClient = new GuzzleHttp\Client();
316316
317317// Create an HTTP request factory (can be any PSR-17 compatible HTTP request factory)
318- $httpFactory = new GuzzleHttp\Psr \HttpFactory();
318+ $httpFactory = new GuzzleHttp\Psr7 \HttpFactory();
319319
320320// Create a cache item pool (can be any PSR-6 compatible cache item pool)
321321$cacheItemPool = Phpfastcache\CacheManager::getInstance('files');
@@ -406,8 +406,8 @@ Tests
406406Run the tests using phpunit:
407407
408408``` bash
409- $ pear install PHPUnit
410- $ phpunit --configuration phpunit.xml.dist
409+ $ composer update
410+ $ vendor/bin/ phpunit -c phpunit.xml.dist
411411PHPUnit 3.7.10 by Sebastian Bergmann.
412412.....
413413Time: 0 seconds, Memory: 2.50Mb
0 commit comments