Skip to content

Commit a0f8008

Browse files
authored
Update README.md
1 parent 351f0cf commit a0f8008

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,42 @@ $jwks = ['keys' => []];
203203
JWT::decode($payload, JWK::parseKeySet($jwks));
204204
```
205205

206+
Using Cached Key Sets
207+
---------------------
208+
209+
The `CachedKeySet` class can be used to fetch JWK keys from a public URI. This has
210+
the following advantages:
211+
212+
1. The results are cached for performance
213+
2. If an unrecognized key is requested, the cache is refreshed, to accomodate for key rotation.
214+
215+
```php
216+
use Firebase\JWT\CachedKeySet;
217+
use Firebase\JWT\JWT;
218+
219+
// The URI for the JWK keys you wish to cash the results from
220+
$jwkUri = 'https://www.gstatic.com/iap/verify/public_key-jwk';
221+
222+
// Create an HTTP client (can be any PSR-7 compatible HTTP client)
223+
$httpClient = new GuzzleHttp\Client();
224+
225+
// Create an HTTP request factory (can be any PSR-17 compatible HTTP request factory)
226+
$httpFactory = new GuzzleHttp\Psr\HttpFactory();
227+
228+
// Create a cache item pool (can be any PSR-6 compatible cache item pool)
229+
$cacheItemPool = Phpfastcache\CacheManager::getInstance('files');
230+
231+
$keySet = new CachedKeySet(
232+
$jwkUri,
233+
$httpClient,
234+
$httpFactory,
235+
$cacheItemPool
236+
);
237+
238+
$decoded = JWT::decode($payload, $keySet);
239+
```
240+
241+
206242
Miscellaneous
207243
-------------
208244

0 commit comments

Comments
 (0)