Filament handling encryption and decryption

When using the encrypted cast in a model, does filament handle the encryption and decryption states automatically, or do I have to do this manually? Do I have to do this myself, according to the Laravel documentation example for encrypting data? https://laravel.com/docs/11.x/encryption#using-the-encrypter
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
9 Replies
toeknee
toeknee6d ago
No the cast should suffice since it's cast too and from, this works perfectly for me in filament and encrypts in the database perfectly.
DevShaded
DevShadedOP6d ago
I made a field encrypted with the model cast like this, but when I do this and try to edit something with that encrypted field I get an error back instead.
protected function casts(): array
{
return [
'client_secret' => 'encrypted',
];
}
protected function casts(): array
{
return [
'client_secret' => 'encrypted',
];
}
No description
DevShaded
DevShadedOP6d ago
Oh, nvm... I viewd a old one that wasnt encrypted thats was I got the error
toeknee
toeknee5d ago
So you can write an getAttribute method which checks if it is encryped and if not it encrypts it
DevShaded
DevShadedOP5d ago
That’s true, I had some issues when creating seeders with the encrypted fields it does not work for some reason when I try to view them in the filament panel
toeknee
toeknee5d ago
It’ll be how they are being handled in the seeder if you are not using the models you need to ensure they are encrypted at store
DevShaded
DevShadedOP5d ago
This is how my factory and seeder looks like
// Factory
public function definition(): array
{
$stateParameters = [
'random_string' => bin2hex(random_bytes(16)),
'session_id' => 'user_session_12345',
'csrf_token' => bin2hex(random_bytes(32)),
];

return [
'client_id' => $this->faker->word(),
'client_secret' => Crypt::encrypt('example-client-secret-' . Str::random(10)),
'authorization_endpoint' => 'https://example.com/oauth/authorize',
'token_endpoint' => 'https://example.com/oauth/token',
'redirect_uris' => [
['uri' => 'http://localhost:8000/callback'],
['uri' => 'http://localhost:8000/callback'],
],
'scopes' => [
['scope' => 'read'],
['scope' => 'write'],
],
'grant_types' => [
['grant_type' => 'authorization_code'],
['grant_type' => 'implicit'],
['grant_type' => 'client_credentials'],
['grant_type' => 'refresh_token'],
],
'state_parameter' => $this->faker->randomElement($stateParameters),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),

'user_id' => User::factory(),
];
}
// Factory
public function definition(): array
{
$stateParameters = [
'random_string' => bin2hex(random_bytes(16)),
'session_id' => 'user_session_12345',
'csrf_token' => bin2hex(random_bytes(32)),
];

return [
'client_id' => $this->faker->word(),
'client_secret' => Crypt::encrypt('example-client-secret-' . Str::random(10)),
'authorization_endpoint' => 'https://example.com/oauth/authorize',
'token_endpoint' => 'https://example.com/oauth/token',
'redirect_uris' => [
['uri' => 'http://localhost:8000/callback'],
['uri' => 'http://localhost:8000/callback'],
],
'scopes' => [
['scope' => 'read'],
['scope' => 'write'],
],
'grant_types' => [
['grant_type' => 'authorization_code'],
['grant_type' => 'implicit'],
['grant_type' => 'client_credentials'],
['grant_type' => 'refresh_token'],
],
'state_parameter' => $this->faker->randomElement($stateParameters),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),

'user_id' => User::factory(),
];
}
// Seeder
$users = User::factory(10)->create();

foreach ($users as $user) {
OauthClient::factory(5)->create([
'user_id' => $user->id,
]);
}
// Seeder
$users = User::factory(10)->create();

foreach ($users as $user) {
OauthClient::factory(5)->create([
'user_id' => $user->id,
]);
}
Ah, its fixed lol ok nvm ahha Sorry for the ping
toeknee
toeknee4d ago
No problemo

Did you find this page helpful?