From b37fa2687ef6d79828b524eb09634be2180f6134 Mon Sep 17 00:00:00 2001 From: Christophe Date: Tue, 9 Apr 2024 18:58:47 +0200 Subject: [PATCH 1/2] Ability to encrypt automatically message's body --- config/chat.php | 3 ++- src/Models/Message.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/config/chat.php b/config/chat.php index 526a3ff..c04c822 100644 --- a/config/chat.php +++ b/config/chat.php @@ -14,5 +14,6 @@ 'media_folder' => env('CHAT_MEDIA_FOLDER', 'image'), 'pusher_event_trigger' => [ 'send_message' => env('CHAT_SEND_MESSAGE_PUSHER_EVENT', true) - ] + ], + 'encrypt_message' => env('CHAT_ENCRYPT_MESSAGE', false), ]; diff --git a/src/Models/Message.php b/src/Models/Message.php index 12043ff..1d5d257 100644 --- a/src/Models/Message.php +++ b/src/Models/Message.php @@ -73,4 +73,22 @@ public function variables() { return $this->hasMany(MessageVariable::class, 'message_id', 'id'); } + + /** + * Get the attributes that should be cast. + * + * @return array + */ + protected function casts(): array + { + if(config('chat.encrypt_message')) { + return [ + 'body' => 'encrypted', + ]; + }else { + return [ + 'body' => 'string', + ]; + } + } } From 0c379e1c6ac2238e2b554410cd9601ee709912e9 Mon Sep 17 00:00:00 2001 From: Harshil Patanvadiya Date: Wed, 22 May 2024 11:10:01 +0530 Subject: [PATCH 2/2] Fix encryption message --- README.md | 2 +- src/Helpers/Helper.php | 12 ++++++++++++ src/Models/Message.php | 13 +++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index be7aff0..40096e1 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ php artisan migrate To configure message body encryption add these key on `.env` file. ``` -CHAT_ENCRYPT_MESSAGE=true #boolen +CHAT_ENCRYPT_MESSAGE=true #boolean ``` ## Usage diff --git a/src/Helpers/Helper.php b/src/Helpers/Helper.php index 6874ad9..db9877c 100644 --- a/src/Helpers/Helper.php +++ b/src/Helpers/Helper.php @@ -6,7 +6,9 @@ use Illuminate\Support\Str; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Storage; +use Illuminate\Contracts\Encryption\DecryptException; class Helper { @@ -63,4 +65,14 @@ public static function fileDelete($disk, $path, $fileName) return true; } } + + public static function isEncrypted($value) + { + try { + Crypt::decryptString($value); + return true; + } catch (DecryptException $e) { + return false; + } + } } diff --git a/src/Models/Message.php b/src/Models/Message.php index 1d5d257..e2b31e5 100644 --- a/src/Models/Message.php +++ b/src/Models/Message.php @@ -3,9 +3,12 @@ namespace SevenSpan\Chat\Models; use App\Models\User; +use SevenSpan\Chat\Helpers\Helper; +use Illuminate\Support\Facades\Crypt; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Storage; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; class Message extends Model @@ -91,4 +94,14 @@ protected function casts(): array ]; } } + + + protected function body(): Attribute + { + return Attribute::make( + get: function (string $value) { + return Helper::isEncrypted($value) ? Crypt::decryptString($value) : $value; + } + ); + } }