@@ -17,11 +17,12 @@ class CachedKeySetTest extends TestCase
1717 private $ testJwksUri = 'https://jwk.uri ' ;
1818 private $ testJwksUriKey = 'jwkshttpsjwk.uri ' ;
1919 private $ testJwks1 = '{"keys": [{"kid":"foo","kty":"RSA","alg":"foo","n":"","e":""}]} ' ;
20+ private $ testCachedJwks1 = ['foo ' => ['kid ' => 'foo ' , 'kty ' => 'RSA ' , 'alg ' => 'foo ' , 'n ' => '' , 'e ' => '' ]];
2021 private $ testJwks2 = '{"keys": [{"kid":"bar","kty":"RSA","alg":"bar","n":"","e":""}]} ' ;
2122 private $ testJwks3 = '{"keys": [{"kid":"baz","kty":"RSA","n":"","e":""}]} ' ;
2223
2324 private $ googleRsaUri = 'https://www.googleapis.com/oauth2/v3/certs ' ;
24- // private $googleEcUri = 'https://www.gstatic.com/iap/verify/public_key-jwk';
25+ private $ googleEcUri = 'https://www.gstatic.com/iap/verify/public_key-jwk ' ;
2526
2627 public function testEmptyUriThrowsException ()
2728 {
@@ -117,7 +118,7 @@ public function testKeyIdIsCached()
117118 $ cacheItem ->isHit ()
118119 ->willReturn (true );
119120 $ cacheItem ->get ()
120- ->willReturn ($ this ->testJwks1 );
121+ ->willReturn ($ this ->testCachedJwks1 );
121122
122123 $ cache = $ this ->prophesize (CacheItemPoolInterface::class);
123124 $ cache ->getItem ($ this ->testJwksUriKey )
@@ -136,6 +137,66 @@ public function testKeyIdIsCached()
136137 }
137138
138139 public function testCachedKeyIdRefresh ()
140+ {
141+ $ cacheItem = $ this ->prophesize (CacheItemInterface::class);
142+ $ cacheItem ->isHit ()
143+ ->shouldBeCalledOnce ()
144+ ->willReturn (true );
145+ $ cacheItem ->get ()
146+ ->shouldBeCalledOnce ()
147+ ->willReturn ($ this ->testCachedJwks1 );
148+ $ cacheItem ->set (Argument::any ())
149+ ->shouldBeCalledOnce ()
150+ ->will (function () {
151+ return $ this ;
152+ });
153+
154+ $ cache = $ this ->prophesize (CacheItemPoolInterface::class);
155+ $ cache ->getItem ($ this ->testJwksUriKey )
156+ ->shouldBeCalledOnce ()
157+ ->willReturn ($ cacheItem ->reveal ());
158+ $ cache ->save (Argument::any ())
159+ ->shouldBeCalledOnce ()
160+ ->willReturn (true );
161+
162+ $ cachedKeySet = new CachedKeySet (
163+ $ this ->testJwksUri ,
164+ $ this ->getMockHttpClient ($ this ->testJwks2 ), // updated JWK
165+ $ this ->getMockHttpFactory (),
166+ $ cache ->reveal ()
167+ );
168+ $ this ->assertInstanceOf (Key::class, $ cachedKeySet ['foo ' ]);
169+ $ this ->assertSame ('foo ' , $ cachedKeySet ['foo ' ]->getAlgorithm ());
170+
171+ $ this ->assertInstanceOf (Key::class, $ cachedKeySet ['bar ' ]);
172+ $ this ->assertSame ('bar ' , $ cachedKeySet ['bar ' ]->getAlgorithm ());
173+ }
174+
175+ public function testKeyIdIsCachedFromPreviousFormat ()
176+ {
177+ $ cacheItem = $ this ->prophesize (CacheItemInterface::class);
178+ $ cacheItem ->isHit ()
179+ ->willReturn (true );
180+ $ cacheItem ->get ()
181+ ->willReturn ($ this ->testJwks1 );
182+
183+ $ cache = $ this ->prophesize (CacheItemPoolInterface::class);
184+ $ cache ->getItem ($ this ->testJwksUriKey )
185+ ->willReturn ($ cacheItem ->reveal ());
186+ $ cache ->save (Argument::any ())
187+ ->willReturn (true );
188+
189+ $ cachedKeySet = new CachedKeySet (
190+ $ this ->testJwksUri ,
191+ $ this ->prophesize (ClientInterface::class)->reveal (),
192+ $ this ->prophesize (RequestFactoryInterface::class)->reveal (),
193+ $ cache ->reveal ()
194+ );
195+ $ this ->assertInstanceOf (Key::class, $ cachedKeySet ['foo ' ]);
196+ $ this ->assertSame ('foo ' , $ cachedKeySet ['foo ' ]->getAlgorithm ());
197+ }
198+
199+ public function testCachedKeyIdRefreshFromPreviousFormat ()
139200 {
140201 $ cacheItem = $ this ->prophesize (CacheItemInterface::class);
141202 $ cacheItem ->isHit ()
@@ -213,12 +274,18 @@ public function testJwtVerify()
213274 $ payload = ['sub ' => 'foo ' , 'exp ' => strtotime ('+10 seconds ' )];
214275 $ msg = JWT ::encode ($ payload , $ privKey1 , 'RS256 ' , 'jwk1 ' );
215276
277+ // format the cached value to match the expected format
278+ $ cachedJwks = [];
279+ $ rsaKeySet = file_get_contents (__DIR__ . '/data/rsa-jwkset.json ' );
280+ foreach (json_decode ($ rsaKeySet , true )['keys ' ] as $ k => $ v ) {
281+ $ cachedJwks [$ v ['kid ' ]] = $ v ;
282+ }
283+
216284 $ cacheItem = $ this ->prophesize (CacheItemInterface::class);
217285 $ cacheItem ->isHit ()
218286 ->willReturn (true );
219287 $ cacheItem ->get ()
220- ->willReturn (file_get_contents (__DIR__ . '/data/rsa-jwkset.json ' )
221- );
288+ ->willReturn ($ cachedJwks );
222289
223290 $ cache = $ this ->prophesize (CacheItemPoolInterface::class);
224291 $ cache ->getItem ($ this ->testJwksUriKey )
@@ -297,7 +364,7 @@ public function provideFullIntegration()
297364 {
298365 return [
299366 [$ this ->googleRsaUri ],
300- // [$this->googleEcUri, 'LYyP2g']
367+ [$ this ->googleEcUri , 'LYyP2g ' ]
301368 ];
302369 }
303370
0 commit comments