Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
if: contains(matrix.symfony, '@dev')
- run: composer update --no-interaction --no-progress --ansi ${{ matrix.composer_option }}
- run: |
sed -ri 's/"symfony\/(.+)": "(.+)"/"symfony\/\1": "'${{ matrix.symfony }}'"/' composer.json;
sed -ri 's/"symfony\/(config|dependency-injection|form|http-kernel|validator)": "(.+)"/"symfony\/\1": "'${{ matrix.symfony }}'"/' composer.json;
if: matrix.symfony
- run: composer update --no-interaction --no-progress --ansi ${{ matrix.composer_option }}
- name: Run tests
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"symfony/http-client-contracts": "^3.5",
"symfony/phpunit-bridge": "^7.2"
},
"suggest": {
Expand Down
5 changes: 5 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<argument>%beelab_recaptcha2.secret%</argument>
<argument type="service" id="beelab_recaptcha2.google_recaptcha.request_method" />
</service>
<service id="Beelab\Recaptcha2Bundle\Recaptcha\SymfonyClientRequestMethod" public="false">
<call method="setClient">
<argument type="service" id="Symfony\Contracts\HttpClient\HttpClientInterface" on-invalid="ignore"/>
</call>
</service>
<service id="beelab_recaptcha2.type" class="Beelab\Recaptcha2Bundle\Form\Type\RecaptchaType" public="true">
<argument>%beelab_recaptcha2.site_key%</argument>
<tag name="form.type" alias="beelab_recaptcha2" />
Expand Down
11 changes: 8 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,22 @@ beelab_recaptcha2:
If your PHP environment has restrictions about `file_get_contents()` making HTTP requests,
you can use another `RequestMethod` from Google's Recaptcha library.

Currently, this bundle supports the default `Post` and `CurlPost` methods.
You can use the latter by adding in your `config.yml`:
Currently, this bundle supports the default `Post` and `CurlPost` methods, and an additional
method leveraging the [Symfony HTTP Client][1].
You can define it by adding in your configuration:

```yaml
# config/packages/beelab_recaptcha2.yaml

beelab_recaptcha2:
request_method: curl_post
request_method: curl_post # or http_client
```

Otherwise, the default value `post` will be used.

If you want to use the `http_client` request method, you need to require `symfony/http-client`.


## 3. Usage

In your form, use `Beelab\Recaptcha2Bundle\Form\Type\RecaptchaType` form type, as any other Symfony form type.
Expand Down Expand Up @@ -125,3 +129,4 @@ You can add to your `_form_theme.html.twig` file the following lines:
{%- endblock %}
```

[1]: https://symfony.com/doc/current/http_client.html
2 changes: 2 additions & 0 deletions src/DependencyInjection/BeelabRecaptcha2Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Beelab\Recaptcha2Bundle\DependencyInjection;

use Beelab\Recaptcha2Bundle\Recaptcha\SymfonyClientRequestMethod;
use ReCaptcha\RequestMethod\CurlPost;
use ReCaptcha\RequestMethod\Post;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -30,6 +31,7 @@ private function getRequestMethod(string $requestMethod): string
{
return match ($requestMethod) {
'curl_post' => CurlPost::class,
'http_client' => SymfonyClientRequestMethod::class,
default => Post::class,
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode
->children()
->enumNode('request_method')
->values(['curl_post', 'post'])
->values(['curl_post', 'post', 'http_client'])
->defaultValue('post')
->end()
->scalarNode('site_key')
Expand Down
43 changes: 43 additions & 0 deletions src/Recaptcha/SymfonyClientRequestMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Beelab\Recaptcha2Bundle\Recaptcha;

use ReCaptcha\ReCaptcha;
use ReCaptcha\RequestMethod;
use ReCaptcha\RequestParameters;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class SymfonyClientRequestMethod implements RequestMethod
{
private ?HttpClientInterface $client = null;

public function __construct(
private string $siteVerifyUrl = ReCaptcha::SITE_VERIFY_URL,
) {
}

public function setClient(?HttpClientInterface $client): void
{
$this->client = $client;
}

public function submit(RequestParameters $params): string
{
if (null === $this->client) {
throw new \UnexpectedValueException('Needed service is not injected.');
}

$response = $this->client->request('POST', $this->siteVerifyUrl, [
'body' => $params->toQueryString(),
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
]);

if (200 !== $response->getStatusCode()) {
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
}

return $response->getContent();
}
}
51 changes: 51 additions & 0 deletions tests/Recaptcha/SymfonyClientRequestMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Beelab\Recaptcha2Bundle\Tests\Recaptcha;

use Beelab\Recaptcha2Bundle\Recaptcha\SymfonyClientRequestMethod;
use PHPUnit\Framework\TestCase;
use ReCaptcha\RequestParameters;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

final class SymfonyClientRequestMethodTest extends TestCase
{
/** @var \PHPUnit\Framework\MockObject\MockObject|HttpClientInterface */
protected $client;

protected function setUp(): void
{
$this->client = $this->createMock(HttpClientInterface::class);
}

public function testServiceNotInjected(): void
{
$method = new SymfonyClientRequestMethod();
$this->expectException(\UnexpectedValueException::class);

$method->submit(new RequestParameters('', ''));
}

public function testRequestFailure(): void
{
$method = new SymfonyClientRequestMethod();
$method->setClient($this->client);
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->once())->method('getStatusCode')->willReturn(404);
$this->client->expects($this->once())->method('request')->willReturn($response);
$content = $method->submit(new RequestParameters('', ''));
self::assertEquals('{"success": false, "error-codes": ["connection-failed"]}', $content);
}

public function testRequestSuccess(): void
{
$method = new SymfonyClientRequestMethod();
$method->setClient($this->client);
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->once())->method('getStatusCode')->willReturn(200);
$response->expects($this->once())->method('getContent')->willReturn('"OK"');
$this->client->expects($this->once())->method('request')->willReturn($response);
$content = $method->submit(new RequestParameters('', ''));
self::assertEquals('"OK"', $content);
}
}