package and depencies

This commit is contained in:
RafficMohammed
2023-01-08 02:57:24 +05:30
parent d5332eb421
commit 1d54b8bc7f
4309 changed files with 193331 additions and 172289 deletions

View File

@@ -12,8 +12,6 @@
namespace Tymon\JWTAuth\Providers;
use Illuminate\Support\ServiceProvider;
use Lcobucci\JWT\Builder as JWTBuilder;
use Lcobucci\JWT\Parser as JWTParser;
use Namshi\JOSE\JWS;
use Tymon\JWTAuth\Blacklist;
use Tymon\JWTAuth\Claims\Factory as ClaimFactory;
@@ -27,11 +25,9 @@ use Tymon\JWTAuth\Http\Middleware\AuthenticateAndRenew;
use Tymon\JWTAuth\Http\Middleware\Check;
use Tymon\JWTAuth\Http\Middleware\RefreshToken;
use Tymon\JWTAuth\Http\Parser\AuthHeaders;
use Tymon\JWTAuth\Http\Parser\Cookies;
use Tymon\JWTAuth\Http\Parser\InputSource;
use Tymon\JWTAuth\Http\Parser\Parser;
use Tymon\JWTAuth\Http\Parser\QueryString;
use Tymon\JWTAuth\Http\Parser\RouteParams;
use Tymon\JWTAuth\JWT;
use Tymon\JWTAuth\JWTAuth;
use Tymon\JWTAuth\JWTGuard;
@@ -169,8 +165,6 @@ abstract class AbstractServiceProvider extends ServiceProvider
{
$this->app->singleton('tymon.jwt.provider.jwt.lcobucci', function ($app) {
return new Lcobucci(
new JWTBuilder(),
new JWTParser(),
$this->config('secret'),
$this->config('algo'),
$this->config('keys')
@@ -235,8 +229,6 @@ abstract class AbstractServiceProvider extends ServiceProvider
new AuthHeaders,
new QueryString,
new InputSource,
new RouteParams,
new Cookies($this->config('decrypt_cookies')),
]
);
@@ -354,7 +346,6 @@ abstract class AbstractServiceProvider extends ServiceProvider
*
* @param string $key
* @param string $default
*
* @return mixed
*/
protected function config($key, $default = null)
@@ -366,7 +357,6 @@ abstract class AbstractServiceProvider extends ServiceProvider
* Get an instantiable configuration instance.
*
* @param string $key
*
* @return mixed
*/
protected function getConfigInstance($key)

View File

@@ -27,7 +27,6 @@ class Illuminate implements Auth
* Constructor.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
*
* @return void
*/
public function __construct(GuardContract $auth)
@@ -39,7 +38,6 @@ class Illuminate implements Auth
* Check a user's credentials.
*
* @param array $credentials
*
* @return bool
*/
public function byCredentials(array $credentials)
@@ -51,7 +49,6 @@ class Illuminate implements Auth
* Authenticate a user via the id.
*
* @param mixed $id
*
* @return bool
*/
public function byId($id)

View File

@@ -11,23 +11,19 @@
namespace Tymon\JWTAuth\Providers\JWT;
use DateTimeImmutable;
use DateTimeInterface;
use Exception;
use Illuminate\Support\Collection;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Ecdsa;
use Lcobucci\JWT\Signer\Ecdsa\Sha256 as ES256;
use Lcobucci\JWT\Signer\Ecdsa\Sha384 as ES384;
use Lcobucci\JWT\Signer\Ecdsa\Sha512 as ES512;
use Lcobucci\JWT\Signer\Hmac\Sha256 as HS256;
use Lcobucci\JWT\Signer\Hmac\Sha384 as HS384;
use Lcobucci\JWT\Signer\Hmac\Sha512 as HS512;
use Lcobucci\JWT\Signer\Keychain;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa;
use Lcobucci\JWT\Signer\Rsa\Sha256 as RS256;
use Lcobucci\JWT\Signer\Rsa\Sha384 as RS384;
use Lcobucci\JWT\Signer\Rsa\Sha512 as RS512;
use ReflectionClass;
use Lcobucci\JWT\Token\Builder;
use Lcobucci\JWT\Token\RegisteredClaims;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Tymon\JWTAuth\Contracts\Providers\JWT;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
@@ -35,42 +31,30 @@ use Tymon\JWTAuth\Exceptions\TokenInvalidException;
class Lcobucci extends Provider implements JWT
{
/**
* The Builder instance.
*
* @var \Lcobucci\JWT\Builder
* \Lcobucci\JWT\Signer.
*/
protected $builder;
protected $signer;
/**
* The Parser instance.
*
* @var \Lcobucci\JWT\Parser
* \Lcobucci\JWT\Configuration.
*/
protected $parser;
protected $config;
/**
* Create the Lcobucci provider.
*
* @param \Lcobucci\JWT\Builder $builder
* @param \Lcobucci\JWT\Parser $parser
* @param string $secret
* @param string $algo
* @param array $keys
*
* @param \Lcobucci\JWT\Configuration|null $config
* @return void
*/
public function __construct(
Builder $builder,
Parser $parser,
$secret,
$algo,
array $keys
) {
public function __construct($secret, $algo, array $keys, $config = null)
{
parent::__construct($secret, $algo, $keys);
$this->builder = $builder;
$this->parser = $parser;
$this->signer = $this->getSigner();
$this->config = $config ?: $this->buildConfig();
}
/**
@@ -79,75 +63,141 @@ class Lcobucci extends Provider implements JWT
* @var array
*/
protected $signers = [
'HS256' => HS256::class,
'HS384' => HS384::class,
'HS512' => HS512::class,
'RS256' => RS256::class,
'RS384' => RS384::class,
'RS512' => RS512::class,
'ES256' => ES256::class,
'ES384' => ES384::class,
'ES512' => ES512::class,
self::ALGO_HS256 => Signer\Hmac\Sha256::class,
self::ALGO_HS384 => Signer\Hmac\Sha384::class,
self::ALGO_HS512 => Signer\Hmac\Sha512::class,
self::ALGO_RS256 => Signer\Rsa\Sha256::class,
self::ALGO_RS384 => Signer\Rsa\Sha384::class,
self::ALGO_RS512 => Signer\Rsa\Sha512::class,
self::ALGO_ES256 => Signer\Ecdsa\Sha256::class,
self::ALGO_ES384 => Signer\Ecdsa\Sha384::class,
self::ALGO_ES512 => Signer\Ecdsa\Sha512::class,
];
/**
* Create a JSON Web Token.
*
* @param array $payload
* @return string
*
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*
* @return string
*/
public function encode(array $payload)
{
// Remove the signature on the builder instance first.
$this->builder->unsign();
$builder = $this->getBuilderFromClaims($payload);
try {
foreach ($payload as $key => $value) {
$this->builder->set($key, $value);
}
$this->builder->sign($this->signer, $this->getSigningKey());
return $builder
->getToken($this->config->signer(), $this->config->signingKey())
->toString();
} catch (Exception $e) {
throw new JWTException('Could not create token: '.$e->getMessage(), $e->getCode(), $e);
}
return (string) $this->builder->getToken();
}
/**
* Decode a JSON Web Token.
*
* @param string $token
* @return array
*
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*
* @return array
*/
public function decode($token)
{
try {
$jwt = $this->parser->parse($token);
/** @var \Lcobucci\JWT\Token\Plain */
$token = $this->config->parser()->parse($token);
} catch (Exception $e) {
throw new TokenInvalidException('Could not decode token: '.$e->getMessage(), $e->getCode(), $e);
}
if (! $jwt->verify($this->signer, $this->getVerificationKey())) {
if (! $this->config->validator()->validate($token, ...$this->config->validationConstraints())) {
throw new TokenInvalidException('Token Signature could not be verified.');
}
return (new Collection($jwt->getClaims()))->map(function ($claim) {
return is_object($claim) ? $claim->getValue() : $claim;
})->toArray();
return Collection::wrap($token->claims()->all())
->map(function ($claim) {
if ($claim instanceof DateTimeInterface) {
return $claim->getTimestamp();
}
return is_object($claim) && method_exists($claim, 'getValue')
? $claim->getValue()
: $claim;
})
->toArray();
}
/**
* Create an instance of the builder with all of the claims applied.
*
* @param array $payload
* @return \Lcobucci\JWT\Token\Builder
*/
protected function getBuilderFromClaims(array $payload): Builder
{
$builder = $this->config->builder();
foreach ($payload as $key => $value) {
switch ($key) {
case RegisteredClaims::ID:
$builder->identifiedBy($value);
break;
case RegisteredClaims::EXPIRATION_TIME:
$builder->expiresAt(DateTimeImmutable::createFromFormat('U', $value));
break;
case RegisteredClaims::NOT_BEFORE:
$builder->canOnlyBeUsedAfter(DateTimeImmutable::createFromFormat('U', $value));
break;
case RegisteredClaims::ISSUED_AT:
$builder->issuedAt(DateTimeImmutable::createFromFormat('U', $value));
break;
case RegisteredClaims::ISSUER:
$builder->issuedBy($value);
break;
case RegisteredClaims::AUDIENCE:
$builder->permittedFor($value);
break;
case RegisteredClaims::SUBJECT:
$builder->relatedTo($value);
break;
default:
$builder->withClaim($key, $value);
}
}
return $builder;
}
/**
* Build the configuration.
*
* @return \Lcobucci\JWT\Configuration
*/
protected function buildConfig(): Configuration
{
$config = $this->isAsymmetric()
? Configuration::forAsymmetricSigner(
$this->signer,
$this->getSigningKey(),
$this->getVerificationKey()
)
: Configuration::forSymmetricSigner($this->signer, $this->getSigningKey());
$config->setValidationConstraints(
new SignedWith($this->signer, $this->getVerificationKey())
);
return $config;
}
/**
* Get the signer instance.
*
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*
* @return \Lcobucci\JWT\Signer
*
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*/
protected function getSigner()
{
@@ -155,7 +205,13 @@ class Lcobucci extends Provider implements JWT
throw new JWTException('The given algorithm could not be found');
}
return new $this->signers[$this->algo];
$signer = $this->signers[$this->algo];
if (is_subclass_of($signer, Ecdsa::class)) {
return $signer::create();
}
return new $signer();
}
/**
@@ -163,28 +219,63 @@ class Lcobucci extends Provider implements JWT
*/
protected function isAsymmetric()
{
$reflect = new ReflectionClass($this->signer);
return $reflect->isSubclassOf(Rsa::class) || $reflect->isSubclassOf(Ecdsa::class);
return is_subclass_of($this->signer, Rsa::class)
|| is_subclass_of($this->signer, Ecdsa::class);
}
/**
* {@inheritdoc}
*
* @return \Lcobucci\JWT\Signer\Key
*
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*/
protected function getSigningKey()
{
return $this->isAsymmetric() ?
(new Keychain())->getPrivateKey($this->getPrivateKey(), $this->getPassphrase()) :
$this->getSecret();
if ($this->isAsymmetric()) {
if (! $privateKey = $this->getPrivateKey()) {
throw new JWTException('Private key is not set.');
}
return $this->getKey($privateKey, $this->getPassphrase() ?? '');
}
if (! $secret = $this->getSecret()) {
throw new JWTException('Secret is not set.');
}
return $this->getKey($secret);
}
/**
* {@inheritdoc}
*
* @return \Lcobucci\JWT\Signer\Key
*
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*/
protected function getVerificationKey()
{
return $this->isAsymmetric() ?
(new Keychain())->getPublicKey($this->getPublicKey()) :
$this->getSecret();
if ($this->isAsymmetric()) {
if (! $public = $this->getPublicKey()) {
throw new JWTException('Public key is not set.');
}
return $this->getKey($public);
}
if (! $secret = $this->getSecret()) {
throw new JWTException('Secret is not set.');
}
return $this->getKey($secret);
}
/**
* Get the signing key instance.
*/
protected function getKey(string $contents, string $passphrase = ''): Key
{
return InMemory::plainText($contents, $passphrase);
}
}

View File

@@ -37,7 +37,6 @@ class Namshi extends Provider implements JWT
* @param string $secret
* @param string $algo
* @param array $keys
*
* @return void
*/
public function __construct(JWS $jws, $secret, $algo, array $keys)
@@ -51,10 +50,9 @@ class Namshi extends Provider implements JWT
* Create a JSON Web Token.
*
* @param array $payload
* @return string
*
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*
* @return string
*/
public function encode(array $payload)
{
@@ -71,10 +69,9 @@ class Namshi extends Provider implements JWT
* Decode a JSON Web Token.
*
* @param string $token
* @return array
*
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*
* @return array
*/
public function decode($token)
{

View File

@@ -15,6 +15,16 @@ use Illuminate\Support\Arr;
abstract class Provider
{
const ALGO_HS256 = 'HS256';
const ALGO_HS384 = 'HS384';
const ALGO_HS512 = 'HS512';
const ALGO_RS256 = 'RS256';
const ALGO_RS384 = 'RS384';
const ALGO_RS512 = 'RS512';
const ALGO_ES256 = 'ES256';
const ALGO_ES384 = 'ES384';
const ALGO_ES512 = 'ES512';
/**
* The secret.
*
@@ -42,7 +52,6 @@ abstract class Provider
* @param string $secret
* @param string $algo
* @param array $keys
*
* @return void
*/
public function __construct($secret, $algo, array $keys)
@@ -56,7 +65,6 @@ abstract class Provider
* Set the algorithm used to sign the token.
*
* @param string $algo
*
* @return $this
*/
public function setAlgo($algo)
@@ -80,7 +88,6 @@ abstract class Provider
* Set the secret used to sign the token.
*
* @param string $secret
*
* @return $this
*/
public function setSecret($secret)
@@ -104,7 +111,6 @@ abstract class Provider
* Set the keys used to sign the token.
*
* @param array $keys
*
* @return $this
*/
public function setKeys(array $keys)
@@ -115,8 +121,7 @@ abstract class Provider
}
/**
* Get the array of keys used to sign tokens
* with an asymmetric algorithm.
* Get the array of keys used to sign tokens with an asymmetric algorithm.
*
* @return array
*/
@@ -126,10 +131,9 @@ abstract class Provider
}
/**
* Get the public key used to sign tokens
* with an asymmetric algorithm.
* Get the public key used to sign tokens with an asymmetric algorithm.
*
* @return resource|string
* @return string|null
*/
public function getPublicKey()
{
@@ -137,10 +141,9 @@ abstract class Provider
}
/**
* Get the private key used to sign tokens
* with an asymmetric algorithm.
* Get the private key used to sign tokens with an asymmetric algorithm.
*
* @return resource|string
* @return string|null
*/
public function getPrivateKey()
{
@@ -151,7 +154,7 @@ abstract class Provider
* Get the passphrase used to sign tokens
* with an asymmetric algorithm.
*
* @return string
* @return string|null
*/
public function getPassphrase()
{
@@ -161,7 +164,7 @@ abstract class Provider
/**
* Get the key used to sign the tokens.
*
* @return resource|string
* @return string|null
*/
protected function getSigningKey()
{
@@ -171,7 +174,7 @@ abstract class Provider
/**
* Get the key used to verify the tokens.
*
* @return resource|string
* @return string|null
*/
protected function getVerificationKey()
{
@@ -179,10 +182,7 @@ abstract class Provider
}
/**
* Determine if the algorithm is asymmetric, and thus
* requires a public/private key combo.
*
* @throws \Tymon\JWTAuth\Exceptions\JWTException
* Determine if the algorithm is asymmetric, and thus requires a public/private key combo.
*
* @return bool
*/

View File

@@ -11,6 +11,9 @@
namespace Tymon\JWTAuth\Providers;
use Tymon\JWTAuth\Http\Parser\Cookies;
use Tymon\JWTAuth\Http\Parser\RouteParams;
class LaravelServiceProvider extends AbstractServiceProvider
{
/**
@@ -26,6 +29,11 @@ class LaravelServiceProvider extends AbstractServiceProvider
$this->aliasMiddleware();
$this->extendAuthGuard();
$this->app['tymon.jwt.parser']->addParser([
new RouteParams,
new Cookies($this->config('decrypt_cookies')),
]);
}
/**

View File

@@ -11,10 +11,7 @@
namespace Tymon\JWTAuth\Providers;
use Tymon\JWTAuth\Http\Parser\AuthHeaders;
use Tymon\JWTAuth\Http\Parser\InputSource;
use Tymon\JWTAuth\Http\Parser\LumenRouteParams;
use Tymon\JWTAuth\Http\Parser\QueryString;
class LumenServiceProvider extends AbstractServiceProvider
{
@@ -32,11 +29,6 @@ class LumenServiceProvider extends AbstractServiceProvider
$this->extendAuthGuard();
$this->app['tymon.jwt.parser']->setChain([
new AuthHeaders,
new QueryString,
new InputSource,
new LumenRouteParams,
]);
$this->app['tymon.jwt.parser']->addParser(new LumenRouteParams);
}
}

View File

@@ -46,7 +46,6 @@ class Illuminate implements Storage
* Constructor.
*
* @param \Illuminate\Contracts\Cache\Repository $cache
*
* @return void
*/
public function __construct(CacheContract $cache)
@@ -60,7 +59,6 @@ class Illuminate implements Storage
* @param string $key
* @param mixed $value
* @param int $minutes
*
* @return void
*/
public function add($key, $value, $minutes)
@@ -81,7 +79,6 @@ class Illuminate implements Storage
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function forever($key, $value)
@@ -93,7 +90,6 @@ class Illuminate implements Storage
* Get an item from storage.
*
* @param string $key
*
* @return mixed
*/
public function get($key)
@@ -105,7 +101,6 @@ class Illuminate implements Storage
* Remove an item from storage.
*
* @param string $key
*
* @return bool
*/
public function destroy($key)