diff --git a/src/Auth0/Login/Middleware/Auth0JWTMiddleware.php b/src/Auth0/Login/Middleware/Auth0JWTMiddleware.php index 16e9e168..6bbc3feb 100644 --- a/src/Auth0/Login/Middleware/Auth0JWTMiddleware.php +++ b/src/Auth0/Login/Middleware/Auth0JWTMiddleware.php @@ -12,37 +12,44 @@ public function __construct(Auth0UserRepository $userRepository) { $this->userRepository = $userRepository; } + protected function getToken($request) { + // Get the encrypted user JWT + $authorizationHeader = $request->header("Authorization"); + return trim(str_replace('Bearer ', '', $authorizationHeader)); + } + + protected function validateToken($token) { + return ($token !== ''); + } + public function handle($request, \Closure $next) { $auth0 = \App::make('auth0'); - // Get the encrypted user JWT - $authorizationHeader = $request->header("Authorization"); - $encUser = str_replace('Bearer ', '', $authorizationHeader); - - if (trim($encUser) == '') { + $token = $this->getToken($request); + + if ( ! $this->validateToken($token)) { return \Response::make("Unauthorized user", 401); } - try { - $jwtUser = $auth0->decodeJWT($encUser); - } - catch(CoreException $e) { - return \Response::make("Unauthorized user", 401); - } - catch(Exception $e) { - echo $e;exit; - } + if ($token) { + try { + $jwtUser = $auth0->decodeJWT($token); + } + catch(CoreException $e) { + return \Response::make("Unauthorized user", 401); + } - // if it does not represent a valid user, return a HTTP 401 - $user = $this->userRepository->getUserByDecodedJWT($jwtUser); + // if it does not represent a valid user, return a HTTP 401 + $user = $this->userRepository->getUserByDecodedJWT($jwtUser); - if (!$user) { - return \Response::make("Unauthorized user", 401); - } + if (!$user) { + return \Response::make("Unauthorized user", 401); + } - // lets log the user in so it is accessible - \Auth::login($user); + // lets log the user in so it is accessible + \Auth::login($user); + } // continue the execution return $next($request); diff --git a/src/Auth0/Login/Middleware/Auth0OptionalJWTMiddleware.php b/src/Auth0/Login/Middleware/Auth0OptionalJWTMiddleware.php new file mode 100644 index 00000000..f2bb76f3 --- /dev/null +++ b/src/Auth0/Login/Middleware/Auth0OptionalJWTMiddleware.php @@ -0,0 +1,9 @@ +