ldc-web/app/Http/Requests/ValidateOtpRequest.php

52 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2022-09-21 11:17:58 +00:00
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Carbon;
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()
{
if (!session()->get('tmpUserOTP')) {
return false;
}
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'otp' => [
'string',
'required',
function ($attribute, $value, $fail) {
$user = session()->get('tmpUserOTP');
$lastCode = (new Totp())->GenerateToken(
Base32::decode($user->totp_secret),
Carbon::now()->subSeconds(30)->timestamp
);
$code = (new Totp())->GenerateToken(Base32::decode($user->totp_secret));
if (!($value === $code || $value === $lastCode)) {
$fail("Code de vérification $attribute invalide.");
}
}
]
];
}
}