diff --git a/src/Http/Middleware/Captcha.php b/src/Http/Middleware/Captcha.php index eb5a6031..befbea09 100644 --- a/src/Http/Middleware/Captcha.php +++ b/src/Http/Middleware/Captcha.php @@ -20,13 +20,21 @@ class Captcha public function handle($request, Closure $next) { - if (config('network.recaptcha.site_key')) { + $captcha = setting('captcha'); + $siteKey = setting('captcha_site_key') ?: config('network.recaptcha.site_key'); + $secretKey = setting('captcha_site_secret') ?: config('network.recaptcha.secret_key'); + + if (is_null($captcha) && $siteKey) { + $captcha = 'recaptcha-v2-invisible'; + } + + if ($captcha == 'recaptcha-v2-invisible' && $siteKey) { $client = new Client(['connect_timeout' => 10, 'timeout' => 10]); $response = $client->post( 'https://www.google.com/recaptcha/api/siteverify', [ 'form_params' => [ - 'secret' => config('network.recaptcha.secret_key'), + 'secret' => $secretKey, 'response' => $request->input('g-recaptcha-response'), ], ] diff --git a/src/Providers/AdminServiceProvider.php b/src/Providers/AdminServiceProvider.php index d54d25eb..2ed57b21 100644 --- a/src/Providers/AdminServiceProvider.php +++ b/src/Providers/AdminServiceProvider.php @@ -293,6 +293,10 @@ function () { // Cookie Consent Settings Setting::make('cookie_consent_enabled')->default(false); Setting::make('cookie_consent_message')->rules(['nullable', 'string']); + + Setting::make('captcha')->rules(['nullable', 'string']); + Setting::make('captcha_site_key')->rules(['nullable', 'string']); + Setting::make('captcha_site_secret')->rules(['nullable', 'string']); } ); } diff --git a/src/Rules/ReCaptchaValidator.php b/src/Rules/ReCaptchaValidator.php index 6a2537a8..bf30b851 100644 --- a/src/Rules/ReCaptchaValidator.php +++ b/src/Rules/ReCaptchaValidator.php @@ -8,10 +8,12 @@ class ReCaptchaValidator { public function validate($attribute, $value, $parameters, $validator): bool { + $secretKey = setting('captcha_site_secret') ?: (config('services.recaptcha.secret') ?: config('network.recaptcha.secret_key')); + $response = Http::asForm()->post( 'https://www.google.com/recaptcha/api/siteverify', [ - 'secret' => config('services.recaptcha.secret'), + 'secret' => $secretKey, 'response' => $value, ] ); diff --git a/src/resources/views/admin/setting/index.blade.php b/src/resources/views/admin/setting/index.blade.php index 4354f32e..79ee552f 100644 --- a/src/resources/views/admin/setting/index.blade.php +++ b/src/resources/views/admin/setting/index.blade.php @@ -120,6 +120,29 @@ ]) }} + +
+
+

{{ __('Captcha') }}

+
+
+ {{ Field::select(__('Captcha'), 'captcha', [ + 'options' => [ + '0' => __('core::translation.none'), + 'recaptcha-v2-invisible' => __('reCAPTCHA v2 Invisible'), + ], + 'value' => [setting('captcha', '0')], + ]) }} + + {{ Field::text(__('Captcha Site Key'), 'captcha_site_key', [ + 'value' => setting('captcha_site_key'), + ]) }} + + {{ Field::text(__('Captcha Site Secret'), 'captcha_site_secret', [ + 'value' => setting('captcha_site_secret'), + ]) }} +
+
diff --git a/src/resources/views/layouts/auth.blade.php b/src/resources/views/layouts/auth.blade.php index edac7ebf..58bd7964 100644 --- a/src/resources/views/layouts/auth.blade.php +++ b/src/resources/views/layouts/auth.blade.php @@ -44,9 +44,16 @@ -@if(config("network.recaptcha.site_key")) +@php + $captcha = setting('captcha'); + $captchaSiteKey = setting('captcha_site_key') ?: config('network.recaptcha.site_key'); + if (is_null($captcha) && $captchaSiteKey) { + $captcha = 'recaptcha-v2-invisible'; + } +@endphp +@if($captcha == 'recaptcha-v2-invisible' && $captchaSiteKey) @endif