36 private CacheItemPoolInterface
$cache;
80 ClientInterface $httpClient,
81 RequestFactoryInterface $httpFactory,
82 CacheItemPoolInterface $cache,
83 int $expiresAfter = null,
84 bool $rateLimit =
false,
85 string $defaultAlg = null
106 return JWK::parseKey($this->keySet[$keyId], $this->defaultAlg);
140 $jwks = json_decode($jwks,
true);
142 if (!isset($jwks[
'keys'])) {
146 if (empty($jwks[
'keys'])) {
151 foreach ($jwks[
'keys'] as $k => $v) {
152 $kid = isset($v[
'kid']) ? $v[
'kid'] : $k;
161 if (null === $this->keySet) {
164 if ($item->isHit()) {
166 $this->keySet = $item->get();
169 if (\is_string($this->keySet)) {
175 if (!isset($this->keySet[$keyId])) {
179 $request = $this->httpFactory->createRequest(
'GET', $this->jwksUri);
180 $jwksResponse = $this->httpClient->sendRequest($request);
181 if ($jwksResponse->getStatusCode() !== 200) {
184 'HTTP Error: %d %s for URI "%s"',
185 $jwksResponse->getStatusCode(),
186 $jwksResponse->getReasonPhrase(),
189 $jwksResponse->getStatusCode()
194 if (!isset($this->keySet[$keyId])) {
199 $item->set($this->keySet);
200 if ($this->expiresAfter) {
201 $item->expiresAfter($this->expiresAfter);
203 $this->cache->save($item);
211 if (!$this->rateLimit) {
215 $cacheItem = $this->cache->getItem($this->rateLimitCacheKey);
216 if (!$cacheItem->isHit()) {
217 $cacheItem->expiresAfter(1);
220 $callsPerMinute = (
int) $cacheItem->get();
221 if (++$callsPerMinute > $this->maxCallsPerMinute) {
224 $cacheItem->set($callsPerMinute);
225 $this->cache->save($cacheItem);
231 if (\is_null($this->cacheItem)) {
232 $this->cacheItem = $this->cache->getItem($this->cacheKey);
240 if (empty($this->jwksUri)) {
245 $key = preg_replace(
'|[^a-zA-Z0-9_\.!]|',
'', $this->jwksUri);
248 $key = $this->cacheKeyPrefix .
$key;
251 if (\strlen(
$key) > $this->maxKeyLength) {
252 $key = substr(
hash(
'sha256',
$key), 0, $this->maxKeyLength);
255 $this->cacheKey =
$key;
257 if ($this->rateLimit) {
259 $rateLimitKey = $this->cacheKeyPrefix .
'ratelimit' .
$key;
262 if (\strlen($rateLimitKey) > $this->maxKeyLength) {
263 $rateLimitKey = substr(
hash(
'sha256', $rateLimitKey), 0, $this->maxKeyLength);
266 $this->rateLimitCacheKey = $rateLimitKey;
static parseKey(array $jwk, string $defaultAlg=null)
Parse a JWK key.
string $rateLimitCacheKey
CacheItemInterface $cacheItem
RequestFactoryInterface $httpFactory
formatJwksForCache(string $jwks)
keyIdExists(string $keyId)
__construct(string $jwksUri, ClientInterface $httpClient, RequestFactoryInterface $httpFactory, CacheItemPoolInterface $cache, int $expiresAfter=null, bool $rateLimit=false, string $defaultAlg=null)
offsetSet($offset, $value)
CacheItemPoolInterface $cache
ClientInterface $httpClient