Skip to content

Instantly share code, notes, and snippets.

@jeremyquinton
Last active February 28, 2019 14:53
Show Gist options
  • Save jeremyquinton/7d6b1c016c62a19b0d4cbb2e21972291 to your computer and use it in GitHub Desktop.
Save jeremyquinton/7d6b1c016c62a19b0d4cbb2e21972291 to your computer and use it in GitHub Desktop.
<?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