Skip to content

Commit 2857ba1

Browse files
committed
Make audience signing optional
1 parent 9e11378 commit 2857ba1

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Please check the [Laravel support policy](https://laravel.com/docs/master/releas
4545
'handler' => env('STACKKIT_CLOUD_TASKS_HANDLER', ''),
4646
'queue' => env('STACKKIT_CLOUD_TASKS_QUEUE', 'default'),
4747
'service_account_email' => env('STACKKIT_CLOUD_TASKS_SERVICE_EMAIL', ''),
48+
'signed_audience' => env('STACKKIT_CLOUD_TASKS_SIGNED_AUDIENCE', false),
4849
// Optional: The deadline in seconds for requests sent to the worker. If the worker
4950
// does not respond by this deadline then the request is cancelled and the attempt
5051
// is marked as a DEADLINE_EXCEEDED failure.
@@ -70,6 +71,7 @@ Please check the table below on what the values mean and what their value should
7071
| `STACKKIT_CLOUD_TASKS_QUEUE` | The default queue a job will be added to |`emails`
7172
| `STACKKIT_CLOUD_TASKS_SERVICE_EMAIL` | The email address of the service account. Important, it should have the correct roles. See the section below which roles. |`my-service-account@appspot.gserviceaccount.com`
7273
| `STACKKIT_CLOUD_TASKS_HANDLER` (optional) | The URL that Cloud Tasks will call to process a job. This should be the URL to your Laravel app. By default we will use the URL that dispatched the job. |`https://<your website>.com`
74+
| `STACKKIT_CLOUD_TASKS_SIGNED_AUDIENCE` (optional) | True or false depending if you want extra security by signing the audience of your tasks. May misbehave in certain Cloud Run setups. Defaults to false. | `false`
7375
</details>
7476
<details>
7577
<summary>

src/CloudTasksQueue.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,13 @@ function ($payload, $queue, $delay) {
132132
*/
133133
protected function pushToCloudTasks($queue, $payload, $delay = 0)
134134
{
135+
$handleTargetUrl = $this->getHandler();
135136
$queue = $this->getQueue($queue);
136137
$queueName = $this->client->queueName($this->config['project'], $this->config['location'], $queue);
137138
$availableAt = $this->availableAt($delay);
138139

139140
$httpRequest = $this->createHttpRequest();
140-
$httpRequest->setUrl($this->getHandler());
141+
$httpRequest->setUrl($handleTargetUrl);
141142
$httpRequest->setHttpMethod(HttpMethod::POST);
142143

143144
$payload = json_decode($payload, true);
@@ -167,14 +168,14 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
167168

168169
$token = new OidcToken;
169170
$token->setServiceAccountEmail($this->config['service_account_email']);
170-
$token->setAudience(hash_hmac('sha256', $this->getHandler(), config('app.key')));
171+
$token->setAudience($this->getAudience());
171172
$httpRequest->setOidcToken($token);
172173

173174
if ($availableAt > time()) {
174175
$task->setScheduleTime(new Timestamp(['seconds' => $availableAt]));
175176
}
176177

177-
$createdTask = CloudTasksApi::createTask($queueName, $task);
178+
CloudTasksApi::createTask($queueName, $task);
178179

179180
event((new TaskCreated)->queue($queue)->task($task));
180181

@@ -268,4 +269,9 @@ public function getHandler(): string
268269
{
269270
return Config::getHandler($this->config['handler']);
270271
}
272+
273+
public function getAudience(): string
274+
{
275+
return Config::getAudience($this->config);
276+
}
271277
}

src/Config.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,16 @@ public static function getHandler($handler): string
8282
);
8383
}
8484
}
85+
86+
/**
87+
* @param string $handler
88+
*/
89+
public static function getAudience(array $config): string
90+
{
91+
$handler = self::getHandler($config['handler']);
92+
93+
return $config['signed_audience'] ?? false
94+
? hash_hmac('sha256', $handler, config('app.key'))
95+
: $handler;
96+
}
8597
}

src/OpenIdVerificatorConcrete.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function verify(?string $token, array $config): void
1818
(new AccessToken())->verify(
1919
$token,
2020
[
21-
'audience' => hash_hmac('sha256', app('queue')->getHandler(), config('app.key')),
21+
'audience' => Config::getAudience($config),
2222
'throwException' => true,
2323
]
2424
);

src/OpenIdVerificatorFake.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function verify(?string $token, array $config): void
1717
(new AccessToken())->verify(
1818
$token,
1919
[
20-
'audience' => hash_hmac('sha256', app('queue')->getHandler(), config('app.key')),
20+
'audience' => Config::getAudience($config),
2121
'throwException' => true,
2222
'certsLocation' => __DIR__ . '/../tests/Support/self-signed-public-key-as-jwk.json',
2323
]

0 commit comments

Comments
 (0)