48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use lfkeitel\phptotp\Base32;
|
|
use lfkeitel\phptotp\Totp;
|
|
|
|
class ValidateOtpRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize()
|
|
{
|
|
return Auth::check();
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
'otp' => [
|
|
'string',
|
|
'required',
|
|
function ($attribute, $value, $fail) {
|
|
$lastCode = (new Totp())->GenerateToken(
|
|
Base32::decode(Auth::user()->totp_secret),
|
|
Carbon::now()->subSeconds(30)->timestamp
|
|
);
|
|
$code = (new Totp())->GenerateToken(Base32::decode(Auth::user()->totp_secret));
|
|
|
|
if (!($value === $code || $value === $lastCode)) {
|
|
$fail("Code de vérification $attribute invalide.");
|
|
}
|
|
}
|
|
]
|
|
];
|
|
}
|
|
}
|