-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6b65d66
Showing
7 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.DS_Store | ||
composer.lock | ||
phpunit.xml | ||
vendor | ||
.idea | ||
composer.phar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Josh Lockhart <[email protected]> | ||
|
||
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
``` | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "geggleto/psr7-recaptcha", | ||
"type" : "library", | ||
"description": "", | ||
"keywords": ["slim","framework","middleware"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Glenn Eggleton", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
bootstrap="tests/bootstrap.php" | ||
> | ||
<testsuites> | ||
<testsuite name="Renderer Tests"> | ||
<directory>tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>src/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Glenn | ||
* Date: 2015-11-16 | ||
* Time: 10:18 AM | ||
*/ | ||
|
||
namespace Geggleto\Service; | ||
|
||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use ReCaptcha\ReCaptcha; | ||
|
||
class Captcha | ||
{ | ||
/** @var \ReCaptcha\ReCaptcha */ | ||
protected $recaptcha; | ||
|
||
public function __construct (ReCaptcha $reCaptcha) | ||
{ | ||
$this->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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Glenn | ||
* Date: 2015-11-12 | ||
* Time: 11:31 AM | ||
*/ | ||
|
||
include "vendor/autoload.php"; |