-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters