-
-
Save jeremyquinton/7d6b1c016c62a19b0d4cbb2e21972291 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CaptchService { | |
private $client; | |
private $logger; | |
public function __construct(\GuzzleHttp\Client $client,\Log $logger) | |
{ | |
$this->client = $client; | |
$this->logger = $logger; | |
} | |
public function isValidCaptcha($captcha) { | |
$captchaResult = $this->client->request('POST', 'https://www.recaptcha.net/recaptcha/api/siteverify', [ | |
'form_params' => [ | |
'secret' => env('GOOGLE_RECAPTCHA_SECRET_KEY'), | |
'response' => $captcha, | |
// 'remoteip' => '' | |
] | |
]); | |
if (!$captchaResult->getStatusCode() == 200) { | |
return false; | |
} | |
$data = json_decode($captchaResult->getBody()); | |
if (!$data->success || !$data->action == 'login' || $data->score <= 0.6) { | |
if (isset($data->score)) { | |
$this->logger::info($data->score); | |
} | |
return false; | |
} | |
$this->logger::info($data->score); | |
return true; | |
} | |
} | |
//calling code | |
if (env('GOOGLE_RECAPTCHA_SECRET_KEY')) { | |
$captcha = $request->get('grecaptcha'); | |
if (!$this->captchService->isValidCaptcha($captcha)) { | |
return Redirect::back() | |
->with(['message' => trans("Controllers.incorrect_captcha"), 'failed' => true]) | |
->withInput(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment