diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e9828c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.DS_Store +composer.lock +phpunit.xml +vendor +.idea +composer.phar \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..6df4c84 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Josh Lockhart + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..005f88e --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# psr7-recaptcha + +## Configuration + +This middleware helps you guard your application with ReCaptcha. +You can use it as a middleware or even just as an object in your service layer + +This package uses the "google/recaptcha" package to handle the bulk of the lifting. + +## Usage [Middleware] + +The class is invokable +```php +$container[Recaptcha::class] = new \ReCaptcha\ReCaptcha($key); + +$recaptcha = new \Geggleto\Service\Captcha($container[Recaptcha::class]); + +//As Middleware +$app->post('/login', 'Auth:login')->add($recaptcha); + +//Anywhere else +$recaptcha->verify($response, $ip); + +``` + + + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..beb725c --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "geggleto/psr7-recaptcha", + "type" : "library", + "description": "", + "keywords": ["slim","framework","middleware"], + "license": "MIT", + "authors": [ + { + "name": "Glenn Eggleton", + "email": "geggleto@gmail.com" + } + ], + "require": { + "psr/http-message": "^1.0", + "google/recaptcha": "~1.1" + }, + "autoload": { + "psr-4": { + "Geggleto\\Service\\": "src" + } + }, + "require-dev": { + "phpunit/phpunit": "^4.0", + "slim/slim" : "^3.0-RC2" + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..3f730d7 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,25 @@ + + + + + + tests/ + + + + + + src/ + + + \ No newline at end of file diff --git a/src/Captcha.php b/src/Captcha.php new file mode 100644 index 0000000..2a6117a --- /dev/null +++ b/src/Captcha.php @@ -0,0 +1,57 @@ +recaptcha = $reCaptcha; + } + + public function __invoke ( + ServerRequestInterface $requestInterface, + ResponseInterface $responseInterface, + callable $next) + { + $valid = $this->verify( + $requestInterface->getParam('g-recaptcha-response'), + $requestInterface->getServerParams()['REMOTE_ADDR'] + ); + + if ($valid) { + return $next($requestInterface, $responseInterface); + } else { + throw new \Exception("Captcha Failed"); + } + } + + /** + * @param $response + * @param $ip + * @return bool + */ + public function verify($response, $ip) { + + $resp = $this->recaptcha->verify($response, $ip); + if ($resp->isSuccess()) { + return true; + } else { + return false; + } + } +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..2ec4c51 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,9 @@ +