-
Notifications
You must be signed in to change notification settings - Fork 2
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
Stéphan Kochen
committed
Nov 4, 2016
0 parents
commit cd82174
Showing
13 changed files
with
571 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,2 @@ | ||
vendor | ||
composer.lock |
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,14 @@ | ||
language: php | ||
sudo: false | ||
|
||
php: | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
|
||
before_script: | ||
- composer install -n | ||
|
||
script: | ||
- ./vendor/bin/phpcs --standard=psr2 . --ignore=/vendor/ | ||
- ./vendor/bin/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,19 @@ | ||
Copyright (C) 2016 Angry Bytes | ||
|
||
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,67 @@ | ||
# portier-php | ||
|
||
A [Portier] client library for PHP | ||
|
||
[Portier]: https://portier.github.io/ | ||
|
||
### Example | ||
|
||
```php | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
$app = new \Slim\App(); | ||
|
||
$redis = new Redis(); | ||
$redis->pconnect('127.0.0.1', 6379); | ||
|
||
$portier = new \Portier\Client\Client( | ||
new \Portier\Client\RedisStore($redis), | ||
'http://localhost:8000/verify' | ||
); | ||
|
||
$app->get('/', function($req, $res) { | ||
$res = $res | ||
->withStatus(200) | ||
->withHeader('Content-Type', 'text/html; charset=utf-8'); | ||
|
||
$res->getBody()->write( | ||
<<<EOF | ||
<p>Enter your email address:</p> | ||
<form method="post" action="/auth"> | ||
<input name="email" type="email"> | ||
<button type="submit">Login</button> | ||
</form> | ||
EOF | ||
); | ||
|
||
return $res; | ||
}); | ||
|
||
$app->post('/auth', function($req, $res) use ($portier) { | ||
$authUrl = $portier->authenticate($req->getParsedBodyParam('email')); | ||
|
||
return $res | ||
->withStatus(303) | ||
->withHeader('Location', $authUrl); | ||
}); | ||
|
||
$app->post('/verify', function($req, $res) use ($portier) { | ||
$email = $portier->verify($req->getParsedBodyParam('id_token')); | ||
|
||
$res = $res | ||
->withStatus(200) | ||
->withHeader('Content-Type', 'text/html; charset=utf-8'); | ||
|
||
$res->getBody()->write( | ||
<<<EOF | ||
<p>Verified email address ${email}!</p> | ||
EOF | ||
); | ||
|
||
return $res; | ||
}); | ||
|
||
$app->run(); | ||
``` |
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 @@ | ||
{ | ||
"name": "portier/client", | ||
"description": "Portier client for PHP", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Stéphan Kochen", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Portier\\Client\\": "src/" | ||
} | ||
}, | ||
"require": { | ||
"spomky-labs/base64url": "^1.0", | ||
"fgrosse/phpasn1": "^1.5", | ||
"lcobucci/jwt": "^3.2", | ||
"guzzlehttp/guzzle": "^6.2" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^5.6", | ||
"squizlabs/php_codesniffer": "^2.7" | ||
} | ||
} |
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,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php"> | ||
<testsuites> | ||
<testsuite> | ||
<directory>tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</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,81 @@ | ||
<?php | ||
|
||
namespace Portier\Client; | ||
|
||
/** | ||
* An abstract base class for stores. | ||
* | ||
* Offers default implementations for fetching and generating nonces. | ||
*/ | ||
abstract class AbstractStore implements StoreInterface | ||
{ | ||
/** | ||
* The Guzzle instance to use. | ||
* @var \GuzzleHttp\Client | ||
*/ | ||
public $guzzle; | ||
|
||
/** | ||
* Lifespan of a nonce. | ||
* @var float | ||
*/ | ||
public $nonceTtl = 15 * 60; | ||
|
||
/** | ||
* Minimum time to cache a HTTP response | ||
* @var float | ||
*/ | ||
public $cacheMinTtl = 60 * 60; | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->guzzle = new \GuzzleHttp\Client([ | ||
'timeout' => 10 | ||
]); | ||
} | ||
|
||
/** | ||
* Generate a nonce value. | ||
* @param string $email Optional email context | ||
* @return string The generated nonce. | ||
*/ | ||
public function generateNonce($email) | ||
{ | ||
return bin2hex(random_bytes(16)); | ||
} | ||
|
||
/** | ||
* Fetch a URL using HTTP GET. | ||
* @param string $url The URL to fetch. | ||
* @return object An object with `ttl` and `data` properties. | ||
*/ | ||
public function fetch($url) | ||
{ | ||
$res = $this->guzzle->get($url); | ||
|
||
$data = json_decode($res->getBody()); | ||
if (!($data instanceof \stdClass)) { | ||
throw new \Exception('Invalid response body'); | ||
} | ||
|
||
$ttl = 0; | ||
if ($res->hasHeader('Cache-Control')) { | ||
if (preg_match( | ||
'/max-age\s*=\s*(\d+)/', | ||
$res->getHeader('Cache-Control'), | ||
$matches | ||
)) { | ||
$ttl = intval($matches[1]); | ||
} | ||
} | ||
$ttl = max($this->cacheMinTtl, $ttl); | ||
|
||
return (object) [ | ||
'ttl' => $ttl, | ||
'data' => $data, | ||
]; | ||
} | ||
} |
Oops, something went wrong.