2026-01-26 21:08:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-01-27 16:30:46 +00:00
|
|
|
namespace Core\Tenant\Models;
|
2026-01-26 21:08:59 +00:00
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* User two-factor authentication record.
|
|
|
|
|
*
|
|
|
|
|
* Stores TOTP secrets and recovery codes for 2FA.
|
2026-01-29 12:20:53 +00:00
|
|
|
* Sensitive fields are encrypted at rest using Laravel's encryption.
|
|
|
|
|
*
|
|
|
|
|
* Note: The database column is 'secret' but the codebase uses 'secret_key'.
|
|
|
|
|
* Accessor/mutator methods handle the translation transparently.
|
2026-01-26 21:08:59 +00:00
|
|
|
*/
|
|
|
|
|
class UserTwoFactorAuth extends Model
|
|
|
|
|
{
|
|
|
|
|
protected $table = 'user_two_factor_auth';
|
|
|
|
|
|
2026-01-29 12:20:53 +00:00
|
|
|
/**
|
|
|
|
|
* Fillable attributes.
|
|
|
|
|
*
|
|
|
|
|
* Note: secret_key is an alias for the 'secret' column, handled by mutator.
|
|
|
|
|
*/
|
2026-01-26 21:08:59 +00:00
|
|
|
protected $fillable = [
|
|
|
|
|
'user_id',
|
2026-01-29 12:20:53 +00:00
|
|
|
'secret',
|
|
|
|
|
'secret_key', // Alias handled by setSecretKeyAttribute
|
2026-01-26 21:08:59 +00:00
|
|
|
'recovery_codes',
|
|
|
|
|
'confirmed_at',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
2026-01-29 12:20:53 +00:00
|
|
|
'secret' => 'encrypted',
|
|
|
|
|
'recovery_codes' => 'encrypted:collection',
|
2026-01-26 21:08:59 +00:00
|
|
|
'confirmed_at' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
|
2026-01-29 12:20:53 +00:00
|
|
|
/**
|
|
|
|
|
* Accessor for backward compatibility with code using secret_key.
|
|
|
|
|
*/
|
|
|
|
|
public function getSecretKeyAttribute(): ?string
|
|
|
|
|
{
|
|
|
|
|
return $this->secret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mutator for backward compatibility with code using secret_key.
|
|
|
|
|
*
|
|
|
|
|
* Translates secret_key writes to the actual 'secret' column.
|
|
|
|
|
*/
|
|
|
|
|
public function setSecretKeyAttribute(?string $value): void
|
|
|
|
|
{
|
|
|
|
|
$this->secret = $value;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 21:08:59 +00:00
|
|
|
/**
|
|
|
|
|
* Get the user this 2FA belongs to.
|
|
|
|
|
*/
|
|
|
|
|
public function user(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
|
}
|
|
|
|
|
}
|