feat: add accessor and mutator for encrypting/decrypting discord tokens

This commit is contained in:
Miguel Nogueira 2022-08-23 03:00:11 +01:00
parent d4c144b2d2
commit a5cbd31b76
No known key found for this signature in database
GPG Key ID: 3C6A7E29AF26D370

View File

@ -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