Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

Commit bcacdbe

Browse files
committed
RE #278, #288: Fixed validator with proxy
1 parent db5a2a8 commit bcacdbe

File tree

5 files changed

+30
-22
lines changed

5 files changed

+30
-22
lines changed

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ ewz_recaptcha:
8282
api_host: recaptcha.net
8383
```
8484
85+
You can add HTTP Proxy configuration:
86+
87+
``` yaml
88+
# app/config/config.yml
89+
90+
ewz_recaptcha:
91+
// ...
92+
http_proxy:
93+
host: proxy.mycompany.com
94+
port: 3128
95+
auth: proxy_username:proxy_password
96+
```
97+
8598
#### v2 only Configuration
8699
87100
Sets the default locale:
@@ -117,18 +130,7 @@ ewz_recaptcha:
117130
// ...
118131
ajax: true
119132
```
120-
You can add HTTP Proxy configuration:
121-
122-
``` yaml
123-
# app/config/config.yml
124133
125-
ewz_recaptcha:
126-
// ...
127-
http_proxy:
128-
host: proxy.mycompany.com
129-
port: 3128
130-
auth: proxy_username:proxy_password
131-
```
132134
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:
133135
134136
``` yaml

src/Extension/ReCaptcha/RequestMethod/ProxyPost.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,18 @@ public function submit(RequestParameters $params): string
6363
return $this->cache[$cacheKey];
6464
}
6565

66+
$proxyAuth = !empty($this->httpProxy['auth'])
67+
? sprintf('Proxy-Authorization: Basic %s', base64_encode($this->httpProxy['auth']))
68+
: null;
69+
6670
/**
6771
* PHP 5.6.0 changed the way you specify the peer name for SSL context options.
6872
* Using "CN_name" will still work, but it will raise deprecated errors.
6973
*/
7074
$peerKey = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
7175
$options = array(
7276
'http' => array(
73-
'header' => "Content-type: application/x-www-form-urlencoded\r\n".sprintf('Proxy-Authorization: Basic %s', base64_encode($this->httpProxy['auth'])),
77+
'header' => sprintf("Content-type: application/x-www-form-urlencoded\r\n%s", $proxyAuth),
7478
'method' => 'POST',
7579
'content' => $params->toQueryString(),
7680
// Force the peer to validate (not needed in 5.6.0+, but still works)

src/Resources/config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ services:
7070
public: true
7171
arguments:
7272
- '%ewz_recaptcha.enabled%'
73-
- '%ewz_recaptcha.private_key%'
7473
- '%ewz_recaptcha.score_threshold%'
74+
- '@ewz_recaptcha.recaptcha'
7575
- '@request_stack'
7676
- '@logger'
7777
tags:

src/Validator/Constraints/IsTrueValidatorV3.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ class IsTrueValidatorV3 extends ConstraintValidator
3232
* ContainsRecaptchaValidator constructor.
3333
*
3434
* @param bool $enabled
35-
* @param string $secretKey
3635
* @param float $scoreThreshold
36+
* @param ReCaptcha $scoreThreshold
3737
* @param RequestStack $requestStack
3838
* @param LoggerInterface $logger
3939
*/
4040
public function __construct(
4141
bool $enabled,
42-
string $secretKey,
4342
float $scoreThreshold,
43+
ReCaptcha $reCaptcha,
4444
RequestStack $requestStack,
4545
LoggerInterface $logger
4646
) {
4747
$this->enabled = $enabled;
48-
$this->secretKey = $secretKey;
4948
$this->scoreThreshold = $scoreThreshold;
49+
$this->reCaptcha = $reCaptcha;
5050
$this->requestStack = $requestStack;
5151
$this->logger = $logger;
5252
}
@@ -91,9 +91,7 @@ private function isTokenValid(string $token): bool
9191
$remoteIp = $this->requestStack->getCurrentRequest()->getClientIp();
9292
$action = $this->getActionName();
9393

94-
$recaptcha = new ReCaptcha($this->secretKey);
95-
96-
$response = $recaptcha
94+
$response = $this->reCaptcha
9795
->setExpectedAction($action)
9896
->setScoreThreshold($this->scoreThreshold)
9997
->verify($token, $remoteIp);

tests/Validator/Constraints/IsTrueValidatorV3Test.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrueValidatorV3;
88
use PHPUnit\Framework\TestCase;
99
use Psr\Log\LoggerInterface;
10+
use ReCaptcha\ReCaptcha;
1011
use stdClass;
1112
use Symfony\Component\HttpFoundation\RequestStack;
1213
use Symfony\Component\Validator\Constraint;
@@ -18,6 +19,7 @@ class IsTrueValidatorV3Test extends TestCase
1819

1920
public function testNotEnabledDoesNotValidate(): void
2021
{
22+
$reCaptcha = $this->createMock(ReCaptcha::class);
2123
$requestStack = $this->createMock(RequestStack::class);
2224
$logger = $this->createMock(LoggerInterface::class);
2325
$context = $this->createMock(ExecutionContextInterface::class);
@@ -26,13 +28,14 @@ public function testNotEnabledDoesNotValidate(): void
2628
$context->expects(self::never())
2729
->method('buildViolation');
2830

29-
$validator = new IsTrueValidatorV3(false, 'secret', 0.1, $requestStack, $logger);
31+
$validator = new IsTrueValidatorV3(false, 0.1, $reCaptcha, $requestStack, $logger);
3032
$validator->initialize($context);
3133
$validator->validate('', $this->createMock(Constraint::class));
3234
}
3335

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

47-
$validator = new IsTrueValidatorV3(true, 'secret', 0.1, $requestStack, $logger);
50+
$validator = new IsTrueValidatorV3(true, 0.1, $reCaptcha, $requestStack, $logger);
4851
$validator->initialize($context);
4952
$validator->validate('', $this->createMock(IsTrue::class));
5053
}
5154

5255
public function testRequiresValueNotNullButNotString(): void
5356
{
57+
$reCaptcha = $this->createMock(ReCaptcha::class);
5458
$requestStack = $this->createMock(RequestStack::class);
5559
$logger = $this->createMock(LoggerInterface::class);
5660
$context = $this->createMock(ExecutionContextInterface::class);
@@ -62,7 +66,7 @@ public function testRequiresValueNotNullButNotString(): void
6266
$this->expectException(UnexpectedTypeException::class);
6367
$this->expectExceptionMessage('Expected argument of type "string", "stdClass" given');
6468

65-
$validator = new IsTrueValidatorV3(true, 'secret', 0.1, $requestStack, $logger);
69+
$validator = new IsTrueValidatorV3(true, 0.1, $reCaptcha, $requestStack, $logger);
6670
$validator->initialize($context);
6771
$validator->validate(new stdClass(), new IsTrueV3());
6872
}

0 commit comments

Comments
 (0)