From 57aa135cafeff74b7d8c8379f90128917b192e3e Mon Sep 17 00:00:00 2001 From: Lupu Gheorghe <46172059+gheorghelupu17@users.noreply.github.com> Date: Tue, 3 Oct 2023 16:51:21 +0300 Subject: [PATCH] Feat/recaptcha (#298) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * error handling * init recaptcha --------- Co-authored-by: Andrei Ioniță --- app/Rules/Recaptcha.php | 34 ++++++++++++++++++++++++++++++++++ config/services.php | 6 ++++++ 2 files changed, 40 insertions(+) create mode 100644 app/Rules/Recaptcha.php diff --git a/app/Rules/Recaptcha.php b/app/Rules/Recaptcha.php new file mode 100644 index 00000000..4bd2f095 --- /dev/null +++ b/app/Rules/Recaptcha.php @@ -0,0 +1,34 @@ +post(config('services.google_recaptcha.url'), [ + 'secret' => config('services.google_recaptcha.secret_key'), + 'response' => $value, + ]) + ->json(); + + $success = (bool) data_get($response, 'success'); + $score = (float) data_get($response, 'score'); + + if (! $success || $score < config('services.google_recaptcha.threshold')) { + $fail('The :attribute field is invalid.'); + } + } +} diff --git a/config/services.php b/config/services.php index 985e5b95..04456d44 100644 --- a/config/services.php +++ b/config/services.php @@ -42,4 +42,10 @@ 'google_maps_api_key' => env('GOOGLE_MAPS_API_KEY'), + 'google_recaptcha' => [ + 'url' => 'https://www.google.com/recaptcha/api/siteverify', + 'site_key' => env('GOOGLE_RECAPTCHA_SITE_KEY'), + 'secret_key' => env('GOOGLE_RECAPTCHA_SECRET_SITE_KEY'), + 'threshold' => max(0.0, min(1.0, floatval(env('GOOGLE_RECAPTCHA_SECRET_SITE_KEY', 0.5)))), + ], ];