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
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This bundle provides easy [friendlycaptcha.com](https://www.friendlycaptcha.com)

This is a fork of the [cors][https://github.com/cors-gmbh/friendly-captcha-bundle] bundle, that does not seems to be maintened, [see this PR for details][https://github.com/cors-gmbh/friendly-captcha-bundle/pull/3]

The main branch works with the v2 API, see https://developer.friendlycaptcha.com/docs/v2/guides/upgrading-from-v1/backend-integration for migration instruction or use the main_v1 / v1.x version of the bundle.

## Installation

### Step 1: Use composer and enable Bundle
Expand All @@ -18,37 +20,36 @@ php composer.phar require elma/friendly-captcha-bundle
```

Now, Composer will automatically download all required files, and install them
for you. All that is left to do is to update your ``AppKernel.php`` file, and
for you. All that is left to do is to update your ``bundles.php`` file, and
register the new bundle:

```php
<?php

// in AppKernel::registerBundles()
$bundles = array(
// ...
new CORS\Bundle\FriendlyCaptchaBundle\CORSFriendlyCaptchaBundle(),
// ...
);
// in config/bundles.php
return [
CORS\Bundle\FriendlyCaptchaBundle\CORSFriendlyCaptchaBundle::class => ['all' => true],
//...
];
```

### Step2: Configure the bundle's

```yaml
cors_friendly_captcha:
sitekey: here_is_your_sitekey
secret: here_is_your_secret
secret: here_is_your_api_key
use_eu_endpoints: true|false
```

#### Optionally, change endpoints

```yaml
cors_friendly_captcha:
puzzle:
puzzle:
endpoint: https://api.friendlycaptcha.com/api/v1/puzzle
eu_endpoint: https://eu-api.friendlycaptcha.eu/api/v1/puzzle
validation:
endpoint: https://api.friendlycaptcha.com/api/v1/siteverify
eu_endpoint: https://eu-api.friendlycaptcha.eu/api/v1/siteverify
```
validation:
endpoint: https://global.frcapi.com/api/v2/captcha/siteverify
eu_endpoint: https://eu.frcapi.com/api/v2/captcha/siteverify
```
15 changes: 15 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
UPGRADE FROM 1.x to 2.x
===

## Introduction

The v2 of this bundle is based upon the Friendly Captcha v2 version: https://developer.friendlycaptcha.com/docs/v2

#### Breaking changes

* Removed the form options: `lang`, `callback`, `puzzle-endpoint` and `lang` .

#### Requirements

* To activate the v2 a new api key is necessary: https://developer.friendlycaptcha.com/docs/v2/guides/upgrading-from-v1/backend-integration#3-replace-the-secret-field
* For a v2 migration front guide: https://developer.friendlycaptcha.com/docs/v2/guides/upgrading-from-v1/script
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"extra": {
"branch-alias": {
"dev-main": "1.0-dev"
"dev-main": "2.0-dev"
},
"symfony": {
"allow-contrib": "true"
Expand Down
11 changes: 6 additions & 5 deletions src/DependencyInjection/CORSFriendlyCaptchaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ public function load(array $configs, ContainerBuilder $container): void
$configs = $this->processConfiguration($this->getConfiguration([], $container), $configs);
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

if ($configs['use_eu_endpoints']) {
$puzzleEndpoint = $configs['puzzle']['eu_endpoint'];
$verificationEndpoint = $configs['validation']['eu_endpoint'];
}
else {
$container->setParameter('cors.friendly_captcha.use_eu_endpoints', true);
$puzzleEndpoint = $configs['puzzle']['eu_endpoint'];
$verificationEndpoint = $configs['validation']['eu_endpoint'];

if (isset($configs['use_eu_endpoints']) && $configs['use_eu_endpoints'] == false){
$container->setParameter('cors.friendly_captcha.use_eu_endpoints', false);
$puzzleEndpoint = $configs['puzzle']['endpoint'];
$verificationEndpoint = $configs['validation']['endpoint'];
}
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function getConfigTreeBuilder(): TreeBuilder
->arrayNode('validation')
->addDefaultsIfNotSet()
->children()
->scalarNode('endpoint')->defaultValue('https://api.friendlycaptcha.com/api/v1/siteverify')->end()
->scalarNode('eu_endpoint')->defaultValue('https://eu-api.friendlycaptcha.eu/api/v1/siteverify')->end()
->scalarNode('endpoint')->defaultValue('https://global.frcapi.com/api/v2/captcha/siteverify')->end()
->scalarNode('eu_endpoint')->defaultValue('https://eu.frcapi.com/api/v2/captcha/siteverify')->end()
->end()
->end()
->end();
Expand Down
19 changes: 9 additions & 10 deletions src/Form/Type/FriendlyCaptchaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

final class FriendlyCaptchaType extends AbstractType
{
protected $sitekey;
protected $endpoint;
protected string $sitekey;
protected bool $useEuEndpoints;

public function __construct(string $sitekey, string $endpoint)
public function __construct(string $sitekey, bool $useEuEndpoints)
{
$this->sitekey = $sitekey;
$this->endpoint = $endpoint;
$this->useEuEndpoints = $useEuEndpoints;
}

public function getParent()
Expand All @@ -30,22 +30,21 @@ public function getParent()
public function buildView(FormView $view, FormInterface $form, array $options)
{
$fcValues = array_filter([
'puzzle-endpoint' => $this->endpoint,
'lang' => $options['lang'] ?? null,
'start' => $options['start'] ?? null,
'callback' => $options['callback'] ?? null,
'start' => $options['start'] ?? null
]);

if ($this->useEuEndpoints == true) {
$fcValues['api-endpoint'] = 'eu';
}

$view->vars['sitekey'] = $this->sitekey;
$view->vars['friendly_captcha'] = $fcValues;
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'lang' => null,
'start' => 'focus',
'callback' => null,
'constraints' => [new FriendlyCaptchaValid()]
]);

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ services:
CORS\Bundle\FriendlyCaptchaBundle\Form\Type\FriendlyCaptchaType:
arguments:
- '%cors.friendly_captcha.sitekey%'
- '%cors.friendly_captcha.endpoint.puzzle%'
- '%cors.friendly_captcha.use_eu_endpoints%'
tags:
- { name: form.type }
2 changes: 1 addition & 1 deletion src/Resources/views/friendlycaptcha.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{%- block cors_friendly_catcha_type_row -%}
<div class="frc-captcha"
data-sitekey="{{ form.vars.sitekey }}"
data-solution-field-name="{{ form.vars.full_name }}"
data-form-field-name="{{ form.vars.full_name }}"
{% for type, value in form.vars.friendly_captcha %}
data-{{ type }}="{{ value }}"
{% endfor %}
Expand Down
6 changes: 4 additions & 2 deletions src/Validator/FriendlyCaptchaValidValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ public function validate($value, Constraint $constraint)
try {
$response = $this->httpClient->request('POST', $this->endpoint, [
'body' => [
'secret' => $this->secret,
'sitekey' => $this->sitekey,
'solution' => $value,
'response' => $value
],
'headers' => [
'X-API-Key' => $this->secret,
]
]);
$content = $response->getContent();
} catch (\Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testSimpleEuConfiguration(): void
$this->assertParameter('secret', 'cors.friendly_captcha.secret');
$this->assertParameter('sitekey', 'cors.friendly_captcha.sitekey');
$this->assertParameter('https://eu-api.friendlycaptcha.eu/api/v1/puzzle', 'cors.friendly_captcha.endpoint.puzzle');
$this->assertParameter('https://eu-api.friendlycaptcha.eu/api/v1/siteverify', 'cors.friendly_captcha.endpoint.validation');
$this->assertParameter('https://eu.frcapi.com/api/v2/captcha/siteverify', 'cors.friendly_captcha.endpoint.validation');
}

public function testSimpleComConfiguration(): void
Expand All @@ -53,7 +53,7 @@ public function testSimpleComConfiguration(): void
$this->assertParameter('secret', 'cors.friendly_captcha.secret');
$this->assertParameter('sitekey', 'cors.friendly_captcha.sitekey');
$this->assertParameter('https://api.friendlycaptcha.com/api/v1/puzzle', 'cors.friendly_captcha.endpoint.puzzle');
$this->assertParameter('https://api.friendlycaptcha.com/api/v1/siteverify', 'cors.friendly_captcha.endpoint.validation');
$this->assertParameter('https://global.frcapi.com/api/v2/captcha/siteverify', 'cors.friendly_captcha.endpoint.validation');
}
public function testCustomEndpointConfiguration(): void
{
Expand Down
37 changes: 3 additions & 34 deletions tests/Form/Type/FriendlyCaptchaTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,20 @@ public function buildViewWithOptions(): void
$this->assertArrayNotHasKey('sitekey', $view->vars);

$this->type->buildView($view, $form, [
'lang' => 'de',
'start' => 'none',
'callback' => 'globalThis.loadForm'
'start' => 'none'
]);

$this->assertArrayHasKey('sitekey', $view->vars);
$this->assertArrayHasKey('friendly_captcha', $view->vars);
$this->assertSame([
'lang' => 'de',
'start' => 'none',
'callback' => 'globalThis.loadForm'
'start' => 'none'
], $view->vars['friendly_captcha']);
}

/**
* @test
*/
public function buildViewWithoutLangOption(): void
public function buildViewWithoutStartOption(): void
{
$view = new FormView();

Expand All @@ -76,38 +72,11 @@ public function buildViewWithoutLangOption(): void
$this->assertArrayNotHasKey('sitekey', $view->vars);

$this->type->buildView($view, $form, [
'start' => 'none',
'callback' => 'globalThis.loadForm'
]);

$this->assertArrayHasKey('sitekey', $view->vars);
$this->assertArrayHasKey('friendly_captcha', $view->vars);
$this->assertSame([
'start' => 'none',
'callback' => 'globalThis.loadForm'
], $view->vars['friendly_captcha']);
}

/**
* @test
*/
public function buildViewWithoutCallbackAndStartOption(): void
{
$view = new FormView();

/** @var FormInterface $form */
$form = $this->createMock(FormInterface::class);

$this->assertArrayNotHasKey('sitekey', $view->vars);

$this->type->buildView($view, $form, [
'lang' => 'de',
]);

$this->assertArrayHasKey('sitekey', $view->vars);
$this->assertArrayHasKey('friendly_captcha', $view->vars);
$this->assertSame([
'lang' => 'de',
], $view->vars['friendly_captcha']);
}

Expand Down
Loading