Подтверждение кода в профиле Filament
Давайте подтвердим включение проверки. Перед записью кода в профиле работник вводит его второй раз. Страница /admin/profile.
В схеме управления
добавим поле подтверждения
с правилом same
на основное значение
в PinAuthentication.php:
<?php
namespace App\Filament\Auth;
use App\Models\User;
use Closure;
use Filament\Auth\MultiFactor\Contracts\MultiFactorAuthenticationProvider;
use Filament\Forms\Components\TextInput;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Cache;
use SensitiveParameter;
class PinAuthentication implements MultiFactorAuthenticationProvider
{
protected int $maxAttempts = 5;
public static function make(): static
{
return app(static::class);
}
public function getId(): string
{
return 'pin';
}
public function getLoginFormLabel(): string
{
return 'PIN';
}
public function isEnabled(Authenticatable $user): bool
{
if (! ($user instanceof User)) {
return false;
}
return (bool) $user->pin_enabled;
}
public function getManagementSchemaComponents(): array
{
return [
TextInput::make('pin_code')
->label('PIN code')
->password()
->numeric()
->required(),
TextInput::make('pin_code_confirmation')
->label('Confirm PIN code')
->password()
->numeric()
->same('pin_code')
->required(),
];
}
public function getChallengeFormComponents(Authenticatable $user): array
{
return [
TextInput::make('code')
->label('PIN code')
->required()
->rule(fn (): Closure => function (string $attribute, #[SensitiveParameter] mixed $value, Closure $fail) use ($user): void {
if (! ($user instanceof User)) {
$fail('The PIN code is invalid.');
return;
}
$key = 'pin-mfa-failures:' . $user->getAuthIdentifier();
if ((int) Cache::get($key, 0) >= $this->maxAttempts) {
$fail('Too many attempts.');
return;
}
if (! hash_equals((string) $user->pin_code, (string) $value)) {
Cache::put($key, (int) Cache::get($key, 0) + 1, now()->addMinutes(15));
$fail('The PIN code is invalid.');
return;
}
Cache::forget($key);
}),
];
}
}
?>
Прежде чем запомнить код, попросите ввести его повторно.