← All changes
|
includes/sdk/google/firebase/php-jwt/src/CachedKeySet.php
+27
-25
1.2.11
→
1.4.1
View file →
| @@ -1,16 +1,16 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace Dudlewebs\WPMCS\Firebase\JWT; | |
| 3 | +namespace Dudlewebs\WPMCS\GCP\Firebase\JWT; | |
| 4 | 4 | |
| 5 | 5 | use ArrayAccess; |
| 6 | 6 | use InvalidArgumentException; |
| 7 | 7 | use LogicException; |
| 8 | 8 | use OutOfBoundsException; |
| 9 | -use Dudlewebs\WPMCS\Psr\Cache\CacheItemInterface; | |
| 10 | -use Dudlewebs\WPMCS\Psr\Cache\CacheItemPoolInterface; | |
| 11 | -use Dudlewebs\WPMCS\Psr\Http\Client\ClientInterface; | |
| 12 | -use Dudlewebs\WPMCS\Psr\Http\Message\RequestFactoryInterface; | |
| 9 | +use Dudlewebs\WPMCS\GCP\Psr\Cache\CacheItemInterface; | |
| 10 | +use Dudlewebs\WPMCS\GCP\Psr\Cache\CacheItemPoolInterface; | |
| 11 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Client\ClientInterface; | |
| 12 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\RequestFactoryInterface; | |
| 13 | 13 | use RuntimeException; |
| 14 | 14 | use UnexpectedValueException; |
| 15 | 15 | /** |
| 16 | 16 | * @implements ArrayAccess<string, Key> |
| @@ -72,9 +72,9 @@ | ||
| 72 | 72 | /** |
| 73 | 73 | * @var string|null |
| 74 | 74 | */ |
| 75 | 75 | private $defaultAlg; |
| 76 | - public function __construct(string $jwksUri, ClientInterface $httpClient, RequestFactoryInterface $httpFactory, CacheItemPoolInterface $cache, int $expiresAfter = null, bool $rateLimit = \false, string $defaultAlg = null) | |
| 76 | + public function __construct(string $jwksUri, ClientInterface $httpClient, RequestFactoryInterface $httpFactory, CacheItemPoolInterface $cache, ?int $expiresAfter = null, bool $rateLimit = \false, ?string $defaultAlg = null) | |
| 77 | 77 | { |
| 78 | 78 | $this->jwksUri = $jwksUri; |
| 79 | 79 | $this->httpClient = $httpClient; |
| 80 | 80 | $this->httpFactory = $httpFactory; |
| @@ -87,9 +87,9 @@ | ||
| 87 | 87 | /** |
| 88 | 88 | * @param string $keyId |
| 89 | 89 | * @return Key |
| 90 | 90 | */ |
| 91 | - public function offsetGet($keyId): Key | |
| 91 | + public function offsetGet($keyId) : Key | |
| 92 | 92 | { |
| 93 | 93 | if (!$this->keyIdExists($keyId)) { |
| 94 | 94 | throw new OutOfBoundsException('Key ID not found'); |
| 95 | 95 | } |
| @@ -98,9 +98,9 @@ | ||
| 98 | 98 | /** |
| 99 | 99 | * @param string $keyId |
| 100 | 100 | * @return bool |
| 101 | 101 | */ |
| 102 | - public function offsetExists($keyId): bool | |
| 102 | + public function offsetExists($keyId) : bool | |
| 103 | 103 | { |
| 104 | 104 | return $this->keyIdExists($keyId); |
| 105 | 105 | } |
| 106 | 106 | /** |
| @@ -106,9 +106,9 @@ | ||
| 106 | 106 | /** |
| 107 | 107 | * @param string $offset |
| 108 | 108 | * @param Key $value |
| 109 | 109 | */ |
| 110 | - public function offsetSet($offset, $value): void | |
| 110 | + public function offsetSet($offset, $value) : void | |
| 111 | 111 | { |
| 112 | 112 | throw new LogicException('Method not implemented'); |
| 113 | 113 | } |
| 114 | 114 | /** |
| @@ -113,9 +113,9 @@ | ||
| 113 | 113 | } |
| 114 | 114 | /** |
| 115 | 115 | * @param string $offset |
| 116 | 116 | */ |
| 117 | - public function offsetUnset($offset): void | |
| 117 | + public function offsetUnset($offset) : void | |
| 118 | 118 | { |
| 119 | 119 | throw new LogicException('Method not implemented'); |
| 120 | 120 | } |
| 121 | 121 | /** |
| @@ -120,11 +120,11 @@ | ||
| 120 | 120 | } |
| 121 | 121 | /** |
| 122 | 122 | * @return array<mixed> |
| 123 | 123 | */ |
| 124 | - private function formatJwksForCache(string $jwks): array | |
| 124 | + private function formatJwksForCache(string $jwks) : array | |
| 125 | 125 | { |
| 126 | - $jwks = json_decode($jwks, \true); | |
| 126 | + $jwks = \json_decode($jwks, \true); | |
| 127 | 127 | if (!isset($jwks['keys'])) { |
| 128 | 128 | throw new UnexpectedValueException('"keys" member must exist in the JWK Set'); |
| 129 | 129 | } |
| 130 | 130 | if (empty($jwks['keys'])) { |
| @@ -136,9 +136,9 @@ | ||
| 136 | 136 | $keys[(string) $kid] = $v; |
| 137 | 137 | } |
| 138 | 138 | return $keys; |
| 139 | 139 | } |
| 140 | - private function keyIdExists(string $keyId): bool | |
| 140 | + private function keyIdExists(string $keyId) : bool | |
| 141 | 141 | { |
| 142 | 142 | if (null === $this->keySet) { |
| 143 | 143 | $item = $this->getCacheItem(); |
| 144 | 144 | // Try to load keys from cache |
| @@ -158,9 +158,9 @@ | ||
| 158 | 158 | } |
| 159 | 159 | $request = $this->httpFactory->createRequest('GET', $this->jwksUri); |
| 160 | 160 | $jwksResponse = $this->httpClient->sendRequest($request); |
| 161 | 161 | if ($jwksResponse->getStatusCode() !== 200) { |
| 162 | - throw new UnexpectedValueException(sprintf('HTTP Error: %d %s for URI "%s"', $jwksResponse->getStatusCode(), $jwksResponse->getReasonPhrase(), $this->jwksUri), $jwksResponse->getStatusCode()); | |
| 162 | + throw new UnexpectedValueException(\sprintf('HTTP Error: %d %s for URI "%s"', $jwksResponse->getStatusCode(), $jwksResponse->getReasonPhrase(), $this->jwksUri), $jwksResponse->getStatusCode()); | |
| 163 | 163 | } |
| 164 | 164 | $this->keySet = $this->formatJwksForCache((string) $jwksResponse->getBody()); |
| 165 | 165 | if (!isset($this->keySet[$keyId])) { |
| 166 | 166 | return \false; |
| @@ -173,27 +173,29 @@ | ||
| 173 | 173 | $this->cache->save($item); |
| 174 | 174 | } |
| 175 | 175 | return \true; |
| 176 | 176 | } |
| 177 | - private function rateLimitExceeded(): bool | |
| 177 | + private function rateLimitExceeded() : bool | |
| 178 | 178 | { |
| 179 | 179 | if (!$this->rateLimit) { |
| 180 | 180 | return \false; |
| 181 | 181 | } |
| 182 | 182 | $cacheItem = $this->cache->getItem($this->rateLimitCacheKey); |
| 183 | - if (!$cacheItem->isHit()) { | |
| 184 | - $cacheItem->expiresAfter(1); | |
| 185 | - // # of calls are cached each minute | |
| 183 | + $cacheItemData = []; | |
| 184 | + if ($cacheItem->isHit() && \is_array($data = $cacheItem->get())) { | |
| 185 | + $cacheItemData = $data; | |
| 186 | 186 | } |
| 187 | - $callsPerMinute = (int) $cacheItem->get(); | |
| 187 | + $callsPerMinute = $cacheItemData['callsPerMinute'] ?? 0; | |
| 188 | + $expiry = $cacheItemData['expiry'] ?? new \DateTime('+60 seconds', new \DateTimeZone('UTC')); | |
| 188 | 189 | if (++$callsPerMinute > $this->maxCallsPerMinute) { |
| 189 | 190 | return \true; |
| 190 | 191 | } |
| 191 | - $cacheItem->set($callsPerMinute); | |
| 192 | + $cacheItem->set(['expiry' => $expiry, 'callsPerMinute' => $callsPerMinute]); | |
| 193 | + $cacheItem->expiresAt($expiry); | |
| 192 | 194 | $this->cache->save($cacheItem); |
| 193 | 195 | return \false; |
| 194 | 196 | } |
| 195 | - private function getCacheItem(): CacheItemInterface | |
| 197 | + private function getCacheItem() : CacheItemInterface | |
| 196 | 198 | { |
| 197 | 199 | if (\is_null($this->cacheItem)) { |
| 198 | 200 | $this->cacheItem = $this->cache->getItem($this->cacheKey); |
| 199 | 201 | } |
| @@ -198,20 +200,20 @@ | ||
| 198 | 200 | $this->cacheItem = $this->cache->getItem($this->cacheKey); |
| 199 | 201 | } |
| 200 | 202 | return $this->cacheItem; |
| 201 | 203 | } |
| 202 | - private function setCacheKeys(): void | |
| 204 | + private function setCacheKeys() : void | |
| 203 | 205 | { |
| 204 | 206 | if (empty($this->jwksUri)) { |
| 205 | 207 | throw new RuntimeException('JWKS URI is empty'); |
| 206 | 208 | } |
| 207 | 209 | // ensure we do not have illegal characters |
| 208 | - $key = preg_replace('|[^a-zA-Z0-9_\.!]|', '', $this->jwksUri); | |
| 210 | + $key = \preg_replace('|[^a-zA-Z0-9_\\.!]|', '', $this->jwksUri); | |
| 209 | 211 | // add prefix |
| 210 | 212 | $key = $this->cacheKeyPrefix . $key; |
| 211 | 213 | // Hash keys if they exceed $maxKeyLength of 64 |
| 212 | 214 | if (\strlen($key) > $this->maxKeyLength) { |
| 213 | - $key = substr(hash('sha256', $key), 0, $this->maxKeyLength); | |
| 215 | + $key = \substr(\hash('sha256', $key), 0, $this->maxKeyLength); | |
| 214 | 216 | } |
| 215 | 217 | $this->cacheKey = $key; |
| 216 | 218 | if ($this->rateLimit) { |
| 217 | 219 | // add prefix |
| @@ -217,9 +219,9 @@ | ||
| 217 | 219 | // add prefix |
| 218 | 220 | $rateLimitKey = $this->cacheKeyPrefix . 'ratelimit' . $key; |
| 219 | 221 | // Hash keys if they exceed $maxKeyLength of 64 |
| 220 | 222 | if (\strlen($rateLimitKey) > $this->maxKeyLength) { |
| 221 | - $rateLimitKey = substr(hash('sha256', $rateLimitKey), 0, $this->maxKeyLength); | |
| 223 | + $rateLimitKey = \substr(\hash('sha256', $rateLimitKey), 0, $this->maxKeyLength); | |
| 222 | 224 | } |
| 223 | 225 | $this->rateLimitCacheKey = $rateLimitKey; |
| 224 | 226 | } |
| 225 | 227 | } |