Skip to content

Commit

Permalink
Change encryption to Custom Casts
Browse files Browse the repository at this point in the history
  • Loading branch information
gawsoftpl committed Oct 4, 2024
1 parent 99c8502 commit 6d55f2e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 55 deletions.
56 changes: 1 addition & 55 deletions src/Storage/EntryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Telescope\Database\Factories\EntryModelFactory;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Database\Eloquent\Casts\Attribute;

class EntryModel extends Model
{
Expand All @@ -32,7 +30,7 @@ class EntryModel extends Model
* @var array
*/
protected $casts = [
'content' => 'json',
'content' => EntryModelEncryptionCast::class,
];

/**
Expand All @@ -56,20 +54,6 @@ class EntryModel extends Model
*/
public $incrementing = false;


/**
* Accessor for check the content attribute
*
* @return Illuminate\Database\Eloquent\Casts\Attribute
*/
protected function content(): Attribute
{
return Attribute::make(
get: fn (string $value) => $this->decryptContent($value),
set: fn (string $value) => $this->encryptContent($value),
);
}

/**
* Scope the query for the given query options.
*
Expand Down Expand Up @@ -218,42 +202,4 @@ public static function newFactory()
{
return EntryModelFactory::new();
}

/**
* Decode/Decrypt content attribute. Check that is encrypted.
*
* @return string
*/
protected function decryptContent($content)
{
if (
!$this->encryptionIsEnabled()
|| substr($content, 0, 8) != 'encrypt:'
)
return $content;

return Crypt::decryptString(substr($content, 8, strlen($content)));
}

/**
* Encode/Encrypt content attribute. Add 'encrypt' prefix for mark
* that string is encrypted.
*
* @return string
*/
protected function encryptContent($content)
{
if (!$this->encryptionIsEnabled()) return $value;
return 'encrypt:'.Crypt::encryptString($value);
}

/**
* Check that encryption is enabled.
*
* @return bool
*/
function encryptionIsEnabled()
{
return config('telescope.encryption');
}
}
80 changes: 80 additions & 0 deletions src/Storage/EntryModelEncryptionCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php


namespace Laravel\Telescope\Storage;

use App\ValueObjects\Address as AddressValueObject;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use InvalidArgumentException;
use Illuminate\Database\Eloquent\Casts\Json;
use Illuminate\Support\Facades\Crypt;

class EntryModelEncryptionCast extends Json implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes)
{
return parent::decode(
$this->decryptContent($value)
);
}

/**
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
* @return array<string, string>
*/
public function set(Model $model, string $key, mixed $value, array $attributes)
{
return $this->encryptContent(parent::encode(
$value
));
}

/**
* Decode/Decrypt content attribute. Check that is encrypted.
*
* @return mixed
*/
protected function decryptContent($content)
{
if (
!$content
|| !$this->encryptionIsEnabled()
|| substr($content, 0, 8) != 'encrypt:'
)
return $content;


return Crypt::decryptString(substr($content, 8, strlen($content)));
}

/**
* Encode/Encrypt content attribute. Add 'encrypt' prefix for mark
* that string is encrypted.
*
* @return mixed
*/
protected function encryptContent($content)
{
if (!$content || !$this->encryptionIsEnabled()) return $content;
return 'encrypt:'.Crypt::encryptString($content);
}

/**
* Check that encryption is enabled.
*
* @return bool
*/
function encryptionIsEnabled()
{
return config('telescope.encryption');
}

}
16 changes: 16 additions & 0 deletions tests/Storage/DatabaseEntriesRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,20 @@ public function test_update()
$this->assertCount(1, $failedUpdates);
$this->assertSame('missing-id', $failedUpdates->first()->uuid);
}

public function test_content_is_encrypted()
{
config()->set('telescope.encryption', true);
$content = [
"test" => 123
];

$entry = EntryModelFactory::new()->create([
'content' => $content
]);

$repository = new DatabaseEntriesRepository('testbench');
$result = $repository->find($entry->uuid);
$this->assertSame($result->content, $content);
}
}

0 comments on commit 6d55f2e

Please sign in to comment.