* fix password reset bug * upgrade to laravel 11 * composer.lock * fix migration issues * use ValidationRule Contract * rename password_resets table * implemented casts as protected function * update env variables * fix optional property * fix validation issues * use <env> on php unit xml * fix pint * cmposer.lock * composer json fixes * fix composer dependencies, remove faker * remove unused class * remove test class * fix default value for mysql migration * linting * expression syntax fix --------- Co-authored-by: Julien Nahum <julien@nahum.net>
52 lines
1.2 KiB
PHP
52 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\ImplicitRule;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class ValidHCaptcha implements ImplicitRule
|
|
{
|
|
public const H_CAPTCHA_VERIFY_URL = 'https://hcaptcha.com/siteverify';
|
|
|
|
private $error = 'Invalid CAPTCHA. Please prove you\'re not a bot.';
|
|
|
|
/**
|
|
* Determine if the validation rule passes.
|
|
*
|
|
* @param string $attribute
|
|
* @param mixed $value
|
|
* @return bool
|
|
*/
|
|
public function passes($attribute, $value)
|
|
{
|
|
if (empty($value)) {
|
|
$this->error = 'Please complete the captcha.';
|
|
|
|
return false;
|
|
}
|
|
|
|
return Http::asForm()->post(self::H_CAPTCHA_VERIFY_URL, [
|
|
'secret' => config('services.h_captcha.secret_key'),
|
|
'response' => $value,
|
|
])->json('success');
|
|
}
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
if(!$this->passes($attribute, $value)) {
|
|
$fail($this->message());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the validation error message.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function message()
|
|
{
|
|
return $this->error;
|
|
}
|
|
}
|