52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Security;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class RecaptchaVerifier
|
|
{
|
|
public function isEnabled(): bool
|
|
{
|
|
return (bool) config('services.recaptcha.enabled', false);
|
|
}
|
|
|
|
public function verify(string $token, ?string $ip = null): bool
|
|
{
|
|
if (! $this->isEnabled()) {
|
|
return true;
|
|
}
|
|
|
|
$secret = (string) config('services.recaptcha.secret', '');
|
|
if ($secret === '' || $token === '') {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
/** @var \Illuminate\Http\Client\Response $response */
|
|
$response = Http::asForm()
|
|
->timeout((int) config('services.recaptcha.timeout', 5))
|
|
->post((string) config('services.recaptcha.verify_url'), [
|
|
'secret' => $secret,
|
|
'response' => $token,
|
|
'remoteip' => $ip,
|
|
]);
|
|
|
|
if ($response->status() < 200 || $response->status() >= 300) {
|
|
return false;
|
|
}
|
|
|
|
$payload = json_decode((string) $response->body(), true);
|
|
|
|
return (bool) data_get(is_array($payload) ? $payload : [], 'success', false);
|
|
} catch (\Throwable $e) {
|
|
Log::warning('recaptcha verification request failed', [
|
|
'message' => $e->getMessage(),
|
|
]);
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|