Update v1.0.6

This commit is contained in:
Bhanu Slathia
2016-02-16 23:24:52 +05:30
parent c710c20b9e
commit b1f62846ab
7662 changed files with 1361647 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
<?php
namespace Tymon\JWTAuth\Providers\Auth;
interface AuthInterface
{
/**
* Check a user's credentials
*
* @param array $credentials
* @return bool
*/
public function byCredentials(array $credentials = []);
/**
* Authenticate a user via the id
*
* @param mixed $id
* @return bool
*/
public function byId($id);
/**
* Get the currently authenticated user
*
* @return mixed
*/
public function user();
}

View File

@@ -0,0 +1,58 @@
<?php
namespace Tymon\JWTAuth\Providers\Auth;
use Exception;
use Illuminate\Auth\AuthManager;
class IlluminateAuthAdapter implements AuthInterface
{
/**
* @var \Illuminate\Auth\AuthManager
*/
protected $auth;
/**
* @param \Illuminate\Auth\AuthManager $auth
*/
public function __construct(AuthManager $auth)
{
$this->auth = $auth;
}
/**
* Check a user's credentials
*
* @param array $credentials
* @return bool
*/
public function byCredentials(array $credentials = [])
{
return $this->auth->once($credentials);
}
/**
* 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;
}
}
/**
* Get the currently authenticated user
*
* @return mixed
*/
public function user()
{
return $this->auth->user();
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Tymon\JWTAuth\Providers\JWT;
interface JWTInterface
{
/**
* @param array $payload
* @return string
*/
public function encode(array $payload);
/**
* @param string $token
* @return array
*/
public function decode($token);
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Tymon\JWTAuth\Providers\JWT;
abstract class JWTProvider
{
/**
* @var string
*/
protected $secret;
/**
* @var string
*/
protected $algo;
/**
* @param string $secret
* @param string $algo
*/
public function __construct($secret, $algo = 'HS256')
{
$this->secret = $secret;
$this->algo = $algo;
}
/**
* Set the algorithm used to sign the token
*
* @param string $algo
* @return self
*/
public function setAlgo($algo)
{
$this->algo = $algo;
return $this;
}
/**
* Get the algorithm used to sign the token
*
* @return string
*/
public function getAlgo()
{
return $this->algo;
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace Tymon\JWTAuth\Providers\JWT;
use Exception;
use Namshi\JOSE\JWS;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
class NamshiAdapter extends JWTProvider implements JWTInterface
{
/**
* @var \Namshi\JOSE\JWS
*/
protected $jws;
/**
* @param string $secret
* @param string $algo
* @param null $driver
*/
public function __construct($secret, $algo, $driver = null)
{
parent::__construct($secret, $algo);
$this->jws = $driver ?: new JWS(['typ' => 'JWT', 'alg' => $algo]);
}
/**
* Create a JSON Web Token
*
* @return string
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*/
public function encode(array $payload)
{
try {
$this->jws->setPayload($payload)->sign($this->secret);
return $this->jws->getTokenString();
} catch (Exception $e) {
throw new JWTException('Could not create token: ' . $e->getMessage());
}
}
/**
* Decode a JSON Web Token
*
* @param string $token
* @return array
* @throws \Tymon\JWTAuth\Exceptions\JWTException
*/
public function decode($token)
{
try {
$jws = JWS::load($token);
} catch (Exception $e) {
throw new TokenInvalidException('Could not decode token: ' . $e->getMessage());
}
if (! $jws->verify($this->secret, $this->algo)) {
throw new TokenInvalidException('Token Signature could not be verified.');
}
return $jws->getPayload();
}
}

View File

@@ -0,0 +1,267 @@
<?php
namespace Tymon\JWTAuth\Providers;
use Tymon\JWTAuth\JWTAuth;
use Tymon\JWTAuth\Blacklist;
use Tymon\JWTAuth\JWTManager;
use Tymon\JWTAuth\PayloadFactory;
use Tymon\JWTAuth\Claims\Factory;
use Illuminate\Support\ServiceProvider;
use Tymon\JWTAuth\Commands\JWTGenerateCommand;
use Tymon\JWTAuth\Validators\PayloadValidator;
class JWTAuthServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Boot the service provider.
*/
public function boot()
{
$this->publishes([
__DIR__.'/../config/config.php' => config_path('jwt.php')
], 'config');
$this->bootBindings();
$this->commands('tymon.jwt.generate');
}
/**
* Bind some Interfaces and implementations
*/
protected function bootBindings()
{
$this->app['Tymon\JWTAuth\JWTAuth'] = function ($app) {
return $app['tymon.jwt.auth'];
};
$this->app['Tymon\JWTAuth\Providers\User\UserInterface'] = function ($app) {
return $app['tymon.jwt.provider.user'];
};
$this->app['Tymon\JWTAuth\Providers\JWT\JWTInterface'] = function ($app) {
return $app['tymon.jwt.provider.jwt'];
};
$this->app['Tymon\JWTAuth\Providers\Auth\AuthInterface'] = function ($app) {
return $app['tymon.jwt.provider.auth'];
};
$this->app['Tymon\JWTAuth\Providers\Storage\StorageInterface'] = function ($app) {
return $app['tymon.jwt.provider.storage'];
};
$this->app['Tymon\JWTAuth\JWTManager'] = function ($app) {
return $app['tymon.jwt.manager'];
};
$this->app['Tymon\JWTAuth\Blacklist'] = function ($app) {
return $app['tymon.jwt.blacklist'];
};
$this->app['Tymon\JWTAuth\PayloadFactory'] = function ($app) {
return $app['tymon.jwt.payload.factory'];
};
$this->app['Tymon\JWTAuth\Claims\Factory'] = function ($app) {
return $app['tymon.jwt.claim.factory'];
};
$this->app['Tymon\JWTAuth\Validators\PayloadValidator'] = function ($app) {
return $app['tymon.jwt.validators.payload'];
};
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
// register providers
$this->registerUserProvider();
$this->registerJWTProvider();
$this->registerAuthProvider();
$this->registerStorageProvider();
$this->registerJWTBlacklist();
$this->registerClaimFactory();
$this->registerJWTManager();
$this->registerJWTAuth();
$this->registerPayloadValidator();
$this->registerPayloadFactory();
$this->registerJWTCommand();
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'jwt');
}
/**
* Register the bindings for the User provider
*/
protected function registerUserProvider()
{
$this->app['tymon.jwt.provider.user'] = $this->app->share(function ($app) {
return $app->make($this->config('providers.user'), [$app->make($this->config('user'))]);
});
}
/**
* Register the bindings for the JSON Web Token provider
*/
protected function registerJWTProvider()
{
$this->app['tymon.jwt.provider.jwt'] = $this->app->share(function ($app) {
$secret = $this->config('secret');
$algo = $this->config('algo');
$provider = $this->config('providers.jwt');
return $app->make($provider, [$secret, $algo]);
});
}
/**
* Register the bindings for the Auth provider
*/
protected function registerAuthProvider()
{
$this->app['tymon.jwt.provider.auth'] = $this->app->share(function ($app) {
return $this->getConfigInstance($this->config('providers.auth'));
});
}
/**
* Register the bindings for the Storage provider
*/
protected function registerStorageProvider()
{
$this->app['tymon.jwt.provider.storage'] = $this->app->share(function ($app) {
return $this->getConfigInstance($this->config('providers.storage'));
});
}
/**
* Register the bindings for the Payload Factory
*/
protected function registerClaimFactory()
{
$this->app->singleton('tymon.jwt.claim.factory', function () {
return new Factory();
});
}
/**
* Register the bindings for the JWT Manager
*/
protected function registerJWTManager()
{
$this->app['tymon.jwt.manager'] = $this->app->share(function ($app) {
$instance = new JWTManager(
$app['tymon.jwt.provider.jwt'],
$app['tymon.jwt.blacklist'],
$app['tymon.jwt.payload.factory']
);
return $instance->setBlacklistEnabled((bool) $this->config('blacklist_enabled'));
});
}
/**
* Register the bindings for the main JWTAuth class
*/
protected function registerJWTAuth()
{
$this->app['tymon.jwt.auth'] = $this->app->share(function ($app) {
$auth = new JWTAuth(
$app['tymon.jwt.manager'],
$app['tymon.jwt.provider.user'],
$app['tymon.jwt.provider.auth'],
$app['request']
);
return $auth->setIdentifier($this->config('identifier'));
});
}
/**
* 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']);
});
}
/**
* Register the bindings for the payload validator
*/
protected function registerPayloadValidator()
{
$this->app['tymon.jwt.validators.payload'] = $this->app->share(function () {
return with(new PayloadValidator())->setRefreshTTL($this->config('refresh_ttl'))->setRequiredClaims($this->config('required_claims'));
});
}
/**
* Register the bindings for the Payload Factory
*/
protected function registerPayloadFactory()
{
$this->app['tymon.jwt.payload.factory'] = $this->app->share(function ($app) {
$factory = new PayloadFactory($app['tymon.jwt.claim.factory'], $app['request'], $app['tymon.jwt.validators.payload']);
return $factory->setTTL($this->config('ttl'));
});
}
/**
* Register the Artisan command
*/
protected function registerJWTCommand()
{
$this->app['tymon.jwt.generate'] = $this->app->share(function () {
return new JWTGenerateCommand();
});
}
/**
* Helper to get the config values
*
* @param string $key
* @return string
*/
protected function config($key, $default = null)
{
return config("jwt.$key", $default);
}
/**
* Get an instantiable configuration instance. Pinched from dingo/api :)
*
* @param mixed $instance
* @return object
*/
protected function getConfigInstance($instance)
{
if (is_callable($instance)) {
return call_user_func($instance, $this->app);
} elseif (is_string($instance)) {
return $this->app->make($instance);
}
return $instance;
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace Tymon\JWTAuth\Providers\Storage;
use Illuminate\Cache\CacheManager;
use Tymon\JWTAuth\Providers\Storage\StorageInterface;
class IlluminateCacheAdapter implements StorageInterface
{
/**
* @var \Illuminate\Cache\CacheManager
*/
protected $cache;
/**
* @var string
*/
protected $tag = 'tymon.jwt';
/**
* @param \Illuminate\Cache\CacheManager $cache
*/
public function __construct(CacheManager $cache)
{
$this->cache = $cache;
}
/**
* Add a new item into storage
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
public function add($key, $value, $minutes)
{
$this->cache()->put($key, $value, $minutes);
}
/**
* Check whether a key exists in storage
*
* @param string $key
* @return bool
*/
public function has($key)
{
return $this->cache()->has($key);
}
/**
* Remove an item from storage
*
* @param string $key
* @return bool
*/
public function destroy($key)
{
return $this->cache()->forget($key);
}
/**
* Remove all items associated with the tag
*
* @return void
*/
public function flush()
{
$this->cache()->flush();
}
/**
* Return the cache instance with tags attached
*
* @return \Illuminate\Cache\CacheManager
*/
protected function cache()
{
if (! method_exists($this->cache, 'tags')) {
return $this->cache;
}
return $this->cache->tags($this->tag);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Tymon\JWTAuth\Providers\Storage;
interface StorageInterface
{
/**
* @param string $key
* @param integer $minutes
* @return void
*/
public function add($key, $value, $minutes);
/**
* @param string $key
* @return boolean
*/
public function has($key);
/**
* @param string $key
* @return boolean
*/
public function destroy($key);
/**
* @return void
*/
public function flush();
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Tymon\JWTAuth\Providers\User;
use Illuminate\Database\Eloquent\Model;
class EloquentUserAdapter implements UserInterface
{
/**
* @var \Illuminate\Database\Eloquent\Model
*/
protected $user;
/**
* Create a new User instance
*
* @param \Illuminate\Database\Eloquent\Model $user
*/
public function __construct(Model $user)
{
$this->user = $user;
}
/**
* Get the user by the given key, value
*
* @param mixed $key
* @param mixed $value
* @return Illuminate\Database\Eloquent\Model
*/
public function getBy($key, $value)
{
return $this->user->where($key, $value)->first();
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Tymon\JWTAuth\Providers\User;
interface UserInterface
{
/**
* Get the user by the given key, value
*
* @param string $key
* @param mixed $value
* @return Illuminate\Database\Eloquent\Model|null
*/
public function getBy($key, $value);
}