Skip to content

Commit abe5dbc

Browse files
committed
Simplify authorization with simple token instead of hacky signed audience
1 parent 90359c0 commit abe5dbc

17 files changed

+43
-346
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ Please check the [Laravel support policy](https://laravel.com/docs/master/releas
3535
composer require stackkit/laravel-google-cloud-tasks-queue
3636
```
3737

38+
Publish the service provider:
39+
40+
```console
41+
php artisan vendor:publish --provider=cloud-tasks
42+
```
43+
3844
Add a new queue connection to `config/queue.php`
3945

4046
```php
@@ -51,7 +57,6 @@ Please check the [Laravel support policy](https://laravel.com/docs/master/releas
5157
// Required when not using AppEngine
5258
'handler' => env('STACKKIT_CLOUD_TASKS_HANDLER', ''),
5359
'service_account_email' => env('STACKKIT_CLOUD_TASKS_SERVICE_EMAIL', ''),
54-
'signed_audience' => env('STACKKIT_CLOUD_TASKS_SIGNED_AUDIENCE', true),
5560

5661
// Optional: The deadline in seconds for requests sent to the worker. If the worker
5762
// does not respond by this deadline then the request is cancelled and the attempt
@@ -61,7 +66,17 @@ Please check the [Laravel support policy](https://laravel.com/docs/master/releas
6166
],
6267
```
6368

64-
Update the `QUEUE_CONNECTION` environment variable
69+
If you are using separate services for dispatching and handling tasks, you may want to change the following settings:
70+
71+
```php
72+
// config/cloud-tasks.php
73+
74+
// If the application only dispatches jobs
75+
'disable_task_handler' => env('STACKKIT_CLOUD_TASKS_DISABLE_TASK_HANDLER', false),
76+
77+
// If the application only handles jobs and is secured by already (e.g. requires Authentication)
78+
'disable_security_key_verification' => env('STACKKIT_CLOUD_TASKS_DISABLE_SECURITY_KEY_VERIFICATION', false),
79+
```
6580

6681
```dotenv
6782
QUEUE_CONNECTION=cloudtasks
@@ -82,7 +97,6 @@ Please check the table below on what the values mean and what their value should
8297
| **Non- App Engine apps**
8398
| `STACKKIT_CLOUD_TASKS_SERVICE_EMAIL` (optional) | 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`
8499
| `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`
85-
| `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 true. | `true`
86100
</details>
87101
<details>
88102
<summary>

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"php": "^8.1",
1212
"ext-json": "*",
1313
"phpseclib/phpseclib": "^3.0",
14-
"google/auth": "^v1.29.1",
1514
"google/cloud-tasks": "^1.10",
1615
"thecodingmachine/safe": "^1.0|^2.0"
1716
},

config/cloud-tasks.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
declare(strict_types=1);
44

55
return [
6+
// If the application only dispatches jobs
7+
'disable_task_handler' => env('STACKKIT_CLOUD_TASKS_DISABLE_TASK_HANDLER', false),
8+
9+
// If the application only handles jobs and is secured by already (e.g. requires Authentication)
10+
'disable_security_key_verification' => env('STACKKIT_CLOUD_TASKS_DISABLE_SECURITY_KEY_VERIFICATION', false),
11+
612
'dashboard' => [
713
'enabled' => env('STACKKIT_CLOUD_TASKS_DASHBOARD_ENABLED', false),
814
'password' => env('STACKKIT_CLOUD_TASKS_DASHBOARD_PASSWORD', 'MyPassword1!'),

src/CloudTasksQueue.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
120120
$payload = $this->withQueueName($payload, $queue);
121121
$payload = $this->withTaskName($payload, $task->getName());
122122
$payload = $this->withConnectionName($payload, $this->getConnectionName());
123+
$payload = $this->withSecurityKey($payload);
123124

124125
if (! empty($this->config['app_engine'])) {
125126
$path = \Safe\parse_url(route('cloud-tasks.handle-task'), PHP_URL_PATH);
@@ -143,9 +144,6 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
143144

144145
$token = new OidcToken;
145146
$token->setServiceAccountEmail($this->config['service_account_email']);
146-
if ($audience = $this->getAudience()) {
147-
$token->setAudience($audience);
148-
}
149147
$httpRequest->setOidcToken($token);
150148
$task->setHttpRequest($httpRequest);
151149
}
@@ -221,6 +219,13 @@ private function withConnectionName(array $payload, string $connectionName): arr
221219
return $payload;
222220
}
223221

222+
private function withSecurityKey(array $payload): array
223+
{
224+
$payload['internal']['securityKey'] = encrypt($this->config['security_key'] ?? $payload['uuid']);
225+
226+
return $payload;
227+
}
228+
224229
/**
225230
* Pop the next job off of the queue.
226231
*
@@ -257,9 +262,4 @@ public function getHandler(): string
257262
default => $handler,
258263
};
259264
}
260-
261-
public function getAudience(): ?string
262-
{
263-
return Config::getAudience($this->config);
264-
}
265265
}

src/CloudTasksServiceProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ private function registerClient(): void
2929
return new CloudTasksClient();
3030
});
3131

32-
$this->app->bind('open-id-verificator', OpenIdVerificatorConcrete::class);
3332
$this->app->bind('cloud-tasks-api', CloudTasksApiConcrete::class);
3433
}
3534

@@ -56,6 +55,10 @@ private function registerConfig(): void
5655

5756
private function registerRoutes(): void
5857
{
58+
if (config('cloud-tasks.disable_task_handler')) {
59+
return;
60+
}
61+
5962
/**
6063
* @var \Illuminate\Routing\Router $router
6164
*/

src/Config.php

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/Errors.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/OpenIdVerificator.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/OpenIdVerificatorConcrete.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/OpenIdVerificatorFake.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)