@@ -17,11 +17,12 @@ class CachedKeySetTest extends TestCase
17
17
private $ testJwksUri = 'https://jwk.uri ' ;
18
18
private $ testJwksUriKey = 'jwkshttpsjwk.uri ' ;
19
19
private $ testJwks1 = '{"keys": [{"kid":"foo","kty":"RSA","alg":"foo","n":"","e":""}]} ' ;
20
+ private $ testCachedJwks1 = ['foo ' => ['kid ' => 'foo ' , 'kty ' => 'RSA ' , 'alg ' => 'foo ' , 'n ' => '' , 'e ' => '' ]];
20
21
private $ testJwks2 = '{"keys": [{"kid":"bar","kty":"RSA","alg":"bar","n":"","e":""}]} ' ;
21
22
private $ testJwks3 = '{"keys": [{"kid":"baz","kty":"RSA","n":"","e":""}]} ' ;
22
23
23
24
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 ' ;
25
26
26
27
public function testEmptyUriThrowsException ()
27
28
{
@@ -117,7 +118,7 @@ public function testKeyIdIsCached()
117
118
$ cacheItem ->isHit ()
118
119
->willReturn (true );
119
120
$ cacheItem ->get ()
120
- ->willReturn ($ this ->testJwks1 );
121
+ ->willReturn ($ this ->testCachedJwks1 );
121
122
122
123
$ cache = $ this ->prophesize (CacheItemPoolInterface::class);
123
124
$ cache ->getItem ($ this ->testJwksUriKey )
@@ -136,6 +137,66 @@ public function testKeyIdIsCached()
136
137
}
137
138
138
139
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 ()
139
200
{
140
201
$ cacheItem = $ this ->prophesize (CacheItemInterface::class);
141
202
$ cacheItem ->isHit ()
@@ -213,12 +274,18 @@ public function testJwtVerify()
213
274
$ payload = ['sub ' => 'foo ' , 'exp ' => strtotime ('+10 seconds ' )];
214
275
$ msg = JWT ::encode ($ payload , $ privKey1 , 'RS256 ' , 'jwk1 ' );
215
276
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
+
216
284
$ cacheItem = $ this ->prophesize (CacheItemInterface::class);
217
285
$ cacheItem ->isHit ()
218
286
->willReturn (true );
219
287
$ cacheItem ->get ()
220
- ->willReturn (file_get_contents (__DIR__ . '/data/rsa-jwkset.json ' )
221
- );
288
+ ->willReturn ($ cachedJwks );
222
289
223
290
$ cache = $ this ->prophesize (CacheItemPoolInterface::class);
224
291
$ cache ->getItem ($ this ->testJwksUriKey )
@@ -297,7 +364,7 @@ public function provideFullIntegration()
297
364
{
298
365
return [
299
366
[$ this ->googleRsaUri ],
300
- // [$this->googleEcUri, 'LYyP2g']
367
+ [$ this ->googleEcUri , 'LYyP2g ' ]
301
368
];
302
369
}
303
370
0 commit comments