Skip to content

Commit 2ad6934

Browse files
committed
Add test to assert key is cached
1 parent 44b636c commit 2ad6934

File tree

4 files changed

+64
-24
lines changed

4 files changed

+64
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor/
22
composer.lock
33
.idea/
4+
.phpunit.result.cache

src/OpenIdVerificator.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Stackkit\LaravelGoogleCloudTasksQueue;
44

55
use Firebase\JWT\JWT;
6+
use Firebase\JWT\SignatureInvalidException;
67
use GuzzleHttp\Client;
78
use Illuminate\Support\Arr;
89
use Illuminate\Support\Facades\Cache;
@@ -17,12 +18,33 @@ class OpenIdVerificator
1718

1819
private $guzzle;
1920
private $rsa;
21+
private $jwt;
2022
private $maxAge = [];
2123

22-
public function __construct(Client $guzzle, RSA $rsa)
24+
public function __construct(Client $guzzle, RSA $rsa, JWT $jwt)
2325
{
2426
$this->guzzle = $guzzle;
2527
$this->rsa = $rsa;
28+
$this->jwt = $jwt;
29+
}
30+
31+
public function decodeOpenIdToken($openIdToken, $kid, $cache = true)
32+
{
33+
if (!$cache) {
34+
$this->forgetFromCache();
35+
}
36+
37+
$publicKey = $this->getPublicKey($kid);
38+
39+
try {
40+
return $this->jwt->decode($openIdToken, $publicKey, ['RS256']);
41+
} catch (SignatureInvalidException $e) {
42+
if (!$cache) {
43+
throw $e;
44+
}
45+
46+
return $this->decodeOpenIdToken($openIdToken, $kid, false);
47+
}
2648
}
2749

2850
public function getPublicKey($kid = null)

src/TaskHandler.php

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
namespace Stackkit\LaravelGoogleCloudTasksQueue;
44

5-
use Firebase\JWT\SignatureInvalidException;
65
use Google\Cloud\Tasks\V2\CloudTasksClient;
76
use Illuminate\Http\Request;
87
use Illuminate\Queue\Worker;
98
use Illuminate\Queue\WorkerOptions;
109
use Firebase\JWT\JWT;
11-
use Illuminate\Support\Facades\Cache;
1210

1311
class TaskHandler
1412
{
@@ -50,30 +48,11 @@ public function authorizeRequest()
5048
$openIdToken = $this->request->bearerToken();
5149
$kid = $this->publicKey->getKidFromOpenIdToken($openIdToken);
5250

53-
$decodedToken = $this->decodeOpenIdToken($openIdToken, $kid);
51+
$decodedToken = $this->publicKey->decodeOpenIdToken($openIdToken, $kid);
5452

5553
$this->validateToken($decodedToken);
5654
}
5755

58-
private function decodeOpenIdToken($openIdToken, $kid, $cache = true)
59-
{
60-
if (!$cache) {
61-
$this->publicKey->forgetFromCache();
62-
}
63-
64-
$publicKey = $this->publicKey->getPublicKey($kid);
65-
66-
try {
67-
return $this->jwt->decode($openIdToken, $publicKey, ['RS256']);
68-
} catch (SignatureInvalidException $e) {
69-
if (!$cache) {
70-
throw $e;
71-
}
72-
73-
return $this->decodeOpenIdToken($openIdToken, $kid, false);
74-
}
75-
}
76-
7756
/**
7857
* https://developers.google.com/identity/protocols/oauth2/openid-connect#validatinganidtoken
7958
*

tests/GooglePublicKeyTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
namespace Tests;
44

5+
use Carbon\Carbon;
6+
use Firebase\JWT\JWT;
57
use GuzzleHttp\Client;
8+
use Illuminate\Cache\Events\CacheHit;
9+
use Illuminate\Cache\Events\CacheMissed;
10+
use Illuminate\Cache\Events\KeyWritten;
611
use Illuminate\Support\Facades\Cache;
12+
use Illuminate\Support\Facades\Event;
713
use Mockery;
814
use phpseclib\Crypt\RSA;
915
use Stackkit\LaravelGoogleCloudTasksQueue\OpenIdVerificator;
@@ -26,7 +32,7 @@ protected function setUp(): void
2632

2733
$this->guzzle = Mockery::mock(new Client());
2834

29-
$this->publicKey = new OpenIdVerificator($this->guzzle, new RSA());
35+
$this->publicKey = new OpenIdVerificator($this->guzzle, new RSA(), new JWT());
3036
}
3137

3238
/** @test */
@@ -48,10 +54,42 @@ public function it_caches_the_gcloud_public_key()
4854
/** @test */
4955
public function it_will_return_the_cached_gcloud_public_key()
5056
{
57+
Event::fake();
58+
5159
$this->publicKey->getPublicKey();
5260

61+
Event::assertDispatched(CacheMissed::class);
62+
Event::assertDispatched(KeyWritten::class);
63+
5364
$this->publicKey->getPublicKey();
5465

66+
Event::assertDispatched(CacheHit::class);
67+
5568
$this->guzzle->shouldHaveReceived('get')->twice();
5669
}
70+
71+
/** @test */
72+
public function public_key_is_cached_according_to_cache_control_headers()
73+
{
74+
Event::fake();
75+
76+
$this->publicKey->getPublicKey();
77+
Event::assertDispatched(CacheMissed::class);
78+
Event::assertDispatched(KeyWritten::class);
79+
Event::fake(); // reset
80+
81+
$this->publicKey->getPublicKey();
82+
Event::assertDispatched(CacheHit::class);
83+
Event::fake(); // reset
84+
85+
Carbon::setTestNow(Carbon::now()->addSeconds(3600));
86+
$this->publicKey->getPublicKey();
87+
Event::assertDispatched(CacheHit::class);
88+
Event::fake(); // reset
89+
90+
Carbon::setTestNow(Carbon::now()->addSeconds(1));
91+
$this->publicKey->getPublicKey();
92+
Event::assertDispatched(CacheMissed::class);
93+
Event::assertDispatched(KeyWritten::class);
94+
}
5795
}

0 commit comments

Comments
 (0)