From a5cbd31b767dc913c80f16920d45b44eb6a4ff4d Mon Sep 17 00:00:00 2001 From: miguel456 Date: Tue, 23 Aug 2022 03:00:11 +0100 Subject: [PATCH] feat: add accessor and mutator for encrypting/decrypting discord tokens --- app/User.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/app/User.php b/app/User.php index 92b7ea4..7560bb4 100755 --- a/app/User.php +++ b/app/User.php @@ -24,9 +24,11 @@ namespace App; use App\Services\AccountSuspensionService; use App\Traits\HandlesAccountTokens; use Illuminate\Contracts\Auth\MustVerifyEmail; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Facades\Crypt; use Mpociot\Teamwork\Traits\UserHasTeams; use Spatie\Permission\Traits\HasRoles; @@ -100,6 +102,33 @@ class User extends Authenticatable implements MustVerifyEmail } + // ACCESSORS AND MUTATORS + + /** + * Encrypt/decrypt the Discord access token. Data is encrypted at rest. + * + * @return Attribute + */ + public function discordToken(): Attribute { + return Attribute::make( + get: fn ($value) => Crypt::decryptString($value), + set: fn ($value) => Crypt::encryptString($value) + ); + } + + /** + * Encrypt/decrypt the Discord refresh token. Data is encrypted at rest. + * + * @return Attribute + */ + public function discordRefreshToken(): Attribute { + return Attribute::make( + get: fn ($value) => Crypt::decryptString($value), + set: fn ($value) => Crypt::encryptString($value) + ); + } + + // UTILITY LOGIC