feat(auth): harden authentication and add configurable two-factor support
This commit is contained in:
43
app/Services/Auth/TwoFactorEnrollmentState.php
Normal file
43
app/Services/Auth/TwoFactorEnrollmentState.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Laravel\Fortify\Fortify;
|
||||
|
||||
class TwoFactorEnrollmentState
|
||||
{
|
||||
/**
|
||||
* Build the two-factor state shared by setup and profile screens.
|
||||
*
|
||||
* @return array{
|
||||
* available: bool,
|
||||
* enabled: bool,
|
||||
* pending: bool,
|
||||
* requiresPassword: bool,
|
||||
* qrCodeDataUri: string|null,
|
||||
* secretKey: string|null
|
||||
* }
|
||||
*/
|
||||
public function for(User $user): array
|
||||
{
|
||||
$available = (bool) config('auth-ui.features.two_factor');
|
||||
$enabled = $available && $user->hasEnabledTwoFactorAuthentication();
|
||||
$pending = $available
|
||||
&& filled($user->two_factor_secret)
|
||||
&& ! $enabled;
|
||||
|
||||
return [
|
||||
'available' => $available,
|
||||
'enabled' => $enabled,
|
||||
'pending' => $pending,
|
||||
'requiresPassword' => $user->hasPassword(),
|
||||
'qrCodeDataUri' => $pending
|
||||
? 'data:image/svg+xml;base64,'.base64_encode($user->twoFactorQrCodeSvg())
|
||||
: null,
|
||||
'secretKey' => $pending
|
||||
? Fortify::currentEncrypter()->decrypt($user->two_factor_secret)
|
||||
: null,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user