Skip to content

Commit 607dcd4

Browse files
fix: cache jwks as string in CachedKeySet (#435)
1 parent c68f2a7 commit 607dcd4

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

src/CachedKeySet.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ public function offsetUnset($offset): void
132132

133133
private function keyIdExists(string $keyId): bool
134134
{
135-
$keySetToCache = null;
136135
if (null === $this->keySet) {
137136
$item = $this->getCacheItem();
138137
// Try to load keys from cache
139138
if ($item->isHit()) {
140139
// item found! Return it
141-
$this->keySet = $item->get();
140+
$jwks = $item->get();
141+
$this->keySet = JWK::parseKeySet(json_decode($jwks, true), $this->defaultAlg);
142142
}
143143
}
144144

@@ -148,17 +148,15 @@ private function keyIdExists(string $keyId): bool
148148
}
149149
$request = $this->httpFactory->createRequest('get', $this->jwksUri);
150150
$jwksResponse = $this->httpClient->sendRequest($request);
151-
$jwks = json_decode((string) $jwksResponse->getBody(), true);
152-
$this->keySet = $keySetToCache = JWK::parseKeySet($jwks, $this->defaultAlg);
151+
$jwks = (string) $jwksResponse->getBody();
152+
$this->keySet = JWK::parseKeySet(json_decode($jwks, true), $this->defaultAlg);
153153

154154
if (!isset($this->keySet[$keyId])) {
155155
return false;
156156
}
157-
}
158157

159-
if ($keySetToCache) {
160158
$item = $this->getCacheItem();
161-
$item->set($keySetToCache);
159+
$item->set($jwks);
162160
if ($this->expiresAfter) {
163161
$item->expiresAfter($this->expiresAfter);
164162
}

tests/CachedKeySetTest.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function testKeyIdIsCached()
117117
$cacheItem->isHit()
118118
->willReturn(true);
119119
$cacheItem->get()
120-
->willReturn(JWK::parseKeySet(json_decode($this->testJwks1, true)));
120+
->willReturn($this->testJwks1);
121121

122122
$cache = $this->prophesize(CacheItemPoolInterface::class);
123123
$cache->getItem($this->testJwksUriKey)
@@ -143,7 +143,7 @@ public function testCachedKeyIdRefresh()
143143
->willReturn(true);
144144
$cacheItem->get()
145145
->shouldBeCalledOnce()
146-
->willReturn(JWK::parseKeySet(json_decode($this->testJwks1, true)));
146+
->willReturn($this->testJwks1);
147147
$cacheItem->set(Argument::any())
148148
->shouldBeCalledOnce()
149149
->will(function () {
@@ -217,9 +217,8 @@ public function testJwtVerify()
217217
$cacheItem->isHit()
218218
->willReturn(true);
219219
$cacheItem->get()
220-
->willReturn(JWK::parseKeySet(
221-
json_decode(file_get_contents(__DIR__ . '/data/rsa-jwkset.json'), true)
222-
));
220+
->willReturn(file_get_contents(__DIR__ . '/data/rsa-jwkset.json')
221+
);
223222

224223
$cache = $this->prophesize(CacheItemPoolInterface::class);
225224
$cache->getItem($this->testJwksUriKey)

0 commit comments

Comments
 (0)