Update v1.0.6
This commit is contained in:
55
vendor/tymon/jwt-auth/src/Middleware/BaseMiddleware.php
vendored
Normal file
55
vendor/tymon/jwt-auth/src/Middleware/BaseMiddleware.php
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Tymon\JWTAuth\Middleware;
|
||||
|
||||
use Tymon\JWTAuth\JWTAuth;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
use Illuminate\Routing\ResponseFactory;
|
||||
|
||||
abstract class BaseMiddleware
|
||||
{
|
||||
/**
|
||||
* @var \Illuminate\Routing\ResponseFactory
|
||||
*/
|
||||
protected $response;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Events\Dispatcher
|
||||
*/
|
||||
protected $events;
|
||||
|
||||
/**
|
||||
* @var \Tymon\JWTAuth\JWTAuth
|
||||
*/
|
||||
protected $auth;
|
||||
|
||||
/**
|
||||
* Create a new BaseMiddleware instance
|
||||
*
|
||||
* @param \Illuminate\Routing\ResponseFactory $response
|
||||
* @param \Illuminate\Events\Dispatcher $events
|
||||
* @param \Tymon\JWTAuth\JWTAuth $auth
|
||||
*/
|
||||
public function __construct(ResponseFactory $response, Dispatcher $events, JWTAuth $auth)
|
||||
{
|
||||
$this->response = $response;
|
||||
$this->events = $events;
|
||||
$this->auth = $auth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire event and return the response
|
||||
*
|
||||
* @param string $event
|
||||
* @param string $error
|
||||
* @param integer $status
|
||||
* @param array $payload
|
||||
* @return mixed
|
||||
*/
|
||||
protected function respond($event, $error, $status, $payload = [])
|
||||
{
|
||||
$response = $this->events->fire($event, $payload, true);
|
||||
|
||||
return $response ?: $this->response->json(['error' => $error], $status);
|
||||
}
|
||||
}
|
||||
39
vendor/tymon/jwt-auth/src/Middleware/GetUserFromToken.php
vendored
Normal file
39
vendor/tymon/jwt-auth/src/Middleware/GetUserFromToken.php
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Tymon\JWTAuth\Middleware;
|
||||
|
||||
use Tymon\JWTAuth\Exceptions\JWTException;
|
||||
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
|
||||
|
||||
class GetUserFromToken extends BaseMiddleware
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
if (! $token = $this->auth->setRequest($request)->getToken()) {
|
||||
return $this->respond('tymon.jwt.absent', 'token_not_provided', 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$user = $this->auth->authenticate($token);
|
||||
} catch (TokenExpiredException $e) {
|
||||
return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
|
||||
} catch (JWTException $e) {
|
||||
return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
|
||||
}
|
||||
|
||||
if (! $user) {
|
||||
return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
|
||||
}
|
||||
|
||||
$this->events->fire('tymon.jwt.valid', $user);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
34
vendor/tymon/jwt-auth/src/Middleware/RefreshToken.php
vendored
Normal file
34
vendor/tymon/jwt-auth/src/Middleware/RefreshToken.php
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Tymon\JWTAuth\Middleware;
|
||||
|
||||
use Tymon\JWTAuth\Exceptions\JWTException;
|
||||
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
|
||||
|
||||
class RefreshToken extends BaseMiddleware
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, \Closure $next)
|
||||
{
|
||||
$response = $next($request);
|
||||
|
||||
try {
|
||||
$newToken = $this->auth->setRequest($request)->parseToken()->refresh();
|
||||
} catch (TokenExpiredException $e) {
|
||||
return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
|
||||
} catch (JWTException $e) {
|
||||
return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
|
||||
}
|
||||
|
||||
// send the refreshed token back to the client
|
||||
$response->headers->set('Authorization', 'Bearer ' . $newToken);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user