-
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
0 parents
commit f62a040
Showing
11 changed files
with
506 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,15 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_style = space | ||
indent_size = 4 | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.json] | ||
indent_size = 2 | ||
|
||
[*.yml] | ||
indent_size = 2 |
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,4 @@ | ||
/.php_cs.cache | ||
/composer.lock | ||
/coverage.xml | ||
/vendor/ |
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 @@ | ||
<?php | ||
|
||
$header = <<<EOF | ||
This file is part of vaibhavpandeyvpz/jweety package. | ||
(c) Vaibhav Pandey <contact@vaibhavpandey.com> | ||
This source file is subject to the MIT license that is bundled with this source code in the LICENSE file. | ||
EOF; | ||
|
||
use PhpCsFixer\Config; | ||
use PhpCsFixer\Finder; | ||
|
||
return Config::create() | ||
->setFinder( | ||
Finder::create() | ||
->in(__DIR__ . '/src') | ||
->in(__DIR__ . '/tests') | ||
) | ||
->setRules([ | ||
'@PSR1' => true, | ||
'@PSR2' => true, | ||
'header_comment' => ['header' => $header], | ||
'array_syntax' => ['syntax' => 'long'], | ||
]) | ||
->setUsingCache(true); |
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 @@ | ||
after_success: bash <(curl -s https://codecov.io/bash) | ||
|
||
before_install: travis_retry composer self-update | ||
|
||
branches: | ||
only: master | ||
|
||
cache: | ||
directories: $HOME/.composer/cache | ||
|
||
dist: trusty | ||
|
||
install: travis_retry composer install --no-interaction --prefer-dist | ||
|
||
language: php | ||
|
||
php: | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
|
||
script: 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,7 @@ | ||
Copyright (c) 2016-2017 Vaibhav Pandey | ||
|
||
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,19 @@ | ||
# vaibhavpandeyvpz/jweety | ||
Simple [JWT](https://jwt.io/) ([RFC 7519](https://tools.ietf.org/html/rfc7519)) encoding/decoding and validation library, for [PHP](http://php.net/) >= 5.3. | ||
|
||
[![Build status](https://img.shields.io/travis/vaibhavpandeyvpz/jweety.svg?style=flat-square)](https://travis-ci.org/vaibhavpandeyvpz/jweety) | ||
[![Code Coverage](https://img.shields.io/codecov/c/github/vaibhavpandeyvpz/jweety.svg?style=flat-square)](https://codecov.io/gh/vaibhavpandeyvpz/jweety) | ||
[![Latest Version](https://img.shields.io/github/release/vaibhavpandeyvpz/jweety.svg?style=flat-square)](https://github.com/vaibhavpandeyvpz/jweety/releases) | ||
[![Downloads](https://img.shields.io/packagist/dt/vaibhavpandeyvpz/jweety.svg?style=flat-square)](https://packagist.org/packages/vaibhavpandeyvpz/jweety) | ||
[![PHP Version](http://img.shields.io/badge/php-5.3+-8892be.svg?style=flat-square)](http://php.net/) | ||
[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) | ||
|
||
[![SensiolabsInsight](https://insight.sensiolabs.com/projects/b9d88988-f4d6-4e18-8312-a8d49bb075d4/small.png)](https://insight.sensiolabs.com/projects/b9d88988-f4d6-4e18-8312-a8d49bb075d4) | ||
|
||
Documentation | ||
------- | ||
To view documentation, please visit the [Wiki](https://github.com/vaibhavpandeyvpz/jweety/wiki). | ||
|
||
License | ||
------ | ||
See [LICENSE](LICENSE) file. |
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,36 @@ | ||
{ | ||
"authors": [ | ||
{ | ||
"name": "Vaibhav Pandey", | ||
"email": "contact@vaibhavpandey.com" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Jweety\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Jweety\\": "tests/" | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"description": "Simple JWT (RFC 7519) encoding/decoding and validation library, for PHP >= 5.3.", | ||
"homepage": "http://vaibhavpandeyvpz.github.io/jweety", | ||
"keywords": ["jwt", "json web token", "authentication"], | ||
"license": "MIT", | ||
"name": "vaibhavpandeyvpz/jweety", | ||
"require": { | ||
"php": "^5.3||^7.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^5.0||^6.0" | ||
}, | ||
"suggest": { | ||
"symfony/polyfill-php56": "For using hash_equals(...) on ancient PHP versions." | ||
}, | ||
"type": "library" | ||
} |
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,15 @@ | ||
<phpunit bootstrap="vendor/autoload.php" colors="true" verbose="true"> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="coverage-clover" target="coverage.xml" /> | ||
</logging> | ||
<testsuites> | ||
<testsuite name="vaibhavpandeyvpz/jweety"> | ||
<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,138 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of vaibhavpandeyvpz/jweety package. | ||
* | ||
* (c) Vaibhav Pandey <contact@vaibhavpandey.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled with this source code in the LICENSE file. | ||
*/ | ||
|
||
namespace Jweety; | ||
|
||
/** | ||
* Class Encoder | ||
* @package Jweety | ||
*/ | ||
class Encoder implements EncoderInterface | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $algorithms; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $key; | ||
|
||
/** | ||
* Encoder constructor. | ||
* @param string $key | ||
* @param string|array $algorithms | ||
*/ | ||
public function __construct($key, $algorithms = array('HS256', 'HS384', 'HS512')) | ||
{ | ||
$this->key = $key; | ||
$this->algorithms = (array)$algorithms; | ||
} | ||
|
||
/** | ||
* @param object $claims | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function assert($claims) | ||
{ | ||
if (isset($claims->exp) && (time() >= $claims->exp)) { | ||
throw new \InvalidArgumentException( | ||
sprintf("Provided JWT has expired on '%s'.", date(DATE_ISO8601, $claims->exp)) | ||
); | ||
} | ||
if (isset($claims->iat) && (time() < $claims->iat)) { | ||
throw new \InvalidArgumentException( | ||
sprintf('Provided JWT was issued in future (%s).', date(DATE_ISO8601, $claims->iat)) | ||
); | ||
} | ||
if (isset($claims->nbf) && (time() < $claims->nbf)) { | ||
throw new \InvalidArgumentException( | ||
sprintf("Provided JWT cannot be used before '%s'.", date(DATE_ISO8601, $claims->nbf)) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $payload | ||
* @return string | ||
*/ | ||
protected static function decode($payload) | ||
{ | ||
$remainder = strlen($payload) % 4; | ||
if ($remainder) { | ||
$padding = 4 - $remainder; | ||
$payload .= str_repeat('=', $padding); | ||
} | ||
return base64_decode(strtr($payload, '-_', '+/')); | ||
} | ||
|
||
/** | ||
* @param string $payload | ||
* @return string | ||
*/ | ||
public static function encode($payload) | ||
{ | ||
return str_replace('=', '', strtr(base64_encode($payload), '+/', '-_')); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function parse($token, $assert = true) | ||
{ | ||
$parts = explode('.', $token); | ||
if (3 !== count($parts)) { | ||
throw new \InvalidArgumentException('Invalid or malformed JWT supplied.'); | ||
} | ||
$header = json_decode(self::decode($parts[0]), true); | ||
$signature = $this->sign("$parts[0].$parts[1]", $header['alg']); | ||
if (hash_equals($signature, (string)self::decode($parts[2]))) { | ||
$claims = json_decode(self::decode($parts[1])); | ||
if ($assert) { | ||
$this->assert($claims); | ||
} | ||
return $claims; | ||
} | ||
throw new \RuntimeException('Signature verification failed for supplied token.'); | ||
} | ||
|
||
/** | ||
* @param string $payload | ||
* @param string $algorithm | ||
* @return string | ||
*/ | ||
protected function sign($payload, $algorithm) | ||
{ | ||
if (in_array($algorithm, $this->algorithms)) { | ||
static $methods = array( | ||
'HS256' => 'SHA256', | ||
'HS384' => 'SHA384', | ||
'HS512' => 'SHA512', | ||
); | ||
if (isset($methods[$algorithm])) { | ||
return hash_hmac($methods[$algorithm], $payload, $this->key, true); | ||
} | ||
throw new \InvalidArgumentException("Signature algorithm '$algorithm' is not supported."); | ||
} | ||
throw new \InvalidArgumentException("Signature algorithm '$algorithm' is not allowed."); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function stringify($claims, $alg = 'HS256', $typ = 'JWT') | ||
{ | ||
$header = self::encode(json_encode(compact('alg', 'typ'))); | ||
$payload = self::encode(json_encode($claims)); | ||
$signature = self::encode($this->sign("$header.$payload", $alg)); | ||
return "$header.$payload.$signature"; | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of vaibhavpandeyvpz/jweety package. | ||
* | ||
* (c) Vaibhav Pandey <contact@vaibhavpandey.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled with this source code in the LICENSE file. | ||
*/ | ||
|
||
namespace Jweety; | ||
|
||
/** | ||
* Interface EncoderInterface | ||
* @package Jweety | ||
*/ | ||
interface EncoderInterface | ||
{ | ||
/** | ||
* @param string $token | ||
* @param boolean $assert | ||
* @return object | ||
* @throws \RuntimeException | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function parse($token, $assert = true); | ||
|
||
/** | ||
* @param array|object $claims | ||
* @param string $alg | ||
* @param string $typ | ||
* @return string | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function stringify($claims, $alg = 'HS256', $typ = 'JWT'); | ||
} |
Oops, something went wrong.