Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout for the reCAPTCHA verification #219

Merged
merged 2 commits into from Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -35,6 +35,7 @@ public function getConfigTreeBuilder()
->scalarNode('locale_key')->defaultValue('%kernel.default_locale%')->end()
->scalarNode('api_host')->defaultValue('www.google.com')->end()
->booleanNode('locale_from_request')->defaultFalse()->end()
->integerNode('timeout')->min(0)->defaultNull()->end()
->arrayNode('trusted_roles')->prototype('scalar')->treatNullLike(array())->end()
->end()
;
Expand Down
13 changes: 12 additions & 1 deletion src/Extension/ReCaptcha/RequestMethod/Post.php
Expand Up @@ -17,14 +17,22 @@ class Post implements RequestMethod
*/
private $recaptchaVerifyUrl;

/**
* The timeout for the reCAPTCHA verification.
*
* @var int|null
*/
private $timeout;

/**
* Constructor
*
* @param string $recaptchaVerifyServer
*/
public function __construct($recaptchaVerifyServer)
public function __construct($recaptchaVerifyServer, $timeout)
{
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
$this->timeout = $timeout;
}

/**
Expand All @@ -51,6 +59,9 @@ public function submit(RequestParameters $params)
$peer_key => 'www.google.com',
),
);
if (null !== $this->timeout) {
$options['http']['timeout'] = $this->timeout;
}
$context = stream_context_create($options);
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
}
Expand Down
13 changes: 12 additions & 1 deletion src/Extension/ReCaptcha/RequestMethod/ProxyPost.php
Expand Up @@ -24,16 +24,24 @@ class ProxyPost implements RequestMethod
*/
private $recaptchaVerifyUrl;

/**
* The timeout for the reCAPTCHA verification.
*
* @var int|null
*/
private $timeout;

/**
* Constructor
*
* @param array $httpProxy proxy data to connect to
* @param string $recaptchaVerifyServer
*/
public function __construct(array $httpProxy, $recaptchaVerifyServer)
public function __construct(array $httpProxy, $recaptchaVerifyServer, $timeout)
{
$this->httpProxy = $httpProxy;
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
$this->timeout = $timeout;
}

/**
Expand Down Expand Up @@ -64,6 +72,9 @@ public function submit(RequestParameters $params)
'request_fulluri' => true,
),
);
if (null !== $this->timeout) {
$options['http']['timeout'] = $this->timeout;
}
$context = stream_context_create($options);
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
}
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Expand Up @@ -31,5 +31,6 @@ services:
- '@?security.authorization_checker'
- '%ewz_recaptcha.trusted_roles%'
- '%ewz_recaptcha.api_host%'
- '%ewz_recaptcha.timeout%'
tags:
- { name: validator.constraint_validator, alias: 'ewz_recaptcha.true' }
16 changes: 13 additions & 3 deletions src/Validator/Constraints/IsTrueValidator.php
Expand Up @@ -69,6 +69,13 @@ class IsTrueValidator extends ConstraintValidator
*/
protected $recaptchaVerifyServer;

/**
* The timeout for the reCAPTCHA verification.
*
* @var int|null
*/
private $timeout;

/**
* @param bool $enabled
* @param string $privateKey
Expand All @@ -78,6 +85,7 @@ class IsTrueValidator extends ConstraintValidator
* @param AuthorizationCheckerInterface|null $authorizationChecker
* @param array $trustedRoles
* @param string $apiHost
* @param int|null $timeout
*/
public function __construct(
$enabled,
Expand All @@ -87,7 +95,8 @@ public function __construct(
$verifyHost,
AuthorizationCheckerInterface $authorizationChecker = null,
array $trustedRoles = array(),
$apiHost = 'www.google.com')
$apiHost = 'www.google.com',
$timeout = null)
{
$this->enabled = $enabled;
$this->privateKey = $privateKey;
Expand All @@ -97,6 +106,7 @@ public function __construct(
$this->authorizationChecker = $authorizationChecker;
$this->trustedRoles = $trustedRoles;
$this->recaptchaVerifyServer = 'https://'.$apiHost;
$this->timeout = $timeout;
}

/**
Expand All @@ -123,9 +133,9 @@ public function validate($value, Constraint $constraint)

// Verify user response with Google
if (null !== $this->httpProxy['host'] && null !== $this->httpProxy['port']) {
$requestMethod = new ProxyPost($this->httpProxy, $this->recaptchaVerifyServer);
$requestMethod = new ProxyPost($this->httpProxy, $this->recaptchaVerifyServer, $this->timeout);
} else {
$requestMethod = new Post($this->recaptchaVerifyServer);
$requestMethod = new Post($this->recaptchaVerifyServer, $this->timeout);
}
$recaptcha = new ReCaptcha($this->privateKey, $requestMethod);
$response = $recaptcha->verify($answer, $remoteip);
Expand Down