From d3a4caa0b4e294197322b3e5fa419e6d55e44739 Mon Sep 17 00:00:00 2001 From: Maria Martinez Olivares Date: Wed, 30 Oct 2024 18:06:39 +0100 Subject: [PATCH 1/9] ref #93890 update README for v2 --- README.md | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 41a688f..2478586 100644 --- a/README.md +++ b/README.md @@ -18,25 +18,24 @@ 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 ['all' => true], + //... + ]; ``` ### Step2: Configure the bundle's ```yaml cors_friendly_captcha: - sitekey: here_is_your_sitekey + sitekey: here_is_your_api_key secret: here_is_your_secret use_eu_endpoints: true|false ``` @@ -45,10 +44,10 @@ cors_friendly_captcha: ```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 ``` From 0a00d94b30e836a1e15754fd3c25a281fe8038d3 Mon Sep 17 00:00:00 2001 From: Maria Martinez Olivares Date: Thu, 31 Oct 2024 10:11:35 +0100 Subject: [PATCH 2/9] ref #93890 Upgrade for friendly captcha v2 --- README.md | 6 +++--- src/DependencyInjection/Configuration.php | 4 ++-- src/Validator/FriendlyCaptchaValidValidator.php | 4 +++- .../CORSFriendlyCaptchaExtensionTest.php | 4 ++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2478586..0a53ed6 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,8 @@ return [ ```yaml cors_friendly_captcha: - sitekey: here_is_your_api_key - secret: here_is_your_secret + sitekey: here_is_your_sitekey + secret: here_is_your_api_key use_eu_endpoints: true|false ``` @@ -50,4 +50,4 @@ cors_friendly_captcha: validation: endpoint: https://global.frcapi.com/api/v2/captcha/siteverify eu_endpoint: https://eu.frcapi.com/api/v2/captcha/siteverify -``` +``` \ No newline at end of file diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 34b579b..39c83b4 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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(); diff --git a/src/Validator/FriendlyCaptchaValidValidator.php b/src/Validator/FriendlyCaptchaValidValidator.php index 1bdd975..eda5419 100644 --- a/src/Validator/FriendlyCaptchaValidValidator.php +++ b/src/Validator/FriendlyCaptchaValidValidator.php @@ -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, ], + 'headers' => [ + 'X-API-Key' => $this->secret, + ] ]); $content = $response->getContent(); } catch (\Exception $e) { diff --git a/tests/DependencyInjection/CORSFriendlyCaptchaExtensionTest.php b/tests/DependencyInjection/CORSFriendlyCaptchaExtensionTest.php index 13633f9..ef7b97a 100755 --- a/tests/DependencyInjection/CORSFriendlyCaptchaExtensionTest.php +++ b/tests/DependencyInjection/CORSFriendlyCaptchaExtensionTest.php @@ -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 @@ -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 { From ba946c7d1db434cc39c2dada71e243a8e952f55b Mon Sep 17 00:00:00 2001 From: Maria Martinez Olivares Date: Mon, 4 Nov 2024 14:25:39 +0100 Subject: [PATCH 3/9] ref #93980 Add logic for use_eu_endpoints v2 upgrade --- .../CORSFriendlyCaptchaExtension.php | 4 +- src/DependencyInjection/Configuration.php | 2 +- src/Form/Type/FriendlyCaptchaType.php | 19 +++++----- src/Resources/config/services.yaml | 2 +- src/Resources/views/friendlycaptcha.html.twig | 3 +- tests/Form/Type/FriendlyCaptchaTypeTest.php | 37 ++----------------- 6 files changed, 18 insertions(+), 49 deletions(-) diff --git a/src/DependencyInjection/CORSFriendlyCaptchaExtension.php b/src/DependencyInjection/CORSFriendlyCaptchaExtension.php index d49b17e..18e22c5 100644 --- a/src/DependencyInjection/CORSFriendlyCaptchaExtension.php +++ b/src/DependencyInjection/CORSFriendlyCaptchaExtension.php @@ -16,11 +16,13 @@ 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']) { + if ($configs['use_eu_endpoints'] == true) { + $container->setParameter('cors.friendly_captcha.use_eu_endpoints', true); $puzzleEndpoint = $configs['puzzle']['eu_endpoint']; $verificationEndpoint = $configs['validation']['eu_endpoint']; } else { + $container->setParameter('cors.friendly_captcha.use_eu_endpoints', false); $puzzleEndpoint = $configs['puzzle']['endpoint']; $verificationEndpoint = $configs['validation']['endpoint']; } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 39c83b4..f9587ec 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -19,7 +19,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->children() ->scalarNode('secret')->cannotBeEmpty()->isRequired()->end() ->scalarNode('sitekey')->cannotBeEmpty()->isRequired()->end() - ->booleanNode('use_eu_endpoints')->defaultTrue()->end() + ->booleanNode('use_eu_endpoints')->defaultFalse()->end() ->arrayNode('puzzle') ->addDefaultsIfNotSet() ->children() diff --git a/src/Form/Type/FriendlyCaptchaType.php b/src/Form/Type/FriendlyCaptchaType.php index 4682be0..d77dd12 100644 --- a/src/Form/Type/FriendlyCaptchaType.php +++ b/src/Form/Type/FriendlyCaptchaType.php @@ -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() @@ -30,12 +30,13 @@ 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; } @@ -43,9 +44,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ - 'lang' => null, 'start' => 'focus', - 'callback' => null, 'constraints' => [new FriendlyCaptchaValid()] ]); diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index d29ede2..9e532f3 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -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 } \ No newline at end of file diff --git a/src/Resources/views/friendlycaptcha.html.twig b/src/Resources/views/friendlycaptcha.html.twig index 705ff55..6f7730a 100644 --- a/src/Resources/views/friendlycaptcha.html.twig +++ b/src/Resources/views/friendlycaptcha.html.twig @@ -1,7 +1,6 @@ {%- block cors_friendly_catcha_type_row -%}
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 buildViewWithouOption(): void { $view = new FormView(); @@ -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']); } From 39a0ebb900dd79747d59630a4a2f0a65242bbab3 Mon Sep 17 00:00:00 2001 From: Maria Martinez Olivares Date: Mon, 4 Nov 2024 14:31:26 +0100 Subject: [PATCH 4/9] ref #93890 Cs code --- tests/Form/Type/FriendlyCaptchaTypeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Form/Type/FriendlyCaptchaTypeTest.php b/tests/Form/Type/FriendlyCaptchaTypeTest.php index e255e9e..07d6436 100755 --- a/tests/Form/Type/FriendlyCaptchaTypeTest.php +++ b/tests/Form/Type/FriendlyCaptchaTypeTest.php @@ -62,7 +62,7 @@ public function buildViewWithOptions(): void /** * @test */ - public function buildViewWithouOption(): void + public function buildViewWithoutStartOption(): void { $view = new FormView(); From e929355a129cb2323b94b0690edff2a7b170985b Mon Sep 17 00:00:00 2001 From: Maria Martinez Olivares Date: Wed, 6 Nov 2024 15:18:10 +0100 Subject: [PATCH 5/9] ref #93890 Set use_eu_points to true by default --- .../CORSFriendlyCaptchaExtension.php | 11 +++++------ src/DependencyInjection/Configuration.php | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/DependencyInjection/CORSFriendlyCaptchaExtension.php b/src/DependencyInjection/CORSFriendlyCaptchaExtension.php index 18e22c5..bac8ba3 100644 --- a/src/DependencyInjection/CORSFriendlyCaptchaExtension.php +++ b/src/DependencyInjection/CORSFriendlyCaptchaExtension.php @@ -16,12 +16,11 @@ 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'] == true) { - $container->setParameter('cors.friendly_captcha.use_eu_endpoints', true); - $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']; diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index f9587ec..39c83b4 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -19,7 +19,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->children() ->scalarNode('secret')->cannotBeEmpty()->isRequired()->end() ->scalarNode('sitekey')->cannotBeEmpty()->isRequired()->end() - ->booleanNode('use_eu_endpoints')->defaultFalse()->end() + ->booleanNode('use_eu_endpoints')->defaultTrue()->end() ->arrayNode('puzzle') ->addDefaultsIfNotSet() ->children() From e60b391cbe0003afcd6fe6ee0de077211b6f5cbe Mon Sep 17 00:00:00 2001 From: Maria Martinez Olivares Date: Thu, 7 Nov 2024 14:19:39 +0100 Subject: [PATCH 6/9] ref #93890 Update field name tag --- src/Resources/views/friendlycaptcha.html.twig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Resources/views/friendlycaptcha.html.twig b/src/Resources/views/friendlycaptcha.html.twig index 6f7730a..8780805 100644 --- a/src/Resources/views/friendlycaptcha.html.twig +++ b/src/Resources/views/friendlycaptcha.html.twig @@ -1,6 +1,7 @@ {%- block cors_friendly_catcha_type_row -%}
Date: Thu, 7 Nov 2024 14:51:35 +0100 Subject: [PATCH 7/9] ref #93890 Send response parameter --- src/Validator/FriendlyCaptchaValidValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Validator/FriendlyCaptchaValidValidator.php b/src/Validator/FriendlyCaptchaValidValidator.php index eda5419..fb2bedb 100644 --- a/src/Validator/FriendlyCaptchaValidValidator.php +++ b/src/Validator/FriendlyCaptchaValidValidator.php @@ -49,7 +49,7 @@ public function validate($value, Constraint $constraint) $response = $this->httpClient->request('POST', $this->endpoint, [ 'body' => [ 'sitekey' => $this->sitekey, - 'solution' => $value, + 'response' => $value ], 'headers' => [ 'X-API-Key' => $this->secret, From 28a02f429cf8ea285e8151d049b3985b127a66b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lernon?= Date: Fri, 8 Nov 2024 09:37:19 +0100 Subject: [PATCH 8/9] init v2 release --- README.md | 2 ++ composer.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 41a688f..15a8cd1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/composer.json b/composer.json index 68ec69a..a5dff9b 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,7 @@ }, "extra": { "branch-alias": { - "dev-main": "1.0-dev" + "dev-main": "2.0-dev" }, "symfony": { "allow-contrib": "true" From a4b4cb6692db1d1a94f7625f339b2c56d2de0af3 Mon Sep 17 00:00:00 2001 From: Maria Martinez Olivares Date: Fri, 8 Nov 2024 11:52:16 +0100 Subject: [PATCH 9/9] ref #93890 Add upgrade file --- UPGRADE.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 UPGRADE.md diff --git a/UPGRADE.md b/UPGRADE.md new file mode 100644 index 0000000..1041e7e --- /dev/null +++ b/UPGRADE.md @@ -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