|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Firebase\JWT; |
| 4 | + |
| 5 | +use ArrayAccess; |
| 6 | +use LogicException; |
| 7 | +use OutOfBoundsException; |
| 8 | +use Psr\Cache\CacheItemInterface; |
| 9 | +use Psr\Cache\CacheItemPoolInterface; |
| 10 | +use Psr\Http\Client\ClientInterface; |
| 11 | +use Psr\Http\Message\RequestFactoryInterface; |
| 12 | +use RuntimeException; |
| 13 | + |
| 14 | +/** |
| 15 | + * @implements ArrayAccess<string, Key> |
| 16 | + */ |
| 17 | +class CachedKeySet implements ArrayAccess |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + private $jwksUri; |
| 23 | + /** |
| 24 | + * @var ClientInterface |
| 25 | + */ |
| 26 | + private $httpClient; |
| 27 | + /** |
| 28 | + * @var RequestFactoryInterface |
| 29 | + */ |
| 30 | + private $httpFactory; |
| 31 | + /** |
| 32 | + * @var CacheItemPoolInterface |
| 33 | + */ |
| 34 | + private $cache; |
| 35 | + /** |
| 36 | + * @var ?int |
| 37 | + */ |
| 38 | + private $expiresAfter; |
| 39 | + /** |
| 40 | + * @var ?CacheItemInterface |
| 41 | + */ |
| 42 | + private $cacheItem; |
| 43 | + /** |
| 44 | + * @var array<string, Key> |
| 45 | + */ |
| 46 | + private $keySet; |
| 47 | + /** |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + private $cacheKey; |
| 51 | + /** |
| 52 | + * @var string |
| 53 | + */ |
| 54 | + private $cacheKeyPrefix = 'jwks'; |
| 55 | + /** |
| 56 | + * @var int |
| 57 | + */ |
| 58 | + private $maxKeyLength = 64; |
| 59 | + /** |
| 60 | + * @var bool |
| 61 | + */ |
| 62 | + private $rateLimit; |
| 63 | + /** |
| 64 | + * @var string |
| 65 | + */ |
| 66 | + private $rateLimitCacheKey; |
| 67 | + /** |
| 68 | + * @var int |
| 69 | + */ |
| 70 | + private $maxCallsPerMinute = 10; |
| 71 | + |
| 72 | + public function __construct( |
| 73 | + string $jwksUri, |
| 74 | + ClientInterface $httpClient, |
| 75 | + RequestFactoryInterface $httpFactory, |
| 76 | + CacheItemPoolInterface $cache, |
| 77 | + int $expiresAfter = null, |
| 78 | + bool $rateLimit = false |
| 79 | + ) { |
| 80 | + $this->jwksUri = $jwksUri; |
| 81 | + $this->httpClient = $httpClient; |
| 82 | + $this->httpFactory = $httpFactory; |
| 83 | + $this->cache = $cache; |
| 84 | + $this->expiresAfter = $expiresAfter; |
| 85 | + $this->rateLimit = $rateLimit; |
| 86 | + $this->setCacheKeys(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param string $keyId |
| 91 | + * @return Key |
| 92 | + */ |
| 93 | + public function offsetGet($keyId): Key |
| 94 | + { |
| 95 | + if (!$this->keyIdExists($keyId)) { |
| 96 | + throw new OutOfBoundsException('Key ID not found'); |
| 97 | + } |
| 98 | + return $this->keySet[$keyId]; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param string $keyId |
| 103 | + * @return bool |
| 104 | + */ |
| 105 | + public function offsetExists($keyId): bool |
| 106 | + { |
| 107 | + return $this->keyIdExists($keyId); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param string $offset |
| 112 | + * @param Key $value |
| 113 | + */ |
| 114 | + public function offsetSet($offset, $value): void |
| 115 | + { |
| 116 | + throw new LogicException('Method not implemented'); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @param string $offset |
| 121 | + */ |
| 122 | + public function offsetUnset($offset): void |
| 123 | + { |
| 124 | + throw new LogicException('Method not implemented'); |
| 125 | + } |
| 126 | + |
| 127 | + private function keyIdExists(string $keyId): bool |
| 128 | + { |
| 129 | + $keySetToCache = null; |
| 130 | + if (null === $this->keySet) { |
| 131 | + $item = $this->getCacheItem(); |
| 132 | + // Try to load keys from cache |
| 133 | + if ($item->isHit()) { |
| 134 | + // item found! Return it |
| 135 | + $this->keySet = $item->get(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (!isset($this->keySet[$keyId])) { |
| 140 | + if ($this->rateLimitExceeded()) { |
| 141 | + return false; |
| 142 | + } |
| 143 | + $request = $this->httpFactory->createRequest('get', $this->jwksUri); |
| 144 | + $jwksResponse = $this->httpClient->sendRequest($request); |
| 145 | + $jwks = json_decode((string) $jwksResponse->getBody(), true); |
| 146 | + $this->keySet = $keySetToCache = JWK::parseKeySet($jwks); |
| 147 | + |
| 148 | + if (!isset($this->keySet[$keyId])) { |
| 149 | + return false; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if ($keySetToCache) { |
| 154 | + $item = $this->getCacheItem(); |
| 155 | + $item->set($keySetToCache); |
| 156 | + if ($this->expiresAfter) { |
| 157 | + $item->expiresAfter($this->expiresAfter); |
| 158 | + } |
| 159 | + $this->cache->save($item); |
| 160 | + } |
| 161 | + |
| 162 | + return true; |
| 163 | + } |
| 164 | + |
| 165 | + private function rateLimitExceeded(): bool |
| 166 | + { |
| 167 | + if (!$this->rateLimit) { |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + $cacheItem = $this->cache->getItem($this->rateLimitCacheKey); |
| 172 | + if (!$cacheItem->isHit()) { |
| 173 | + $cacheItem->expiresAfter(1); // # of calls are cached each minute |
| 174 | + } |
| 175 | + |
| 176 | + $callsPerMinute = (int) $cacheItem->get(); |
| 177 | + if (++$callsPerMinute > $this->maxCallsPerMinute) { |
| 178 | + return true; |
| 179 | + } |
| 180 | + $cacheItem->set($callsPerMinute); |
| 181 | + $this->cache->save($cacheItem); |
| 182 | + return false; |
| 183 | + } |
| 184 | + |
| 185 | + private function getCacheItem(): CacheItemInterface |
| 186 | + { |
| 187 | + if (\is_null($this->cacheItem)) { |
| 188 | + $this->cacheItem = $this->cache->getItem($this->cacheKey); |
| 189 | + } |
| 190 | + |
| 191 | + return $this->cacheItem; |
| 192 | + } |
| 193 | + |
| 194 | + private function setCacheKeys(): void |
| 195 | + { |
| 196 | + if (empty($this->jwksUri)) { |
| 197 | + throw new RuntimeException('JWKS URI is empty'); |
| 198 | + } |
| 199 | + |
| 200 | + // ensure we do not have illegal characters |
| 201 | + $key = preg_replace('|[^a-zA-Z0-9_\.!]|', '', $this->jwksUri); |
| 202 | + |
| 203 | + // add prefix |
| 204 | + $key = $this->cacheKeyPrefix . $key; |
| 205 | + |
| 206 | + // Hash keys if they exceed $maxKeyLength of 64 |
| 207 | + if (\strlen($key) > $this->maxKeyLength) { |
| 208 | + $key = substr(hash('sha256', $key), 0, $this->maxKeyLength); |
| 209 | + } |
| 210 | + |
| 211 | + $this->cacheKey = $key; |
| 212 | + |
| 213 | + if ($this->rateLimit) { |
| 214 | + // add prefix |
| 215 | + $rateLimitKey = $this->cacheKeyPrefix . 'ratelimit' . $key; |
| 216 | + |
| 217 | + // Hash keys if they exceed $maxKeyLength of 64 |
| 218 | + if (\strlen($rateLimitKey) > $this->maxKeyLength) { |
| 219 | + $rateLimitKey = substr(hash('sha256', $rateLimitKey), 0, $this->maxKeyLength); |
| 220 | + } |
| 221 | + |
| 222 | + $this->rateLimitCacheKey = $rateLimitKey; |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments