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)))), + ], ];