|
2 | 2 |
|
3 | 3 | namespace Stackkit\LaravelGoogleCloudTasksQueue; |
4 | 4 |
|
| 5 | +use Firebase\JWT\SignatureInvalidException; |
5 | 6 | use Google\Cloud\Tasks\V2\CloudTasksClient; |
6 | 7 | use Illuminate\Http\Request; |
7 | 8 | use Illuminate\Queue\Worker; |
8 | 9 | use Illuminate\Queue\WorkerOptions; |
9 | 10 | use Firebase\JWT\JWT; |
| 11 | +use Illuminate\Support\Facades\Cache; |
10 | 12 |
|
11 | 13 | class TaskHandler |
12 | 14 | { |
@@ -47,13 +49,31 @@ public function authorizeRequest() |
47 | 49 |
|
48 | 50 | $openIdToken = $this->request->bearerToken(); |
49 | 51 | $kid = $this->publicKey->getKidFromOpenIdToken($openIdToken); |
50 | | - $publicKey = $this->publicKey->getPublicKey($kid); |
51 | 52 |
|
52 | | - $decodedToken = $this->jwt->decode($openIdToken, $publicKey, ['RS256']); |
| 53 | + $decodedToken = $this->decodeOpenIdToken($openIdToken, $kid); |
53 | 54 |
|
54 | 55 | $this->validateToken($decodedToken); |
55 | 56 | } |
56 | 57 |
|
| 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 | + |
57 | 77 | /** |
58 | 78 | * https://developers.google.com/identity/protocols/oauth2/openid-connect#validatinganidtoken |
59 | 79 | * |
|
0 commit comments