Update v1.0.6.5

This commit is contained in:
sujitprasad
2016-03-02 12:25:21 +05:30
parent 7011553462
commit c56ff86194
218 changed files with 17161 additions and 2358 deletions

View File

@@ -1 +1,6 @@
preset: psr2
preset: laravel
enabled:
- unalign_double_arrow
linting: true

View File

@@ -18,7 +18,3 @@ before_script:
script:
- phpunit --coverage-text --coverage-clover=coverage.clover
after_script:
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi;'
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi;'

View File

@@ -1,34 +0,0 @@
filter:
excluded_paths: [tests/*]
checks:
php:
code_rating: true
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true
tools:
external_code_coverage:
timeout: 1800
runs: 3
php_code_coverage: false
php_code_sniffer:
config:
standard: PSR2
filter:
paths: ['src']
php_loc:
enabled: true
excluded_dirs: [vendor, tests]
php_cpd:
enabled: true
excluded_dirs: [vendor, tests]

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth;
use Tymon\JWTAuth\Providers\Storage\StorageInterface;
@@ -11,6 +20,13 @@ class Blacklist
*/
protected $storage;
/**
* Number of minutes from issue date in which a JWT can be refreshed.
*
* @var int
*/
protected $refreshTTL = 20160;
/**
* @param \Tymon\JWTAuth\Providers\Storage\StorageInterface $storage
*/
@@ -20,34 +36,38 @@ class Blacklist
}
/**
* Add the token (jti claim) to the blacklist
* Add the token (jti claim) to the blacklist.
*
* @param \Tymon\JWTAuth\Payload $payload
* @return boolean
* @return bool
*/
public function add(Payload $payload)
{
$exp = Utils::timestamp($payload['exp']);
$refreshExp = Utils::timestamp($payload['iat'])->addMinutes($this->refreshTTL);
// there is no need to add the token to the blacklist
// if the token has already expired
if ($exp->isPast()) {
// if the token has already expired AND the refresh_ttl
// has gone by
if ($exp->isPast() && $refreshExp->isPast()) {
return false;
}
// add a minute to abate potential overlap
$minutes = $exp->diffInMinutes(Utils::now()->subMinute());
// Set the cache entry's lifetime to be equal to the amount
// of refreshable time it has remaining (which is the larger
// of `exp` and `iat+refresh_ttl`), rounded up a minute
$cacheLifetime = $exp->max($refreshExp)->addMinute()->diffInMinutes();
$this->storage->add($payload['jti'], [], $minutes);
$this->storage->add($payload['jti'], [], $cacheLifetime);
return true;
}
/**
* Determine whether the token has been blacklisted
* Determine whether the token has been blacklisted.
*
* @param \Tymon\JWTAuth\Payload $payload
* @return boolean
* @return bool
*/
public function has(Payload $payload)
{
@@ -55,10 +75,10 @@ class Blacklist
}
/**
* Remove the token (jti claim) from the blacklist
* Remove the token (jti claim) from the blacklist.
*
* @param \Tymon\JWTAuth\Payload $payload
* @return boolean
* @return bool
*/
public function remove(Payload $payload)
{
@@ -66,9 +86,9 @@ class Blacklist
}
/**
* Remove all tokens from the blacklist
* Remove all tokens from the blacklist.
*
* @return boolean
* @return bool
*/
public function clear()
{
@@ -76,4 +96,18 @@ class Blacklist
return true;
}
/**
* Set the refresh time limit.
*
* @param int
*
* @return $this
*/
public function setRefreshTTL($ttl)
{
$this->refreshTTL = (int) $ttl;
return $this;
}
}

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Claims;
class Audience extends Claim
{
/**
* The claim name
* The claim name.
*
* @var string
*/

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Claims;
use Tymon\JWTAuth\Exceptions\InvalidClaimException;
@@ -7,14 +16,14 @@ use Tymon\JWTAuth\Exceptions\InvalidClaimException;
abstract class Claim implements ClaimInterface
{
/**
* The claim name
* The claim name.
*
* @var string
*/
protected $name;
/**
* The claim value
* The claim value.
*
* @var mixed
*/
@@ -29,7 +38,7 @@ abstract class Claim implements ClaimInterface
}
/**
* Set the claim value, and call a validate method if available
* Set the claim value, and call a validate method if available.
*
* @param $value
* @throws \Tymon\JWTAuth\Exceptions\InvalidClaimException
@@ -38,7 +47,7 @@ abstract class Claim implements ClaimInterface
public function setValue($value)
{
if (! $this->validate($value)) {
throw new InvalidClaimException('Invalid value provided for claim "' . $this->getName() . '": ' . $value);
throw new InvalidClaimException('Invalid value provided for claim "'.$this->getName().'": '.$value);
}
$this->value = $value;
@@ -47,7 +56,7 @@ abstract class Claim implements ClaimInterface
}
/**
* Get the claim value
* Get the claim value.
*
* @return mixed
*/
@@ -57,7 +66,7 @@ abstract class Claim implements ClaimInterface
}
/**
* Set the claim name
* Set the claim name.
*
* @param string $name
* @return $this
@@ -70,7 +79,7 @@ abstract class Claim implements ClaimInterface
}
/**
* Get the claim name
* Get the claim name.
*
* @return string
*/
@@ -80,10 +89,10 @@ abstract class Claim implements ClaimInterface
}
/**
* Validate the Claim value
* Validate the Claim value.
*
* @param $value
* @return boolean
* @return bool
*/
protected function validate($value)
{
@@ -91,7 +100,7 @@ abstract class Claim implements ClaimInterface
}
/**
* Build a key value array comprising of the claim name and value
* Build a key value array comprising of the claim name and value.
*
* @return array
*/
@@ -101,7 +110,7 @@ abstract class Claim implements ClaimInterface
}
/**
* Get the claim as a string
* Get the claim as a string.
*
* @return string
*/

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Claims;
interface ClaimInterface
{
/**
* Set the claim value, and call a validate method if available
* Set the claim value, and call a validate method if available.
*
* @param mixed
* @return Claim
@@ -13,14 +22,14 @@ interface ClaimInterface
public function setValue($value);
/**
* Get the claim value
* Get the claim value.
*
* @return mixed
*/
public function getValue();
/**
* Set the claim name
* Set the claim name.
*
* @param string $name
* @return Claim
@@ -28,7 +37,7 @@ interface ClaimInterface
public function setName($name);
/**
* Get the claim name
* Get the claim name.
*
* @return string
*/

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Claims;
class Custom extends Claim

View File

@@ -1,21 +1,30 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Claims;
class Expiration extends Claim
{
/**
* The claim name
* The claim name.
*
* @var string
*/
protected $name = 'exp';
/**
* Validate the expiry claim
* Validate the expiry claim.
*
* @param mixed $value
* @return boolean
* @return bool
*/
protected function validate($value)
{

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Claims;
class Factory
@@ -14,11 +23,11 @@ class Factory
'iss' => 'Tymon\JWTAuth\Claims\Issuer',
'jti' => 'Tymon\JWTAuth\Claims\JwtId',
'nbf' => 'Tymon\JWTAuth\Claims\NotBefore',
'sub' => 'Tymon\JWTAuth\Claims\Subject'
'sub' => 'Tymon\JWTAuth\Claims\Subject',
];
/**
* Get the instance of the claim when passing the name and value
* Get the instance of the claim when passing the name and value.
*
* @param string $name
* @param mixed $value
@@ -34,10 +43,10 @@ class Factory
}
/**
* Check whether the claim exists
* Check whether the claim exists.
*
* @param string $name
* @return boolean
* @return bool
*/
public function has($name)
{

View File

@@ -1,21 +1,30 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Claims;
class IssuedAt extends Claim
{
/**
* The claim name
* The claim name.
*
* @var string
*/
protected $name = 'iat';
/**
* Validate the issued at claim
* Validate the issued at claim.
*
* @param mixed $value
* @return boolean
* @return bool
*/
protected function validate($value)
{

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Claims;
class Issuer extends Claim
{
/**
* The claim name
* The claim name.
*
* @var string
*/

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Claims;
class JwtId extends Claim
{
/**
* The claim name
* The claim name.
*
* @var string
*/

View File

@@ -1,21 +1,30 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Claims;
class NotBefore extends Claim
{
/**
* The claim name
* The claim name.
*
* @var string
*/
protected $name = 'nbf';
/**
* Validate the not before claim
* Validate the not before claim.
*
* @param mixed $value
* @return boolean
* @return bool
*/
protected function validate($value)
{

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Claims;
class Subject extends Claim
{
/**
* The claim name
* The claim name.
*
* @var string
*/

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Commands;
use Illuminate\Support\Str;

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Exceptions;
class InvalidClaimException extends JWTException
{
/**
* @var integer
* @var int
*/
protected $statusCode = 400;
}

View File

@@ -1,17 +1,26 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Exceptions;
class JWTException extends \Exception
{
/**
* @var integer
* @var int
*/
protected $statusCode = 500;
/**
* @param string $message
* @param integer $statusCode
* @param int $statusCode
*/
public function __construct($message = 'An error occurred', $statusCode = null)
{
@@ -23,7 +32,7 @@ class JWTException extends \Exception
}
/**
* @param integer $statusCode
* @param int $statusCode
*/
public function setStatusCode($statusCode)
{
@@ -31,7 +40,7 @@ class JWTException extends \Exception
}
/**
* @return integer the status code
* @return int the status code
*/
public function getStatusCode()
{

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Exceptions;
class PayloadException extends JWTException
{
/**
* @var integer
* @var int
*/
protected $statusCode = 500;
}

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Exceptions;
class TokenBlacklistedException extends TokenInvalidException
{
/**
* @var integer
* @var int
*/
protected $statusCode = 401;
}

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Exceptions;
class TokenExpiredException extends JWTException
{
/**
* @var integer
* @var int
*/
protected $statusCode = 401;
}

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Exceptions;
class TokenInvalidException extends JWTException
{
/**
* @var integer
* @var int
*/
protected $statusCode = 400;
}

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Facades;
use Illuminate\Support\Facades\Facade;

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Facades;
use Illuminate\Support\Facades\Facade;

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth;
use Illuminate\Http\Request;
@@ -140,7 +149,7 @@ class JWTAuth
*
* @param mixed $token
*
* @return boolean
* @return bool
*/
public function invalidate($token = false)
{
@@ -152,7 +161,7 @@ class JWTAuth
/**
* Get the token.
*
* @return boolean|string
* @return bool|string
*/
public function getToken()
{

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
@@ -24,12 +33,12 @@ class JWTManager
protected $payloadFactory;
/**
* @var boolean
* @var bool
*/
protected $blacklistEnabled = true;
/**
* @var boolean
* @var bool
*/
protected $refreshFlow = false;
@@ -46,7 +55,7 @@ class JWTManager
}
/**
* Encode a Payload and return the Token
* Encode a Payload and return the Token.
*
* @param \Tymon\JWTAuth\Payload $payload
* @return \Tymon\JWTAuth\Token
@@ -59,7 +68,7 @@ class JWTManager
}
/**
* Decode a Token and return the Payload
* Decode a Token and return the Payload.
*
* @param \Tymon\JWTAuth\Token $token
* @return Payload
@@ -79,7 +88,7 @@ class JWTManager
}
/**
* Refresh a Token and return a new Token
* Refresh a Token and return a new Token.
*
* @param \Tymon\JWTAuth\Token $token
* @return \Tymon\JWTAuth\Token
@@ -97,16 +106,16 @@ class JWTManager
return $this->encode(
$this->payloadFactory->make([
'sub' => $payload['sub'],
'iat' => $payload['iat']
'iat' => $payload['iat'],
])
);
}
/**
* Invalidate a Token by adding it to the blacklist
* Invalidate a Token by adding it to the blacklist.
*
* @param Token $token
* @return boolean
* @return bool
*/
public function invalidate(Token $token)
{
@@ -118,7 +127,7 @@ class JWTManager
}
/**
* Get the PayloadFactory instance
* Get the PayloadFactory instance.
*
* @return \Tymon\JWTAuth\PayloadFactory
*/
@@ -128,7 +137,7 @@ class JWTManager
}
/**
* Get the JWTProvider instance
* Get the JWTProvider instance.
*
* @return \Tymon\JWTAuth\Providers\JWT\JWTInterface
*/
@@ -138,7 +147,7 @@ class JWTManager
}
/**
* Get the Blacklist instance
* Get the Blacklist instance.
*
* @return \Tymon\JWTAuth\Blacklist
*/
@@ -148,7 +157,7 @@ class JWTManager
}
/**
* Set whether the blacklist is enabled
* Set whether the blacklist is enabled.
*
* @param bool $enabled
*/
@@ -160,9 +169,9 @@ class JWTManager
}
/**
* Set the refresh flow
* Set the refresh flow.
*
* @param boolean $refreshFlow
* @param bool $refreshFlow
* @return $this
*/
public function setRefreshFlow($refreshFlow = true)

View File

@@ -1,20 +1,29 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Middleware;
use Tymon\JWTAuth\JWTAuth;
use Illuminate\Events\Dispatcher;
use Illuminate\Routing\ResponseFactory;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Routing\ResponseFactory;
abstract class BaseMiddleware
{
/**
* @var \Illuminate\Routing\ResponseFactory
* @var \Illuminate\Contracts\Routing\ResponseFactory
*/
protected $response;
/**
* @var \Illuminate\Events\Dispatcher
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $events;
@@ -24,10 +33,10 @@ abstract class BaseMiddleware
protected $auth;
/**
* Create a new BaseMiddleware instance
* Create a new BaseMiddleware instance.
*
* @param \Illuminate\Routing\ResponseFactory $response
* @param \Illuminate\Events\Dispatcher $events
* @param \Illuminate\Contracts\Routing\ResponseFactory $response
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @param \Tymon\JWTAuth\JWTAuth $auth
*/
public function __construct(ResponseFactory $response, Dispatcher $events, JWTAuth $auth)
@@ -38,11 +47,11 @@ abstract class BaseMiddleware
}
/**
* Fire event and return the response
* Fire event and return the response.
*
* @param string $event
* @param string $error
* @param integer $status
* @param int $status
* @param array $payload
* @return mixed
*/

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Middleware;
use Tymon\JWTAuth\Exceptions\JWTException;

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Middleware;
use Tymon\JWTAuth\Exceptions\JWTException;
@@ -27,7 +36,7 @@ class RefreshToken extends BaseMiddleware
}
// send the refreshed token back to the client
$response->headers->set('Authorization', 'Bearer ' . $newToken);
$response->headers->set('Authorization', 'Bearer '.$newToken);
return $response;
}

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth;
use Tymon\JWTAuth\Claims\Claim;
@@ -9,14 +18,14 @@ use Tymon\JWTAuth\Validators\PayloadValidator;
class Payload implements \ArrayAccess
{
/**
* The array of claims
* The array of claims.
*
* @var \Tymon\JWTAuth\Claims\Claim[]
*/
private $claims = [];
/**
* Build the Payload
* Build the Payload.
*
* @param array $claims
* @param \Tymon\JWTAuth\Validators\PayloadValidator $validator
@@ -30,7 +39,7 @@ class Payload implements \ArrayAccess
}
/**
* Get the array of claim instances
* Get the array of claim instances.
*
* @return \Tymon\JWTAuth\Claims\Claim[]
*/
@@ -40,7 +49,7 @@ class Payload implements \ArrayAccess
}
/**
* Get the array of claims
* Get the array of claims.
*
* @return array
*/
@@ -55,7 +64,7 @@ class Payload implements \ArrayAccess
}
/**
* Get the payload
* Get the payload.
*
* @param string $claim
* @return mixed
@@ -74,10 +83,10 @@ class Payload implements \ArrayAccess
}
/**
* Determine whether the payload has the claim
* Determine whether the payload has the claim.
*
* @param \Tymon\JWTAuth\Claims\Claim $claim
* @return boolean
* @return bool
*/
public function has(Claim $claim)
{
@@ -85,7 +94,7 @@ class Payload implements \ArrayAccess
}
/**
* Get the payload as a string
* Get the payload as a string.
*
* @return string
*/
@@ -117,7 +126,7 @@ class Payload implements \ArrayAccess
}
/**
* Don't allow changing the payload as it should be immutable
* Don't allow changing the payload as it should be immutable.
*
* @param mixed $key
* @param mixed $value
@@ -130,7 +139,7 @@ class Payload implements \ArrayAccess
}
/**
* Don't allow changing the payload as it should be immutable
* Don't allow changing the payload as it should be immutable.
*
* @param string $key
* @throws Exceptions\PayloadException
@@ -142,7 +151,7 @@ class Payload implements \ArrayAccess
}
/**
* Magically get a claim value
* Magically get a claim value.
*
* @param string $method
* @param array $parameters

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth;
use Illuminate\Http\Request;
@@ -29,7 +38,7 @@ class PayloadFactory
protected $ttl = 60;
/**
* @var boolean
* @var bool
*/
protected $refreshFlow = false;
@@ -56,7 +65,7 @@ class PayloadFactory
}
/**
* Create the Payload instance
* Create the Payload instance.
*
* @param array $customClaims
* @return \Tymon\JWTAuth\Payload
@@ -69,7 +78,7 @@ class PayloadFactory
}
/**
* Add an array of claims to the Payload
* Add an array of claims to the Payload.
*
* @param array $claims
* @return $this
@@ -84,7 +93,7 @@ class PayloadFactory
}
/**
* Add a claim to the Payload
* Add a claim to the Payload.
*
* @param string $name
* @param mixed $value
@@ -98,7 +107,7 @@ class PayloadFactory
}
/**
* Build the default claims
* Build the default claims.
*
* @param array $customClaims
* @return $this
@@ -118,7 +127,7 @@ class PayloadFactory
}
/**
* Build out the Claim DTO's
* Build out the Claim DTO's.
*
* @return array
*/
@@ -133,7 +142,7 @@ class PayloadFactory
}
/**
* Set the Issuer (iss) claim
* Set the Issuer (iss) claim.
*
* @return string
*/
@@ -143,37 +152,37 @@ class PayloadFactory
}
/**
* Set the Issued At (iat) claim
* Set the Issued At (iat) claim.
*
* @return int
*/
public function iat()
{
return (int) Utils::now()->format('U');
return Utils::now()->timestamp;
}
/**
* Set the Expiration (exp) claim
* Set the Expiration (exp) claim.
*
* @return int
*/
public function exp()
{
return (int) Utils::now()->addMinutes($this->ttl)->format('U');
return Utils::now()->addMinutes($this->ttl)->timestamp;
}
/**
* Set the Not Before (nbf) claim
* Set the Not Before (nbf) claim.
*
* @return int
*/
public function nbf()
{
return (int) Utils::now()->format('U');
return Utils::now()->timestamp;
}
/**
* Set a unique id (jti) for the token
* Set a unique id (jti) for the token.
*
* @return string
*/
@@ -186,7 +195,7 @@ class PayloadFactory
}
/**
* Set the token ttl (in minutes)
* Set the token ttl (in minutes).
*
* @param int $ttl
* @return $this
@@ -199,7 +208,7 @@ class PayloadFactory
}
/**
* Get the token ttl
* Get the token ttl.
*
* @return int
*/
@@ -209,9 +218,9 @@ class PayloadFactory
}
/**
* Set the refresh flow
* Set the refresh flow.
*
* @param boolean $refreshFlow
* @param bool $refreshFlow
* @return $this
*/
public function setRefreshFlow($refreshFlow = true)
@@ -222,7 +231,7 @@ class PayloadFactory
}
/**
* Magically add a claim
* Magically add a claim.
*
* @param string $method
* @param array $parameters

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Providers\Auth;
interface AuthInterface
{
/**
* Check a user's credentials
* Check a user's credentials.
*
* @param array $credentials
* @return bool
@@ -13,7 +22,7 @@ interface AuthInterface
public function byCredentials(array $credentials = []);
/**
* Authenticate a user via the id
* Authenticate a user via the id.
*
* @param mixed $id
* @return bool
@@ -21,7 +30,7 @@ interface AuthInterface
public function byId($id);
/**
* Get the currently authenticated user
* Get the currently authenticated user.
*
* @return mixed
*/

View File

@@ -1,8 +1,16 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Providers\Auth;
use Exception;
use Illuminate\Auth\AuthManager;
class IlluminateAuthAdapter implements AuthInterface
@@ -21,7 +29,7 @@ class IlluminateAuthAdapter implements AuthInterface
}
/**
* Check a user's credentials
* Check a user's credentials.
*
* @param array $credentials
* @return bool
@@ -32,22 +40,18 @@ class IlluminateAuthAdapter implements AuthInterface
}
/**
* Authenticate a user via the id
* Authenticate a user via the id.
*
* @param mixed $id
* @return bool
*/
public function byId($id)
{
try {
return $this->auth->onceUsingId($id);
} catch (Exception $e) {
return false;
}
return $this->auth->onceUsingId($id);
}
/**
* Get the currently authenticated user
* Get the currently authenticated user.
*
* @return mixed
*/

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Providers\JWT;
interface JWTInterface

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Providers\JWT;
abstract class JWTProvider
@@ -25,7 +34,7 @@ abstract class JWTProvider
}
/**
* Set the algorithm used to sign the token
* Set the algorithm used to sign the token.
*
* @param string $algo
* @return self
@@ -38,7 +47,7 @@ abstract class JWTProvider
}
/**
* Get the algorithm used to sign the token
* Get the algorithm used to sign the token.
*
* @return string
*/

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Providers\JWT;
use Exception;
@@ -27,7 +36,7 @@ class NamshiAdapter extends JWTProvider implements JWTInterface
}
/**
* Create a JSON Web Token
* Create a JSON Web Token.
*
* @return string
* @throws \Tymon\JWTAuth\Exceptions\JWTException
@@ -39,12 +48,12 @@ class NamshiAdapter extends JWTProvider implements JWTInterface
return $this->jws->getTokenString();
} catch (Exception $e) {
throw new JWTException('Could not create token: ' . $e->getMessage());
throw new JWTException('Could not create token: '.$e->getMessage());
}
}
/**
* Decode a JSON Web Token
* Decode a JSON Web Token.
*
* @param string $token
* @return array
@@ -55,7 +64,7 @@ class NamshiAdapter extends JWTProvider implements JWTInterface
try {
$jws = JWS::load($token);
} catch (Exception $e) {
throw new TokenInvalidException('Could not decode token: ' . $e->getMessage());
throw new TokenInvalidException('Could not decode token: '.$e->getMessage());
}
if (! $jws->verify($this->secret, $this->algo)) {

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Providers;
use Tymon\JWTAuth\JWTAuth;
@@ -26,7 +35,7 @@ class JWTAuthServiceProvider extends ServiceProvider
public function boot()
{
$this->publishes([
__DIR__.'/../config/config.php' => config_path('jwt.php')
__DIR__.'/../config/config.php' => config_path('jwt.php'),
], 'config');
$this->bootBindings();
@@ -35,7 +44,7 @@ class JWTAuthServiceProvider extends ServiceProvider
}
/**
* Bind some Interfaces and implementations
* Bind some Interfaces and implementations.
*/
protected function bootBindings()
{
@@ -102,11 +111,11 @@ class JWTAuthServiceProvider extends ServiceProvider
$this->registerPayloadFactory();
$this->registerJWTCommand();
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'jwt');
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'jwt');
}
/**
* Register the bindings for the User provider
* Register the bindings for the User provider.
*/
protected function registerUserProvider()
{
@@ -116,7 +125,7 @@ class JWTAuthServiceProvider extends ServiceProvider
}
/**
* Register the bindings for the JSON Web Token provider
* Register the bindings for the JSON Web Token provider.
*/
protected function registerJWTProvider()
{
@@ -131,7 +140,7 @@ class JWTAuthServiceProvider extends ServiceProvider
}
/**
* Register the bindings for the Auth provider
* Register the bindings for the Auth provider.
*/
protected function registerAuthProvider()
{
@@ -141,7 +150,7 @@ class JWTAuthServiceProvider extends ServiceProvider
}
/**
* Register the bindings for the Storage provider
* Register the bindings for the Storage provider.
*/
protected function registerStorageProvider()
{
@@ -151,7 +160,7 @@ class JWTAuthServiceProvider extends ServiceProvider
}
/**
* Register the bindings for the Payload Factory
* Register the bindings for the Payload Factory.
*/
protected function registerClaimFactory()
{
@@ -161,7 +170,7 @@ class JWTAuthServiceProvider extends ServiceProvider
}
/**
* Register the bindings for the JWT Manager
* Register the bindings for the JWT Manager.
*/
protected function registerJWTManager()
{
@@ -178,7 +187,7 @@ class JWTAuthServiceProvider extends ServiceProvider
}
/**
* Register the bindings for the main JWTAuth class
* Register the bindings for the main JWTAuth class.
*/
protected function registerJWTAuth()
{
@@ -196,17 +205,19 @@ class JWTAuthServiceProvider extends ServiceProvider
}
/**
* Register the bindings for the main JWTAuth class
* Register the bindings for the main JWTAuth class.
*/
protected function registerJWTBlacklist()
{
$this->app['tymon.jwt.blacklist'] = $this->app->share(function ($app) {
return new Blacklist($app['tymon.jwt.provider.storage']);
$instance = new Blacklist($app['tymon.jwt.provider.storage']);
return $instance->setRefreshTTL($this->config('refresh_ttl'));
});
}
/**
* Register the bindings for the payload validator
* Register the bindings for the payload validator.
*/
protected function registerPayloadValidator()
{
@@ -216,7 +227,7 @@ class JWTAuthServiceProvider extends ServiceProvider
}
/**
* Register the bindings for the Payload Factory
* Register the bindings for the Payload Factory.
*/
protected function registerPayloadFactory()
{
@@ -228,7 +239,7 @@ class JWTAuthServiceProvider extends ServiceProvider
}
/**
* Register the Artisan command
* Register the Artisan command.
*/
protected function registerJWTCommand()
{
@@ -238,7 +249,7 @@ class JWTAuthServiceProvider extends ServiceProvider
}
/**
* Helper to get the config values
* Helper to get the config values.
*
* @param string $key
* @return string
@@ -249,7 +260,7 @@ class JWTAuthServiceProvider extends ServiceProvider
}
/**
* Get an instantiable configuration instance. Pinched from dingo/api :)
* Get an instantiable configuration instance. Pinched from dingo/api :).
*
* @param mixed $instance
* @return object

View File

@@ -1,9 +1,17 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Providers\Storage;
use Illuminate\Cache\CacheManager;
use Tymon\JWTAuth\Providers\Storage\StorageInterface;
class IlluminateCacheAdapter implements StorageInterface
{
@@ -26,7 +34,7 @@ class IlluminateCacheAdapter implements StorageInterface
}
/**
* Add a new item into storage
* Add a new item into storage.
*
* @param string $key
* @param mixed $value
@@ -39,7 +47,7 @@ class IlluminateCacheAdapter implements StorageInterface
}
/**
* Check whether a key exists in storage
* Check whether a key exists in storage.
*
* @param string $key
* @return bool
@@ -50,7 +58,7 @@ class IlluminateCacheAdapter implements StorageInterface
}
/**
* Remove an item from storage
* Remove an item from storage.
*
* @param string $key
* @return bool
@@ -61,7 +69,7 @@ class IlluminateCacheAdapter implements StorageInterface
}
/**
* Remove all items associated with the tag
* Remove all items associated with the tag.
*
* @return void
*/
@@ -71,7 +79,7 @@ class IlluminateCacheAdapter implements StorageInterface
}
/**
* Return the cache instance with tags attached
* Return the cache instance with tags attached.
*
* @return \Illuminate\Cache\CacheManager
*/

View File

@@ -1,25 +1,34 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Providers\Storage;
interface StorageInterface
{
/**
* @param string $key
* @param integer $minutes
* @param int $minutes
* @return void
*/
public function add($key, $value, $minutes);
/**
* @param string $key
* @return boolean
* @return bool
*/
public function has($key);
/**
* @param string $key
* @return boolean
* @return bool
*/
public function destroy($key);

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Providers\User;
use Illuminate\Database\Eloquent\Model;
@@ -12,7 +21,7 @@ class EloquentUserAdapter implements UserInterface
protected $user;
/**
* Create a new User instance
* Create a new User instance.
*
* @param \Illuminate\Database\Eloquent\Model $user
*/
@@ -22,7 +31,7 @@ class EloquentUserAdapter implements UserInterface
}
/**
* Get the user by the given key, value
* Get the user by the given key, value.
*
* @param mixed $key
* @param mixed $value

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Providers\User;
interface UserInterface
{
/**
* Get the user by the given key, value
* Get the user by the given key, value.
*
* @param string $key
* @param mixed $value

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth;
use Tymon\JWTAuth\Validators\TokenValidator;
@@ -12,7 +21,7 @@ class Token
private $value;
/**
* Create a new JSON Web Token
* Create a new JSON Web Token.
*
* @param string $value
*/
@@ -24,7 +33,7 @@ class Token
}
/**
* Get the token
* Get the token.
*
* @return string
*/
@@ -34,7 +43,7 @@ class Token
}
/**
* Get the token when casting to string
* Get the token when casting to string.
*
* @return string
*/

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth;
use Carbon\Carbon;
@@ -7,7 +16,7 @@ use Carbon\Carbon;
class Utils
{
/**
* Get the Carbon instance for the current time
* Get the Carbon instance for the current time.
*
* @return \Carbon\Carbon
*/
@@ -17,7 +26,7 @@ class Utils
}
/**
* Get the Carbon instance for the timestamp
* Get the Carbon instance for the timestamp.
*
* @param int $timestamp
* @return \Carbon\Carbon

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Validators;
use Tymon\JWTAuth\Exceptions\JWTException;
@@ -12,7 +21,7 @@ abstract class AbstractValidator implements ValidatorInterface
protected $refreshFlow = false;
/**
* Helper function to return a boolean
* Helper function to return a boolean.
*
* @param array $value
* @return bool
@@ -29,7 +38,7 @@ abstract class AbstractValidator implements ValidatorInterface
}
/**
* Set the refresh flow flag
* Set the refresh flow flag.
*
* @param bool $refreshFlow
* @return $this

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Validators;
use Tymon\JWTAuth\Utils;
@@ -14,12 +23,12 @@ class PayloadValidator extends AbstractValidator
protected $requiredClaims = ['iss', 'iat', 'exp', 'nbf', 'sub', 'jti'];
/**
* @var integer
* @var int
*/
protected $refreshTTL = 20160;
/**
* Run the validations on the payload array
* Run the validations on the payload array.
*
* @param array $value
* @return void
@@ -37,7 +46,7 @@ class PayloadValidator extends AbstractValidator
/**
* Ensure the payload contains the required claims and
* the claims have the relevant type
* the claims have the relevant type.
*
* @param array $payload
* @throws \Tymon\JWTAuth\Exceptions\TokenInvalidException
@@ -53,12 +62,12 @@ class PayloadValidator extends AbstractValidator
}
/**
* Validate the payload timestamps
* Validate the payload timestamps.
*
* @param array $payload
* @throws \Tymon\JWTAuth\Exceptions\TokenExpiredException
* @throws \Tymon\JWTAuth\Exceptions\TokenInvalidException
* @return boolean
* @return bool
*/
protected function validateTimestamps(array $payload)
{
@@ -78,14 +87,14 @@ class PayloadValidator extends AbstractValidator
}
/**
* Check the token in the refresh flow context
* Check the token in the refresh flow context.
*
* @param $payload
* @return bool
*/
protected function validateRefresh(array $payload)
{
if (isset($payload['iat']) && Utils::timestamp($payload['iat'])->diffInMinutes(Utils::now()) >= $this->refreshTTL) {
if (isset($payload['iat']) && Utils::timestamp($payload['iat'])->addMinutes($this->refreshTTL)->isPast()) {
throw new TokenExpiredException('Token has expired and can no longer be refreshed', 400);
}
@@ -93,7 +102,7 @@ class PayloadValidator extends AbstractValidator
}
/**
* Set the required claims
* Set the required claims.
*
* @param array $claims
*/
@@ -105,9 +114,9 @@ class PayloadValidator extends AbstractValidator
}
/**
* Set the refresh ttl
* Set the refresh ttl.
*
* @param integer $ttl
* @param int $ttl
*/
public function setRefreshTTL($ttl)
{

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Validators;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
@@ -7,7 +16,7 @@ use Tymon\JWTAuth\Exceptions\TokenInvalidException;
class TokenValidator extends AbstractValidator
{
/**
* Check the structure of the token
* Check the structure of the token.
*
* @param string $value
* @return void
@@ -20,7 +29,7 @@ class TokenValidator extends AbstractValidator
/**
* @param string $token
* @throws \Tymon\JWTAuth\Exceptions\TokenInvalidException
* @return boolean
* @return bool
*/
protected function validateStructure($token)
{

View File

@@ -1,11 +1,20 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Validators;
interface ValidatorInterface
{
/**
* Perform some checks on the value
* Perform some checks on the value.
*
* @param mixed $value
* @return void
@@ -13,7 +22,7 @@ interface ValidatorInterface
public function check($value);
/**
* Helper function to return a boolean
* Helper function to return a boolean.
*
* @param array $value
* @return bool

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
return [
/*
@@ -146,9 +155,7 @@ return [
|
*/
'auth' => function ($app) {
return new Tymon\JWTAuth\Providers\Auth\IlluminateAuthAdapter($app['auth']);
},
'auth' => 'Tymon\JWTAuth\Providers\Auth\IlluminateAuthAdapter',
/*
|--------------------------------------------------------------------------
@@ -159,10 +166,8 @@ return [
|
*/
'storage' => function ($app) {
return new Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter($app['cache']);
}
'storage' => 'Tymon\JWTAuth\Providers\Storage\IlluminateCacheAdapter',
]
],
];

View File

@@ -1,7 +1,17 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Providers\JWT;
use Carbon\Carbon;
use Mockery;
use Tymon\JWTAuth\Blacklist;
use Tymon\JWTAuth\Payload;
@@ -9,17 +19,18 @@ use Tymon\JWTAuth\Claims\Issuer;
use Tymon\JWTAuth\Claims\IssuedAt;
use Tymon\JWTAuth\Claims\Expiration;
use Tymon\JWTAuth\Claims\NotBefore;
use Tymon\JWTAuth\Claims\Audience;
use Tymon\JWTAuth\Claims\Subject;
use Tymon\JWTAuth\Claims\JwtId;
use Tymon\JWTAuth\Claims\Custom;
class BlacklistTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
Carbon::setTestNow(Carbon::createFromTimeStampUTC(123));
$this->storage = Mockery::mock('Tymon\JWTAuth\Providers\Storage\StorageInterface');
$this->blacklist = new Blacklist($this->storage);
$this->blacklist->setRefreshTTL(20160);
$this->validator = Mockery::mock('Tymon\JWTAuth\Validators\PayloadValidator');
$this->validator->shouldReceive('setRefreshFlow->check');
@@ -36,27 +47,44 @@ class BlacklistTest extends \PHPUnit_Framework_TestCase
$claims = [
new Subject(1),
new Issuer('http://example.com'),
new Expiration(123 + 3600),
new NotBefore(123),
new IssuedAt(123),
new JwtId('foo')
new Expiration(100 + 3600),
new NotBefore(100),
new IssuedAt(100),
new JwtId('foo'),
];
$payload = new Payload($claims, $this->validator);
$this->storage->shouldReceive('add')->with('foo', [], 61);
$this->blacklist->add($payload);
$this->storage->shouldReceive('add')->once()->with('foo', [], 20160);
$this->assertTrue($this->blacklist->add($payload));
}
/** @test */
public function it_should_return_false_when_adding_an_expired_token_to_the_blacklist()
public function it_should_return_true_when_adding_a_refreshable_expired_token_to_the_blacklist()
{
$claims = [
new Subject(1),
new Issuer('http://example.com'),
new Expiration(123 - 3600),
new NotBefore(123),
new IssuedAt(123),
new JwtId('foo')
new Expiration(101),
new NotBefore(100),
new IssuedAt(100),
new JwtId('foo'),
];
$payload = new Payload($claims, $this->validator, true);
$this->storage->shouldReceive('add')->once()->with('foo', [], 20160);
$this->assertTrue($this->blacklist->add($payload));
}
/** @test */
public function it_should_return_false_when_adding_an_unrefreshable_token_to_the_blacklist()
{
$claims = [
new Subject(1),
new Issuer('http://example.com'),
new Expiration(100), // default refresh_ttl
new NotBefore(100),
new IssuedAt(100 - 20160 * 60),
new JwtId('foo'),
];
$payload = new Payload($claims, $this->validator, true);
@@ -64,6 +92,24 @@ class BlacklistTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($this->blacklist->add($payload));
}
/** @test */
public function it_should_return_false_when_adding_a_unrefreshable_token_after_modifying_refresh_ttl()
{
$claims = [
new Subject(1),
new Issuer('http://example.com'),
new Expiration(101),
new NotBefore(100),
new IssuedAt(100),
new JwtId('foo'),
];
$payload = new Payload($claims, $this->validator, true);
$this->storage->shouldReceive('add')->never();
$this->blacklist->setRefreshTTL(0);
$this->assertFalse($this->blacklist->add($payload));
}
/** @test */
public function it_should_check_whether_a_token_has_been_blacklisted()
{
@@ -73,11 +119,11 @@ class BlacklistTest extends \PHPUnit_Framework_TestCase
new Expiration(123 + 3600),
new NotBefore(123),
new IssuedAt(123),
new JwtId('foobar')
new JwtId('foobar'),
];
$payload = new Payload($claims, $this->validator);
$this->storage->shouldReceive('has')->with('foobar')->andReturn(true);
$this->storage->shouldReceive('has')->once()->with('foobar')->andReturn(true);
$this->assertTrue($this->blacklist->has($payload));
}
@@ -90,18 +136,18 @@ class BlacklistTest extends \PHPUnit_Framework_TestCase
new Expiration(123 + 3600),
new NotBefore(123),
new IssuedAt(123),
new JwtId('foobar')
new JwtId('foobar'),
];
$payload = new Payload($claims, $this->validator);
$this->storage->shouldReceive('destroy')->with('foobar')->andReturn(true);
$this->storage->shouldReceive('destroy')->once()->with('foobar')->andReturn(true);
$this->assertTrue($this->blacklist->remove($payload));
}
/** @test */
public function it_should_empty_the_blacklist()
{
$this->storage->shouldReceive('flush');
$this->storage->shouldReceive('flush')->once();
$this->assertTrue($this->blacklist->clear());
}
}

View File

@@ -1,8 +1,16 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test;
use Mockery;
use Symfony\Component\Console\Tester\CommandTester;
use Tymon\JWTAuth\Commands\JWTGenerateCommand;
use Illuminate\Foundation\Application;
@@ -29,7 +37,7 @@ class JWTGenerateCommandTest extends \PHPUnit_Framework_TestCase
// $this->runCommand($this->command);
}
protected function runCommand($command, $input = array())
protected function runCommand($command, $input = [])
{
return $command->run(new ArrayInput($input), new NullOutput);
}

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test;
use Mockery;
@@ -93,7 +102,7 @@ class JWTAuthTest extends \PHPUnit_Framework_TestCase
{
$this->setExpectedException('Tymon\JWTAuth\Exceptions\JWTException');
$user = $this->jwtAuth->toUser();
$this->jwtAuth->toUser();
}
/** @test */

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Providers\JWT;
use Mockery;
@@ -10,10 +19,8 @@ use Tymon\JWTAuth\Claims\Issuer;
use Tymon\JWTAuth\Claims\IssuedAt;
use Tymon\JWTAuth\Claims\Expiration;
use Tymon\JWTAuth\Claims\NotBefore;
use Tymon\JWTAuth\Claims\Audience;
use Tymon\JWTAuth\Claims\Subject;
use Tymon\JWTAuth\Claims\JwtId;
use Tymon\JWTAuth\Claims\Custom;
class JWTManagerTest extends \PHPUnit_Framework_TestCase
{
@@ -42,7 +49,7 @@ class JWTManagerTest extends \PHPUnit_Framework_TestCase
new Expiration(123 + 3600),
new NotBefore(123),
new IssuedAt(123),
new JwtId('foo')
new JwtId('foo'),
];
$payload = new Payload($claims, $this->validator);
@@ -62,7 +69,7 @@ class JWTManagerTest extends \PHPUnit_Framework_TestCase
new Expiration(123 + 3600),
new NotBefore(123),
new IssuedAt(123),
new JwtId('foo')
new JwtId('foo'),
];
$payload = new Payload($claims, $this->validator);
$token = new Token('foo.bar.baz');
@@ -87,7 +94,7 @@ class JWTManagerTest extends \PHPUnit_Framework_TestCase
new Expiration(123 + 3600),
new NotBefore(123),
new IssuedAt(123),
new JwtId('foo')
new JwtId('foo'),
];
$payload = new Payload($claims, $this->validator);
$token = new Token('foo.bar.baz');
@@ -96,7 +103,7 @@ class JWTManagerTest extends \PHPUnit_Framework_TestCase
$this->factory->shouldReceive('setRefreshFlow->make')->with($payload->toArray())->andReturn($payload);
$this->blacklist->shouldReceive('has')->with($payload)->andReturn(true);
$payload = $this->manager->decode($token);
$this->manager->decode($token);
}
/** @test */
@@ -108,7 +115,7 @@ class JWTManagerTest extends \PHPUnit_Framework_TestCase
new Expiration(123 - 3600),
new NotBefore(123),
new IssuedAt(123),
new JwtId('foo')
new JwtId('foo'),
];
$payload = new Payload($claims, $this->validator, true);
$token = new Token('foo.bar.baz');
@@ -137,7 +144,7 @@ class JWTManagerTest extends \PHPUnit_Framework_TestCase
new Expiration(123 + 3600),
new NotBefore(123),
new IssuedAt(123),
new JwtId('foo')
new JwtId('foo'),
];
$payload = new Payload($claims, $this->validator);
$token = new Token('foo.bar.baz');

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test;
use Mockery;
@@ -11,11 +20,11 @@ class GetUserFromTokenTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->events = Mockery::mock('Illuminate\Events\Dispatcher');
$this->events = Mockery::mock('Illuminate\Contracts\Events\Dispatcher');
$this->auth = Mockery::mock('Tymon\JWTAuth\JWTAuth');
$this->request = Mockery::mock('Illuminate\Http\Request');
$this->response = Mockery::mock('Illuminate\Routing\ResponseFactory');
$this->response = Mockery::mock('Illuminate\Contracts\Routing\ResponseFactory');
$this->middleware = new GetUserFromToken($this->response, $this->events, $this->auth);

View File

@@ -1,16 +1,24 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Providers\JWT;
use Carbon\Carbon;
use Mockery;
use Tymon\JWTAuth\Payload;
use Tymon\JWTAuth\PayloadFactory;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Claims\Issuer;
use Tymon\JWTAuth\Claims\IssuedAt;
use Tymon\JWTAuth\Claims\Expiration;
use Tymon\JWTAuth\Claims\NotBefore;
use Tymon\JWTAuth\Claims\Audience;
use Tymon\JWTAuth\Claims\Subject;
use Tymon\JWTAuth\Claims\JwtId;
use Tymon\JWTAuth\Claims\Custom;
@@ -19,6 +27,8 @@ class PayloadFactoryTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
Carbon::setTestNow(Carbon::createFromTimeStampUTC(123));
$this->claimFactory = Mockery::mock('Tymon\JWTAuth\Claims\Factory');
$this->validator = Mockery::mock('Tymon\JWTAuth\Validators\PayloadValidator');
$this->factory = new PayloadFactory($this->claimFactory, Request::create('/foo', 'GET'), $this->validator);
@@ -34,13 +44,13 @@ class PayloadFactoryTest extends \PHPUnit_Framework_TestCase
{
$this->validator->shouldReceive('setRefreshFlow->check');
$expTime = time() + 3600;
$expTime = 123 + 3600;
$this->claimFactory->shouldReceive('get')->once()->with('sub', 1)->andReturn(new Subject(1));
$this->claimFactory->shouldReceive('get')->once()->with('iss', Mockery::any())->andReturn(new Issuer('/foo'));
$this->claimFactory->shouldReceive('get')->once()->with('iat', 123)->andReturn(new IssuedAt(123));
$this->claimFactory->shouldReceive('get')->once()->with('jti', 'foo')->andReturn(new JwtId('foo'));
$this->claimFactory->shouldReceive('get')->once()->with('nbf', time())->andReturn(new NotBefore(time()));
$this->claimFactory->shouldReceive('get')->once()->with('nbf', 123)->andReturn(new NotBefore(123));
$this->claimFactory->shouldReceive('get')->once()->with('exp', $expTime)->andReturn(new Expiration($expTime));
$payload = $this->factory->make(['sub' => 1, 'jti' => 'foo', 'iat' => 123]);
@@ -59,10 +69,10 @@ class PayloadFactoryTest extends \PHPUnit_Framework_TestCase
$this->claimFactory->shouldReceive('get')->once()->with('sub', 1)->andReturn(new Subject(1));
$this->claimFactory->shouldReceive('get')->once()->with('iss', Mockery::any())->andReturn(new Issuer('/foo'));
$this->claimFactory->shouldReceive('get')->once()->with('exp', time() + 3600)->andReturn(new Expiration(time() + 3600));
$this->claimFactory->shouldReceive('get')->once()->with('iat', time())->andReturn(new IssuedAt(time()));
$this->claimFactory->shouldReceive('get')->once()->with('exp', 123 + 3600)->andReturn(new Expiration(123 + 3600));
$this->claimFactory->shouldReceive('get')->once()->with('iat', 123)->andReturn(new IssuedAt(123));
$this->claimFactory->shouldReceive('get')->once()->with('jti', Mockery::any())->andReturn(new JwtId('foo'));
$this->claimFactory->shouldReceive('get')->once()->with('nbf', time())->andReturn(new NotBefore(time()));
$this->claimFactory->shouldReceive('get')->once()->with('nbf', 123)->andReturn(new NotBefore(123));
$this->claimFactory->shouldReceive('get')->once()->with('foo', 'baz')->andReturn(new Custom('foo', 'baz'));
$payload = $this->factory->sub(1)->foo('baz')->make();
@@ -81,10 +91,10 @@ class PayloadFactoryTest extends \PHPUnit_Framework_TestCase
$this->claimFactory->shouldReceive('get')->once()->with('sub', 1)->andReturn(new Subject(1));
$this->claimFactory->shouldReceive('get')->once()->with('iss', Mockery::any())->andReturn(new Issuer('/foo'));
$this->claimFactory->shouldReceive('get')->once()->with('exp', Mockery::any())->andReturn(new Expiration(time() + 3600));
$this->claimFactory->shouldReceive('get')->once()->with('iat', Mockery::any())->andReturn(new IssuedAt(time()));
$this->claimFactory->shouldReceive('get')->once()->with('exp', Mockery::any())->andReturn(new Expiration(123 + 3600));
$this->claimFactory->shouldReceive('get')->once()->with('iat', Mockery::any())->andReturn(new IssuedAt(123));
$this->claimFactory->shouldReceive('get')->once()->with('jti', Mockery::any())->andReturn(new JwtId('foo'));
$this->claimFactory->shouldReceive('get')->once()->with('nbf', Mockery::any())->andReturn(new NotBefore(time()));
$this->claimFactory->shouldReceive('get')->once()->with('nbf', Mockery::any())->andReturn(new NotBefore(123));
$this->claimFactory->shouldReceive('get')->once()->with('foo', ['bar' => [0, 0, 0]])->andReturn(new Custom('foo', ['bar' => [0, 0, 0]]));
$payload = $this->factory->sub(1)->foo(['bar' => [0, 0, 0]])->make();

View File

@@ -1,10 +1,18 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Providers\JWT;
use Tymon\JWTAuth\Providers\JWT\FirebaseAdapter;
use Carbon\Carbon;
use Tymon\JWTAuth\Payload;
use Tymon\JWTAuth\PayloadFactory;
use Mockery;
use Tymon\JWTAuth\Claims\Issuer;
use Tymon\JWTAuth\Claims\IssuedAt;
@@ -13,19 +21,20 @@ use Tymon\JWTAuth\Claims\NotBefore;
use Tymon\JWTAuth\Claims\Audience;
use Tymon\JWTAuth\Claims\Subject;
use Tymon\JWTAuth\Claims\JwtId;
use Tymon\JWTAuth\Claims\Custom;
class PayloadTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
Carbon::setTestNow(Carbon::createFromTimeStampUTC(123));
$claims = [
new Subject(1),
new Issuer('http://example.com'),
new Expiration(time() + 3600),
new NotBefore(time()),
new IssuedAt(time()),
new JwtId('foo')
new Expiration(123 + 3600),
new NotBefore(123),
new IssuedAt(123),
new JwtId('foo'),
];
$this->validator = Mockery::mock('Tymon\JWTAuth\Validators\PayloadValidator');

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Providers\Auth;
use Mockery;
@@ -35,10 +44,18 @@ class IlluminateAuthAdapterTest extends \PHPUnit_Framework_TestCase
/** @test */
public function it_should_return_false_if_user_is_not_found()
{
$this->authManager->shouldReceive('onceUsingId')->once()->with(123)->andThrow(new \Exception);
$this->authManager->shouldReceive('onceUsingId')->once()->with(123)->andReturn(false);
$this->assertFalse($this->auth->byId(123));
}
/** @test */
public function it_should_bubble_exceptions_from_auth()
{
$this->authManager->shouldReceive('onceUsingId')->once()->with(123)->andThrow(new \Exception('Some auth failure'));
$this->setExpectedException('Exception', 'Some auth failure');
$this->auth->byId(123);
}
/** @test */
public function it_should_return_the_currently_authenticated_user()
{

View File

@@ -1,9 +1,17 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Providers\JWT;
use Mockery;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Test\Stubs\JWTProviderStub;
class JWTProviderTest extends \PHPUnit_Framework_TestCase

View File

@@ -1,15 +1,26 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Providers\JWT;
use Carbon\Carbon;
use Mockery;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Providers\JWT\NamshiAdapter;
class NamshiAdapterTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
Carbon::setTestNow(Carbon::createFromTimeStampUTC(123));
$this->jws = Mockery::mock('Namshi\JOSE\JWS');
$this->provider = new NamshiAdapter('secret', 'HS256', $this->jws);
}
@@ -22,7 +33,7 @@ class NamshiAdapterTest extends \PHPUnit_Framework_TestCase
/** @test */
public function it_should_return_the_token_when_passing_a_valid_subject_to_encode()
{
$payload = ['sub' => 1, 'exp' => time(), 'iat' => time(), 'iss' => '/foo'];
$payload = ['sub' => 1, 'exp' => 123, 'iat' => 123, 'iss' => '/foo'];
$this->jws->shouldReceive('setPayload')->once()->with($payload)->andReturn(Mockery::self());
$this->jws->shouldReceive('sign')->once()->with('secret')->andReturn(Mockery::self());
@@ -40,8 +51,8 @@ class NamshiAdapterTest extends \PHPUnit_Framework_TestCase
$this->jws->shouldReceive('sign')->andThrow(new \Exception);
$payload = ['sub' => 1, 'exp' => time(), 'iat' => time(), 'iss' => '/foo'];
$token = $this->provider->encode($payload);
$payload = ['sub' => 1, 'exp' => 123, 'iat' => 123, 'iss' => '/foo'];
$this->provider->encode($payload);
}
/** @test */

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Providers\Storage;
use Mockery;

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Providers\User;
use Mockery;

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Stubs;
use Tymon\JWTAuth\Providers\JWT\JWTProvider;

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test\Providers\JWT;
use Tymon\JWTAuth\Token;

View File

@@ -1,14 +1,24 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test;
use Mockery;
use Carbon\Carbon;
use Tymon\JWTAuth\Validators\PayloadValidator;
class PayloadValidatorTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
Carbon::setTestNow(Carbon::createFromTimeStampUTC(123));
$this->validator = new PayloadValidator();
}
@@ -17,11 +27,11 @@ class PayloadValidatorTest extends \PHPUnit_Framework_TestCase
{
$payload = [
'iss' => 'http://example.com',
'iat' => time(),
'nbf' => time(),
'exp' => time() + 3600,
'iat' => 100,
'nbf' => 100,
'exp' => 100 + 3600,
'sub' => 1,
'jti' => 'foo'
'jti' => 'foo',
];
$this->assertTrue($this->validator->isValid($payload));
@@ -34,11 +44,11 @@ class PayloadValidatorTest extends \PHPUnit_Framework_TestCase
$payload = [
'iss' => 'http://example.com',
'iat' => time() - 3660,
'nbf' => time() - 3660,
'exp' => time() - 1440,
'iat' => 20,
'nbf' => 20,
'exp' => 120,
'sub' => 1,
'jti' => 'foo'
'jti' => 'foo',
];
$this->validator->check($payload);
@@ -51,11 +61,11 @@ class PayloadValidatorTest extends \PHPUnit_Framework_TestCase
$payload = [
'iss' => 'http://example.com',
'iat' => time() - 3660,
'nbf' => time() + 3660,
'exp' => time() + 1440,
'iat' => 100,
'nbf' => 150,
'exp' => 150 + 3600,
'sub' => 1,
'jti' => 'foo'
'jti' => 'foo',
];
$this->validator->check($payload);
@@ -68,11 +78,11 @@ class PayloadValidatorTest extends \PHPUnit_Framework_TestCase
$payload = [
'iss' => 'http://example.com',
'iat' => time() + 3660,
'nbf' => time() - 3660,
'exp' => time() + 1440,
'iat' => 150,
'nbf' => 100,
'exp' => 150 + 3600,
'sub' => 1,
'jti' => 'foo'
'jti' => 'foo',
];
$this->validator->check($payload);
@@ -85,7 +95,7 @@ class PayloadValidatorTest extends \PHPUnit_Framework_TestCase
$payload = [
'iss' => 'http://example.com',
'sub' => 1
'sub' => 1,
];
$this->validator->check($payload);
@@ -98,10 +108,10 @@ class PayloadValidatorTest extends \PHPUnit_Framework_TestCase
$payload = [
'iss' => 'http://example.com',
'iat' => time() - 3660,
'iat' => 100,
'exp' => 'foo',
'sub' => 1,
'jti' => 'foo'
'jti' => 'foo',
];
$this->validator->check($payload);

View File

@@ -1,5 +1,14 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test;
use Tymon\JWTAuth\Validators\TokenValidator;