Laravel 13 + Livewire/Volt app for managing client organizations, documents, passwords, flexible assets, virtual machines, contacts, domains, todos, shortcuts, and role-based user administration.
35 lines
972 B
PHP
35 lines
972 B
PHP
<?php
|
|
|
|
namespace App\Casts;
|
|
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
/**
|
|
* AES-256-GCM encrypt/decrypt via the app's Crypt facade.
|
|
*
|
|
* Simplification: uses the single app-wide key as the encryption key (KEK == DEK).
|
|
* Production hardening: wrap a per-organization DEK with a KMS-held KEK (envelope
|
|
* encryption) so a single leaked app key doesn't expose every tenant's secrets.
|
|
*/
|
|
class EncryptedField implements CastsAttributes
|
|
{
|
|
public function get($model, string $key, $value, array $attributes)
|
|
{
|
|
if ($value === null) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
return Crypt::decryptString($value);
|
|
} catch (\Illuminate\Contracts\Encryption\DecryptException) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function set($model, string $key, $value, array $attributes)
|
|
{
|
|
return $value === null ? null : Crypt::encryptString($value);
|
|
}
|
|
}
|