Skip to content

Commit

Permalink
RE #278, #288: Fixed validator with proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
excelwebzone committed Apr 8, 2022
1 parent db5a2a8 commit bcacdbe
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
24 changes: 13 additions & 11 deletions README.md
Expand Up @@ -82,6 +82,19 @@ ewz_recaptcha:
api_host: recaptcha.net
```

You can add HTTP Proxy configuration:

``` yaml
# app/config/config.yml

ewz_recaptcha:
// ...
http_proxy:
host: proxy.mycompany.com
port: 3128
auth: proxy_username:proxy_password
```

#### v2 only Configuration

Sets the default locale:
Expand Down Expand Up @@ -117,18 +130,7 @@ ewz_recaptcha:
// ...
ajax: true
```
You can add HTTP Proxy configuration:

``` yaml
# app/config/config.yml

ewz_recaptcha:
// ...
http_proxy:
host: proxy.mycompany.com
port: 3128
auth: proxy_username:proxy_password
```
In case you have turned off the domain name checking on reCAPTCHA's end, you'll need to check the origin of the response by enabling the ``verify_host`` option:

``` yaml
Expand Down
6 changes: 5 additions & 1 deletion src/Extension/ReCaptcha/RequestMethod/ProxyPost.php
Expand Up @@ -63,14 +63,18 @@ public function submit(RequestParameters $params): string
return $this->cache[$cacheKey];
}

$proxyAuth = !empty($this->httpProxy['auth'])
? sprintf('Proxy-Authorization: Basic %s', base64_encode($this->httpProxy['auth']))
: null;

/**
* PHP 5.6.0 changed the way you specify the peer name for SSL context options.
* Using "CN_name" will still work, but it will raise deprecated errors.
*/
$peerKey = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n".sprintf('Proxy-Authorization: Basic %s', base64_encode($this->httpProxy['auth'])),
'header' => sprintf("Content-type: application/x-www-form-urlencoded\r\n%s", $proxyAuth),
'method' => 'POST',
'content' => $params->toQueryString(),
// Force the peer to validate (not needed in 5.6.0+, but still works)
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.yml
Expand Up @@ -70,8 +70,8 @@ services:
public: true
arguments:
- '%ewz_recaptcha.enabled%'
- '%ewz_recaptcha.private_key%'
- '%ewz_recaptcha.score_threshold%'
- '@ewz_recaptcha.recaptcha'
- '@request_stack'
- '@logger'
tags:
Expand Down
10 changes: 4 additions & 6 deletions src/Validator/Constraints/IsTrueValidatorV3.php
Expand Up @@ -32,21 +32,21 @@ class IsTrueValidatorV3 extends ConstraintValidator
* ContainsRecaptchaValidator constructor.
*
* @param bool $enabled
* @param string $secretKey
* @param float $scoreThreshold
* @param ReCaptcha $scoreThreshold
* @param RequestStack $requestStack
* @param LoggerInterface $logger
*/
public function __construct(
bool $enabled,
string $secretKey,
float $scoreThreshold,
ReCaptcha $reCaptcha,
RequestStack $requestStack,
LoggerInterface $logger
) {
$this->enabled = $enabled;
$this->secretKey = $secretKey;
$this->scoreThreshold = $scoreThreshold;
$this->reCaptcha = $reCaptcha;
$this->requestStack = $requestStack;
$this->logger = $logger;
}
Expand Down Expand Up @@ -91,9 +91,7 @@ private function isTokenValid(string $token): bool
$remoteIp = $this->requestStack->getCurrentRequest()->getClientIp();
$action = $this->getActionName();

$recaptcha = new ReCaptcha($this->secretKey);

$response = $recaptcha
$response = $this->reCaptcha
->setExpectedAction($action)
->setScoreThreshold($this->scoreThreshold)
->verify($token, $remoteIp);
Expand Down
10 changes: 7 additions & 3 deletions tests/Validator/Constraints/IsTrueValidatorV3Test.php
Expand Up @@ -7,6 +7,7 @@
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrueValidatorV3;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use ReCaptcha\ReCaptcha;
use stdClass;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraint;
Expand All @@ -18,6 +19,7 @@ class IsTrueValidatorV3Test extends TestCase

public function testNotEnabledDoesNotValidate(): void
{
$reCaptcha = $this->createMock(ReCaptcha::class);
$requestStack = $this->createMock(RequestStack::class);
$logger = $this->createMock(LoggerInterface::class);
$context = $this->createMock(ExecutionContextInterface::class);
Expand All @@ -26,13 +28,14 @@ public function testNotEnabledDoesNotValidate(): void
$context->expects(self::never())
->method('buildViolation');

$validator = new IsTrueValidatorV3(false, 'secret', 0.1, $requestStack, $logger);
$validator = new IsTrueValidatorV3(false, 0.1, $reCaptcha, $requestStack, $logger);
$validator->initialize($context);
$validator->validate('', $this->createMock(Constraint::class));
}

public function testRequiresV3(): void
{
$reCaptcha = $this->createMock(ReCaptcha::class);
$requestStack = $this->createMock(RequestStack::class);
$logger = $this->createMock(LoggerInterface::class);
$context = $this->createMock(ExecutionContextInterface::class);
Expand All @@ -44,13 +47,14 @@ public function testRequiresV3(): void
$this->expectException(UnexpectedTypeException::class);
$this->expectExceptionMessage('Expected argument of type "EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrueV3",');

$validator = new IsTrueValidatorV3(true, 'secret', 0.1, $requestStack, $logger);
$validator = new IsTrueValidatorV3(true, 0.1, $reCaptcha, $requestStack, $logger);
$validator->initialize($context);
$validator->validate('', $this->createMock(IsTrue::class));
}

public function testRequiresValueNotNullButNotString(): void
{
$reCaptcha = $this->createMock(ReCaptcha::class);
$requestStack = $this->createMock(RequestStack::class);
$logger = $this->createMock(LoggerInterface::class);
$context = $this->createMock(ExecutionContextInterface::class);
Expand All @@ -62,7 +66,7 @@ public function testRequiresValueNotNullButNotString(): void
$this->expectException(UnexpectedTypeException::class);
$this->expectExceptionMessage('Expected argument of type "string", "stdClass" given');

$validator = new IsTrueValidatorV3(true, 'secret', 0.1, $requestStack, $logger);
$validator = new IsTrueValidatorV3(true, 0.1, $reCaptcha, $requestStack, $logger);
$validator->initialize($context);
$validator->validate(new stdClass(), new IsTrueV3());
}
Expand Down

0 comments on commit bcacdbe

Please sign in to comment.