Skip to content

Commit c6d26c0

Browse files
committed
Add retry if signature verification fails
1 parent ba5557d commit c6d26c0

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/OpenIdVerificator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,9 @@ public function isCached()
7070
{
7171
return Cache::has(self::V3_CERTS);
7272
}
73+
74+
public function forgetFromCache()
75+
{
76+
Cache::forget(self::V3_CERTS);
77+
}
7378
}

src/TaskHandler.php

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

33
namespace Stackkit\LaravelGoogleCloudTasksQueue;
44

5+
use Firebase\JWT\SignatureInvalidException;
56
use Google\Cloud\Tasks\V2\CloudTasksClient;
67
use Illuminate\Http\Request;
78
use Illuminate\Queue\Worker;
89
use Illuminate\Queue\WorkerOptions;
910
use Firebase\JWT\JWT;
11+
use Illuminate\Support\Facades\Cache;
1012

1113
class TaskHandler
1214
{
@@ -47,13 +49,31 @@ public function authorizeRequest()
4749

4850
$openIdToken = $this->request->bearerToken();
4951
$kid = $this->publicKey->getKidFromOpenIdToken($openIdToken);
50-
$publicKey = $this->publicKey->getPublicKey($kid);
5152

52-
$decodedToken = $this->jwt->decode($openIdToken, $publicKey, ['RS256']);
53+
$decodedToken = $this->decodeOpenIdToken($openIdToken, $kid);
5354

5455
$this->validateToken($decodedToken);
5556
}
5657

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+
5777
/**
5878
* https://developers.google.com/identity/protocols/oauth2/openid-connect#validatinganidtoken
5979
*

0 commit comments

Comments
 (0)