Skip to content

Commit 9051308

Browse files
committed
add more tests
1 parent dc6e704 commit 9051308

File tree

2 files changed

+95
-50
lines changed

2 files changed

+95
-50
lines changed

src/JWT.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ private static function getKey($keyOrKeyArray, $kid = null)
394394
return $keyOrKeyArray;
395395
}
396396

397+
if ($keyOrKeyArray instanceof CachedKeySet) {
398+
return $keyOrKeyArray[$kid];
399+
}
400+
397401
if (is_array($keyOrKeyArray) || $keyOrKeyArray instanceof ArrayAccess) {
398402
foreach ($keyOrKeyArray as $keyId => $key) {
399403
if (!$key instanceof Key) {

tests/CachedKeySetTest.php

Lines changed: 91 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,97 @@ public function testCachedKeyIdRefresh()
138138
$this->assertEquals('bar', $cachedKeySet['bar']->getAlgorithm());
139139
}
140140

141+
public function testCacheItemWithExpiresAfter()
142+
{
143+
$expiresAfter = 10;
144+
$cacheItem = $this->prophesize('Psr\Cache\CacheItemInterface');
145+
$cacheItem->isHit()
146+
->shouldBeCalledOnce()
147+
->willReturn(false);
148+
$cacheItem->set(Argument::any())
149+
->shouldBeCalledOnce();
150+
$cacheItem->expiresAfter($expiresAfter)
151+
->shouldBeCalledOnce();
152+
153+
$cache = $this->prophesize('Psr\Cache\CacheItemPoolInterface');
154+
$cache->getItem($this->testJwkUriKey)
155+
->shouldBeCalledOnce()
156+
->willReturn($cacheItem->reveal());
157+
$cache->save(Argument::any())
158+
->shouldBeCalledOnce();
159+
160+
$cachedKeySet = new CachedKeySet(
161+
$this->testJwkUri,
162+
$this->getMockHttpClient($this->testJwk1),
163+
$this->getMockHttpFactory(),
164+
$cache->reveal(),
165+
$expiresAfter
166+
);
167+
$this->assertInstanceOf('Firebase\JWT\Key', $cachedKeySet['foo']);
168+
$this->assertEquals('foo', $cachedKeySet['foo']->getAlgorithm());
169+
}
170+
171+
public function testJwtVerify()
172+
{
173+
$privKey1 = file_get_contents(__DIR__ . '/data/rsa1-private.pem');
174+
$payload = array('sub' => 'foo', 'exp' => strtotime('+10 seconds'));
175+
$msg = JWT::encode($payload, $privKey1, 'RS256', 'jwk1');
176+
177+
$cacheItem = $this->prophesize('Psr\Cache\CacheItemInterface');
178+
$cacheItem->isHit()
179+
->willReturn(true);
180+
$cacheItem->get()
181+
->willReturn(JWK::parseKeySet(
182+
json_decode(file_get_contents(__DIR__ . '/data/rsa-jwkset.json'), true)
183+
));
184+
185+
$cache = $this->prophesize('Psr\Cache\CacheItemPoolInterface');
186+
$cache->getItem($this->testJwkUriKey)
187+
->willReturn($cacheItem->reveal());
188+
189+
$cachedKeySet = new CachedKeySet(
190+
$this->testJwkUri,
191+
$this->prophesize('Psr\Http\Client\ClientInterface')->reveal(),
192+
$this->prophesize('Psr\Http\Message\RequestFactoryInterface')->reveal(),
193+
$cache->reveal()
194+
);
195+
196+
$result = JWT::decode($msg, $cachedKeySet);
197+
198+
$this->assertEquals("foo", $result->sub);
199+
}
200+
201+
/**
202+
* @dataProvider provideFullIntegration
203+
*/
204+
public function testFullIntegration($jwkUri, $kid)
205+
{
206+
if (!class_exists(TestMemoryCacheItemPool::class)) {
207+
$this->markTestSkipped('Use phpunit-system.xml.dist to run this tests');
208+
}
209+
210+
$cache = new TestMemoryCacheItemPool();
211+
$http = new \GuzzleHttp\Client();
212+
$factory = new \GuzzleHttp\Psr7\HttpFactory();
213+
214+
$cachedKeySet = new CachedKeySet(
215+
$jwkUri,
216+
$http,
217+
$factory,
218+
$cache
219+
);
220+
221+
$this->assertArrayHasKey($kid, $cachedKeySet);
222+
}
223+
224+
public function provideFullIntegration()
225+
{
226+
return [
227+
[$this->googleRsaUri, '182e450a35a2081faa1d9ae1d2d75a0f23d91df8'],
228+
// [$this->googleEcUri, 'LYyP2g']
229+
];
230+
}
231+
141232
private function getMockHttpClient($testJwk)
142233
{
143234
$body = $this->prophesize('Psr\Http\Message\StreamInterface');
@@ -188,56 +279,6 @@ private function getMockEmptyCache()
188279
return $cache->reveal();
189280
}
190281

191-
public function testCacheItemWithExpiresAfter()
192-
{
193-
$expiresAfter = 10;
194-
$cacheItem = $this->prophesize('Psr\Cache\CacheItemInterface');
195-
$cacheItem->isHit()
196-
->shouldBeCalledOnce()
197-
->willReturn(false);
198-
$cacheItem->set(Argument::any())
199-
->shouldBeCalledOnce();
200-
$cacheItem->expiresAfter($expiresAfter)
201-
->shouldBeCalledOnce();
202-
203-
$cache = $this->prophesize('Psr\Cache\CacheItemPoolInterface');
204-
$cache->getItem($this->testJwkUriKey)
205-
->shouldBeCalledOnce()
206-
->willReturn($cacheItem->reveal());
207-
$cache->save(Argument::any())
208-
->shouldBeCalledOnce();
209-
210-
$cachedKeySet = new CachedKeySet(
211-
$this->testJwkUri,
212-
$this->getMockHttpClient($this->testJwk1),
213-
$this->getMockHttpFactory(),
214-
$cache->reveal(),
215-
$expiresAfter
216-
);
217-
$this->assertInstanceOf('Firebase\JWT\Key', $cachedKeySet['foo']);
218-
$this->assertEquals('foo', $cachedKeySet['foo']->getAlgorithm());
219-
}
220-
221-
public function testFullIntegration()
222-
{
223-
if (!class_exists(TestMemoryCacheItemPool::class)) {
224-
$this->markTestSkipped('Use phpunit-system.xml.dist to run this tests');
225-
}
226-
227-
$cache = new TestMemoryCacheItemPool();
228-
$http = new \GuzzleHttp\Client();
229-
$factory = new \GuzzleHttp\Psr7\HttpFactory();
230-
231-
$cachedKeySet = new CachedKeySet(
232-
$this->googleRsaUri,
233-
$http,
234-
$factory,
235-
$cache
236-
);
237-
238-
$this->assertArrayHasKey('182e450a35a2081faa1d9ae1d2d75a0f23d91df8', $cachedKeySet);
239-
}
240-
241282
/*
242283
* For compatibility with PHPUnit 4.8 and PHP < 5.6
243284
*/

0 commit comments

Comments
 (0)