Recaptcha ok

This commit is contained in:
Jérémy Munsch 2023-02-16 16:59:51 +01:00
parent fd955c15fd
commit 7aac8fcbca
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
7 changed files with 78 additions and 3 deletions

View file

@ -42,3 +42,7 @@ CRM_TOKEN=
PROJECT_UUID=
# Google Analytics
ANALYTICS_GOOGLE_ID=
# Google Recaptcha
GOOGLE_RECAPTCHA_PUBLIC=
GOOGLE_RECAPTCHA_PRIVATE=
GOOGLE_RECAPTCHA_VERSION=

View file

@ -53,7 +53,11 @@ class SecureHeaders
'https://use.fontawesome.com',
'https://fonts.googleapis.com'
]) . ';',
"script-src 'self' $jsDev " . \implode(' ', [
'frame-src ' . implode(' ', [
'https://www.google.com/recaptcha/',
'https://recaptcha.google.com/recaptcha/',
]),
"script-src 'self' {$jsDev} " . \implode(' ', [
'https://www.googletagmanager.com',
'https://cdnjs.cloudflare.com',
"$scheme://www.google-analytics.com",

View file

@ -2,6 +2,7 @@
namespace App\Http\Requests\Fo;
use App\Lib\Helpers\ToolboxHelper;
use Illuminate\Foundation\Http\FormRequest;
class ContactFormRequest extends FormRequest
@ -23,6 +24,7 @@ class ContactFormRequest extends FormRequest
*/
public function rules()
{
ToolboxHelper::validateRecaptcha($this, \config('services.google.recaptcha.version'));
return [
'firstname' => 'required|min:2|max:255',
'lastname' => 'required|min:2|max:255',

View file

@ -4,6 +4,8 @@ namespace App\Lib\Helpers;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
@ -251,4 +253,58 @@ class ToolboxHelper
throw new \RuntimeException('cannot handle $input of type ' . \gettype($input));
}
}
/**
* Validate recaptcha
*
* @param Request $request
* @param boolean $isV3
* @return void
*/
public static function validateRecaptcha(Request $request, bool $isV3 = true)
{
Validator::validate($request->all(), [
'g-recaptcha-response' => [
'required', 'string',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function ($attribute, $value, $fail) use ($isV3) {
try {
$response = Http::asForm()->post('https://www.google.com/recaptcha/api/siteverify', [
'secret' => config('services.google.recaptcha.private'),
'response' => \strval($value),
'remoteip' => app('clientIp')
]);
if ($e = $response->toException()) {
throw $e;
}
if ($isV3) {
if (
!$response->json('success') or
!$response->json('challenge_ts') or
Carbon::parse($response->json('challenge_ts'))->lessThan(\now('utc')->subMinutes(5)) or
$response->json('hostname') !== \parse_url(config('app.url'), \PHP_URL_HOST) or
!$response->json('score')
) {
throw new \RuntimeException();
}
if (\floatval($response->json('score')) < 0.8) {
$fail("Vous n'avez pas obtenu un score assez élevé lors de la validation du recaptcha");
}
return;
}
if (
!$response->json('success') or
!$response->json('challenge_ts') or
Carbon::parse($response->json('challenge_ts'))->lessThan(\now('utc')->subMinutes(5)) or
$response->json('hostname') !== \parse_url(config('app.url'), \PHP_URL_HOST)
) {
throw new \RuntimeException();
}
} catch (\Exception $e) {
$fail('Erreur lors de la validation du formulaire veuillez réssayer');
} //end try
}
]
], [], ['g-recaptcha-response' => 'reCaptcha']);
}
}

View file

@ -2,7 +2,7 @@
/**
* LDC Service Web Cookies
* Version 2.1.1
* Version 2.1.2
*/
return [

View file

@ -30,4 +30,13 @@ return [
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
'google' => [
'recaptcha' => [
'public' => env('GOOGLE_RECAPTCHA_PUBLIC'),
'private' => env('GOOGLE_RECAPTCHA_PRIVATE'),
'version' => env('GOOGLE_RECAPTCHA_VERSION', "3") === "3" ? true : false
]
],
];

@ -1 +1 @@
Subproject commit a6c9aac5aa48090a17ec88918924deb3cbc95cd4
Subproject commit 060f8cb4093875ef391890984567b7d2a8490beb