From d2809f1dc4bad4d00613398dd0db14d74335ad7a Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Mon, 20 Feb 2017 13:13:57 +0000 Subject: [PATCH 01/12] Update Changelog --- CHANGELOG.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd34d80..d6114ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,13 +2,12 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 2.7.0 - 2017-02-20 -### Added +## 2.7.1 - TBD -- [#29](https://github.com/zendframework/zend-captcha/pull/29) adds support for - zend-recaptch v3. +### Added +- Nothing. ### Deprecated @@ -22,11 +21,13 @@ All notable changes to this project will be documented in this file, in reverse - Nothing. -## 2.6.1 - TBD +## 2.7.0 - 2017-02-20 ### Added -- Nothing. +- [#29](https://github.com/zendframework/zend-captcha/pull/29) adds support for + zend-recaptch v3. + ### Deprecated From 6076a48e4d54de12c66cee26b5bea12d4fa66f4c Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Wed, 22 Feb 2017 10:09:28 +0000 Subject: [PATCH 02/12] Support validation when value contains response If the user passes in the response value directly to isValid(), e.g. by taking the `g-recaptcha-response` value directly, then this should work! Fixes #30 --- phpunit.xml.dist | 4 ++++ src/ReCaptcha.php | 5 ++++- test/ReCaptchaTest.php | 38 ++++++++++++++++++++++++++++++-------- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 73980df..093148e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -27,6 +27,10 @@ + + + + diff --git a/src/ReCaptcha.php b/src/ReCaptcha.php index 722fcf3..d50dec6 100644 --- a/src/ReCaptcha.php +++ b/src/ReCaptcha.php @@ -260,8 +260,11 @@ public function isValid($value, $context = null) } $service = $this->getService(); + if (array_key_exists($value, $context)) { + $value = $context[$value]; + } - $res = $service->verify($context[$value]); + $res = $service->verify($value); if (! $res) { $this->error(self::ERR_CAPTCHA); return false; diff --git a/test/ReCaptchaTest.php b/test/ReCaptchaTest.php index 7ec57c8..a2e590f 100644 --- a/test/ReCaptchaTest.php +++ b/test/ReCaptchaTest.php @@ -28,14 +28,6 @@ public function setUp() if (! getenv('TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT')) { $this->markTestSkipped('Enable TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT to test PDF render'); } - - if (isset($this->word)) { - unset($this->word); - } - - $this->captcha = new ReCaptcha([ - 'sessionClass' => 'ZendTest\Captcha\TestAsset\SessionContainer' - ]); } public function testConstructorShouldSetOptions() @@ -163,4 +155,34 @@ public function testUsesReCaptchaHelper() $captcha = new ReCaptcha; $this->assertEquals('captcha/recaptcha', $captcha->getHelperName()); } + + public function testValidationForDifferentElementName() + { + $captcha = new ReCaptcha([ + 'site_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SITE_KEY'), + 'secret_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SECRET_KEY'), + ]); + $captcha->getService()->setIp('127.0.0.1'); + + $response = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE'); + $value = 'g-recaptcha-response'; + $context = ['g-recaptcha-response' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE')]; + + $this->assertTrue($captcha->isValid($value, $context)); + } + + public function testValidationForResponseElementName() + { + $captcha = new ReCaptcha([ + 'site_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SITE_KEY'), + 'secret_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SECRET_KEY'), + ]); + $captcha->getService()->setIp('127.0.0.1'); + + $response = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE'); + $value = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE'); + $context = ['g-recaptcha-response' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE')]; + + $this->assertTrue($captcha->isValid($value, $context)); + } } From 308b949e0a5484fdf86ac223d22b66e7acfc1fca Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Wed, 22 Feb 2017 10:26:20 +0000 Subject: [PATCH 03/12] Use TLS transport for Travis PHP 5.6 --- test/ReCaptchaTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/ReCaptchaTest.php b/test/ReCaptchaTest.php index a2e590f..31fb0fa 100644 --- a/test/ReCaptchaTest.php +++ b/test/ReCaptchaTest.php @@ -10,6 +10,8 @@ namespace ZendTest\Captcha; use Zend\Captcha\ReCaptcha; +use Zend\Http\Client as HttpClient; +use Zend\Http\Client\Adapter\Socket; use ZendService\ReCaptcha\ReCaptcha as ReCaptchaService; /** @@ -163,6 +165,7 @@ public function testValidationForDifferentElementName() 'secret_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SECRET_KEY'), ]); $captcha->getService()->setIp('127.0.0.1'); + $captcha->getService()->setHttpClient($this->getHttpClient()); $response = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE'); $value = 'g-recaptcha-response'; @@ -178,6 +181,7 @@ public function testValidationForResponseElementName() 'secret_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SECRET_KEY'), ]); $captcha->getService()->setIp('127.0.0.1'); + $captcha->getService()->setHttpClient($this->getHttpClient()); $response = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE'); $value = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE'); @@ -185,4 +189,15 @@ public function testValidationForResponseElementName() $this->assertTrue($captcha->isValid($value, $context)); } + + private function getHttpClient() + { + $socket = new Socket(); + $socket->setOptions([ + 'ssltransport' => 'tls', + ]); + return new HttpClient(null, [ + 'adapter' => $socket, + ]); + } } From bd6f09d8508b426c55c00039f4f97ff68c76630b Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Wed, 22 Feb 2017 11:22:28 +0000 Subject: [PATCH 04/12] Document the test keys in oh-unit.xml.dist --- phpunit.xml.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 093148e..2b314b3 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -28,6 +28,7 @@ include_path or via Composer. --> + From ef127f31b89c238b93cf683809e52b1902d31a64 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Wed, 22 Feb 2017 11:25:20 +0000 Subject: [PATCH 05/12] =?UTF-8?q?Don=E2=80=99t=20repurpose=20value=20when?= =?UTF-8?q?=20extracting=20response=20from=20context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This avoids having a semantic change in the meaning of $value. --- src/ReCaptcha.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ReCaptcha.php b/src/ReCaptcha.php index d50dec6..916c21f 100644 --- a/src/ReCaptcha.php +++ b/src/ReCaptcha.php @@ -261,10 +261,11 @@ public function isValid($value, $context = null) $service = $this->getService(); if (array_key_exists($value, $context)) { - $value = $context[$value]; + $res = $service->verify($context[$value]); + } else { + $res = $service->verify($value); } - $res = $service->verify($value); if (! $res) { $this->error(self::ERR_CAPTCHA); return false; From b9be365cf3df62ac68287f8fb46b8d8158a4ad49 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Wed, 22 Feb 2017 11:26:29 +0000 Subject: [PATCH 06/12] Ensure that value is a string or integer This makes sure that we can use it with array_key_exists. --- src/ReCaptcha.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ReCaptcha.php b/src/ReCaptcha.php index 916c21f..cbfbac7 100644 --- a/src/ReCaptcha.php +++ b/src/ReCaptcha.php @@ -260,7 +260,7 @@ public function isValid($value, $context = null) } $service = $this->getService(); - if (array_key_exists($value, $context)) { + if ((is_string($value) || is_int($value)) && array_key_exists($value, $context)) { $res = $service->verify($context[$value]); } else { $res = $service->verify($value); From 98cb25bd9ff49d25585009557d16000520d15954 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Wed, 22 Feb 2017 11:33:48 +0000 Subject: [PATCH 07/12] Add return type to docblock in ReCaptchTest --- test/ReCaptchaTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/ReCaptchaTest.php b/test/ReCaptchaTest.php index 31fb0fa..2c80f7e 100644 --- a/test/ReCaptchaTest.php +++ b/test/ReCaptchaTest.php @@ -190,6 +190,9 @@ public function testValidationForResponseElementName() $this->assertTrue($captcha->isValid($value, $context)); } + /** + * @return HttpClient + */ private function getHttpClient() { $socket = new Socket(); From 7594b57f8cb5e34e2b61a54dd9561dc059d29e8f Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Wed, 22 Feb 2017 11:34:01 +0000 Subject: [PATCH 08/12] Avoid multiple calls to getService() --- test/ReCaptchaTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/ReCaptchaTest.php b/test/ReCaptchaTest.php index 2c80f7e..1bc47c3 100644 --- a/test/ReCaptchaTest.php +++ b/test/ReCaptchaTest.php @@ -164,8 +164,9 @@ public function testValidationForDifferentElementName() 'site_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SITE_KEY'), 'secret_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SECRET_KEY'), ]); - $captcha->getService()->setIp('127.0.0.1'); - $captcha->getService()->setHttpClient($this->getHttpClient()); + $service = $captcha->getService(); + $service->setIp('127.0.0.1'); + $service->setHttpClient($this->getHttpClient()); $response = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE'); $value = 'g-recaptcha-response'; @@ -180,8 +181,9 @@ public function testValidationForResponseElementName() 'site_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SITE_KEY'), 'secret_key' => getenv('TESTS_ZEND_SERVICE_RECAPTCHA_SECRET_KEY'), ]); - $captcha->getService()->setIp('127.0.0.1'); - $captcha->getService()->setHttpClient($this->getHttpClient()); + $service = $captcha->getService(); + $service->setIp('127.0.0.1'); + $service->setHttpClient($this->getHttpClient()); $response = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE'); $value = getenv('TESTS_ZEND_SERVICE_RECAPTCHA_RESPONSE'); From 64346d0b5d89d858e52cc9d41e237dbaef989035 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Wed, 22 Feb 2017 16:48:28 +0000 Subject: [PATCH 09/12] Fix skipped tests message --- test/ReCaptchaTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ReCaptchaTest.php b/test/ReCaptchaTest.php index 1bc47c3..c51e89d 100644 --- a/test/ReCaptchaTest.php +++ b/test/ReCaptchaTest.php @@ -28,7 +28,7 @@ class ReCaptchaTest extends \PHPUnit_Framework_TestCase public function setUp() { if (! getenv('TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT')) { - $this->markTestSkipped('Enable TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT to test PDF render'); + $this->markTestSkipped('Enable TESTS_ZEND_CAPTCHA_RECAPTCHA_SUPPORT to test Recaptcha'); } } From 2d56293a5ae3e45e7c8ee7030aa8b305768d8014 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Thu, 23 Feb 2017 08:09:44 +0000 Subject: [PATCH 10/12] Update Changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6114ab..81e6d07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,8 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#31](https://github.com/zendframework/zend-captcha/pull/31) fixes using the + ReCaptcha response as the value parameter to isValid(). ## 2.7.0 - 2017-02-20 From 1162aca4704c85cbad2b77d630c7018ba37e71f5 Mon Sep 17 00:00:00 2001 From: Jakub Date: Mon, 6 Mar 2017 15:58:36 +0100 Subject: [PATCH 11/12] correct link from old to new documentation --- doc/book/usage.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/book/usage.md b/doc/book/usage.md index d787906..08c71c9 100644 --- a/doc/book/usage.md +++ b/doc/book/usage.md @@ -44,5 +44,5 @@ you will use the combination of: > > [zend-form](https://github.com/zendframework/zend-form) contains integration > with zend-captcha via the class `Zend\Form\Element\Captcha`; read the -> [documentation on the CAPTCHA form element](http://framework.zend.com/manual/current/en/modules/zend.form.elements.html#captcha) +> [documentation on the CAPTCHA form element](https://docs.zendframework.com/zend-form/element/captcha/) > for more details. From 0aff296b7fb4ad5babc6124a49ecb83bdc1f91cb Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 24 Apr 2018 12:05:53 -0500 Subject: [PATCH 12/12] Updates docs link in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 006e7dc..f1c59b6 100644 --- a/README.md +++ b/README.md @@ -11,4 +11,4 @@ submissions where authenticated users are not necessary, but you want to prevent spam submissions. - File issues at https://github.com/zendframework/zend-captcha/issues -- Documentation is at https://zendframework.github.io/zend-captcha/ +- Documentation is at https://docs.zendframework.com/zend-captcha/