Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
geggleto committed Nov 16, 2015
0 parents commit 6b65d66
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
composer.lock
phpunit.xml
vendor
.idea
composer.phar
22 changes: 22 additions & 0 deletions LICENSE.md
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.

27 changes: 27 additions & 0 deletions README.md
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);

```



26 changes: 26 additions & 0 deletions composer.json
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"
}
}
25 changes: 25 additions & 0 deletions phpunit.xml.dist
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>
57 changes: 57 additions & 0 deletions src/Captcha.php
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;
}
}
}
9 changes: 9 additions & 0 deletions tests/bootstrap.php
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";

0 comments on commit 6b65d66

Please sign in to comment.