Files
CABM-IT/app/Casts/EncryptedField.php
T
jeremy bayse 8f22dca9c8 Initial commit: CABM-IT (ItGlue-style MSP documentation platform)
Laravel 13 + Livewire/Volt app for managing client organizations,
documents, passwords, flexible assets, virtual machines, contacts,
domains, todos, shortcuts, and role-based user administration.
2026-08-16 13:28:12 +02:00

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);
}
}