Skip to content

Commit

Permalink
Feature: Add support for encrypted jobs (#287)
Browse files Browse the repository at this point in the history
* Add support for encrypted jobs

* Add test for EncryptedJob

* Include interface
  • Loading branch information
ArlonAntonius committed Jan 29, 2024
1 parent 5c49cd5 commit 1262cda
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Identification/Queue/Events/Processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

namespace Tenancy\Identification\Drivers\Queue\Events;

use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\SerializesAndRestoresModelIdentifiers;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Tenancy\Identification\Contracts\Tenant;
use Tenancy\Identification\Drivers\Queue\Jobs\Job as TenancyJob;

Expand Down Expand Up @@ -54,6 +56,10 @@ public function __construct(

private function unserializeToJob(string $object): object
{
if (!str_starts_with($object, 'O:')) {
$object = App::make(Encrypter::class)->decrypt($object);
}

$stdClassObj = preg_replace('/^O:\d+:"[^"]++"/', 'O:'.strlen(TenancyJob::class).':"'.TenancyJob::class.'"', $object);

return unserialize($stdClassObj, ['allowed_classes' => [Job::class, TenancyJob::class]]);
Expand Down
59 changes: 59 additions & 0 deletions tests/Identification/Queue/Integration/EncryptedJobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/*
* This file is part of the tenancy/tenancy package.
*
* Copyright Tenancy for Laravel
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @see https://tenancy.dev
* @see https://github.com/tenancy
*/

namespace Tenancy\Tests\Identification\Queue\Integration;

use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Support\Facades\Event;
use Tenancy\Identification\Drivers\Queue\Providers\IdentificationProvider;
use Tenancy\Testing\TestCase;
use Tenancy\Tests\Mocks\Jobs\EncryptedJob;

class EncryptedJobTest extends TestCase
{
protected array $additionalProviders = [IdentificationProvider::class];

/** @test */
public function jobs_do_not_contain_the_tenant_when_none_identified()
{
Event::listen([JobProcessing::class, JobProcessed::class], function ($event) {
$payload = json_decode($event->job->getRawBody(), true);

$this->assertArrayNotHasKey('tenant_identifier', $payload);
$this->assertArrayNotHasKey('tenant_key', $payload);
});

dispatch(new EncryptedJob());
}

/** @test */
public function jobs_do_contain_the_tenant_when_one_is_identified()
{
$tenant = $this->mockTenant();

$this->environment->setTenant($tenant);

Event::listen([JobProcessing::class, JobProcessed::class], function ($event) use ($tenant) {
$payload = json_decode($event->job->getRawBody(), true);

$this->assertEquals($tenant->getTenantIdentifier(), $payload['tenant_identifier']);
$this->assertEquals($tenant->getTenantKey(), $payload['tenant_key']);
});

dispatch(new EncryptedJob());
}
}
23 changes: 23 additions & 0 deletions tests/Mocks/Jobs/EncryptedJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/*
* This file is part of the tenancy/tenancy package.
*
* Copyright Tenancy for Laravel
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @see https://tenancy.dev
* @see https://github.com/tenancy
*/

namespace Tenancy\Tests\Mocks\Jobs;

use Illuminate\Contracts\Queue\ShouldBeEncrypted;

class EncryptedJob extends SimpleJob implements ShouldBeEncrypted
{
}

0 comments on commit 1262cda

Please sign in to comment.