diff --git a/src/CachedKeySet.php b/src/CachedKeySet.php index f1580c92..e2215b30 100644 --- a/src/CachedKeySet.php +++ b/src/CachedKeySet.php @@ -132,13 +132,13 @@ public function offsetUnset($offset): void private function keyIdExists(string $keyId): bool { - $keySetToCache = null; if (null === $this->keySet) { $item = $this->getCacheItem(); // Try to load keys from cache if ($item->isHit()) { // item found! Return it - $this->keySet = $item->get(); + $jwks = $item->get(); + $this->keySet = JWK::parseKeySet(json_decode($jwks, true), $this->defaultAlg); } } @@ -148,17 +148,15 @@ private function keyIdExists(string $keyId): bool } $request = $this->httpFactory->createRequest('get', $this->jwksUri); $jwksResponse = $this->httpClient->sendRequest($request); - $jwks = json_decode((string) $jwksResponse->getBody(), true); - $this->keySet = $keySetToCache = JWK::parseKeySet($jwks, $this->defaultAlg); + $jwks = (string) $jwksResponse->getBody(); + $this->keySet = JWK::parseKeySet(json_decode($jwks, true), $this->defaultAlg); if (!isset($this->keySet[$keyId])) { return false; } - } - if ($keySetToCache) { $item = $this->getCacheItem(); - $item->set($keySetToCache); + $item->set($jwks); if ($this->expiresAfter) { $item->expiresAfter($this->expiresAfter); } diff --git a/tests/CachedKeySetTest.php b/tests/CachedKeySetTest.php index a13c80fd..9e884e6b 100644 --- a/tests/CachedKeySetTest.php +++ b/tests/CachedKeySetTest.php @@ -117,7 +117,7 @@ public function testKeyIdIsCached() $cacheItem->isHit() ->willReturn(true); $cacheItem->get() - ->willReturn(JWK::parseKeySet(json_decode($this->testJwks1, true))); + ->willReturn($this->testJwks1); $cache = $this->prophesize(CacheItemPoolInterface::class); $cache->getItem($this->testJwksUriKey) @@ -143,7 +143,7 @@ public function testCachedKeyIdRefresh() ->willReturn(true); $cacheItem->get() ->shouldBeCalledOnce() - ->willReturn(JWK::parseKeySet(json_decode($this->testJwks1, true))); + ->willReturn($this->testJwks1); $cacheItem->set(Argument::any()) ->shouldBeCalledOnce() ->will(function () { @@ -217,9 +217,8 @@ public function testJwtVerify() $cacheItem->isHit() ->willReturn(true); $cacheItem->get() - ->willReturn(JWK::parseKeySet( - json_decode(file_get_contents(__DIR__ . '/data/rsa-jwkset.json'), true) - )); + ->willReturn(file_get_contents(__DIR__ . '/data/rsa-jwkset.json') + ); $cache = $this->prophesize(CacheItemPoolInterface::class); $cache->getItem($this->testJwksUriKey)