/app/Laravel/Middleware/Api/JWTApiAuth.php

https://bitbucket.org/cityserv/techreportph · PHP · 111 lines · 79 code · 16 blank · 16 comment · 5 complexity · a2d2470504df9e8bf0dccf3f04d76f69 MD5 · raw file

  1. <?php
  2. namespace App\Laravel\Middleware\Api;
  3. use Helper;
  4. use Tymon\JWTAuth\Exceptions\JWTException;
  5. use Tymon\JWTAuth\Exceptions\TokenExpiredException;
  6. use Tymon\JWTAuth\Middleware\BaseMiddleware;
  7. class JWTApiAuth extends BaseMiddleware
  8. {
  9. protected $format;
  10. /**
  11. * Handle an incoming request.
  12. *
  13. * @param \Illuminate\Http\Request $request
  14. * @param \Closure $next
  15. * @return mixed
  16. */
  17. public function handle($request, \Closure $next)
  18. {
  19. $this->format = $request->format;
  20. if (! $token = $this->auth->setRequest($request)->getToken()) {
  21. return $this->respond('tymon.jwt.absent', 'token_not_provided', 400);
  22. }
  23. try {
  24. $user = $this->auth->authenticate($token);
  25. } catch (TokenExpiredException $e) {
  26. return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
  27. } catch (JWTException $e) {
  28. return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
  29. }
  30. if (! $user) {
  31. return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
  32. }
  33. $this->events->fire('tymon.jwt.valid', $user);
  34. $request->merge(['auth' => $user]);
  35. return $next($request);
  36. }
  37. /**
  38. * Fire event and return the response.
  39. *
  40. * @param string $event
  41. * @param string $error
  42. * @param int $status
  43. * @param array $payload
  44. * @return mixed
  45. */
  46. protected function respond($event, $error, $status, $payload = [])
  47. {
  48. $response = array();
  49. switch ($error) {
  50. case 'token_not_provided' :
  51. $response = [
  52. 'msg' => Helper::get_response_message("TOKEN_NOT_PROVIDED"),
  53. 'status' => FALSE,
  54. 'status_code' => "TOKEN_NOT_PROVIDED",
  55. 'hint' => "You can obtain a token in a successful login/register request.",
  56. ];
  57. break;
  58. case 'token_expired' :
  59. $response = [
  60. 'msg' => Helper::get_response_message("TOKEN_EXPIRED"),
  61. 'status' => FALSE,
  62. 'status_code' => "TOKEN_EXPIRED",
  63. 'hint' => "You must try refreshing your token. If this error still occurs, you must re-login.",
  64. ];
  65. break;
  66. case 'token_invalid' :
  67. $response = [
  68. 'msg' => Helper::get_response_message("TOKEN_INVALID"),
  69. 'status' => FALSE,
  70. 'status_code' => "TOKEN_INVALID",
  71. 'hint' => "You can obtain a token in a successful login/register request.",
  72. ];
  73. break;
  74. case 'user_not_found' :
  75. $response = [
  76. 'msg' => Helper::get_response_message("INVALID_AUTH_USER"),
  77. 'status' => FALSE,
  78. 'status_code' => "INVALID_AUTH_USER",
  79. ];
  80. break;
  81. }
  82. $successful = $this->events->fire($event, $payload, true);
  83. if($successful) {
  84. return $successful;
  85. }
  86. switch ($this->format) {
  87. case 'json':
  88. return response()->json($response, 401);
  89. break;
  90. case 'xml':
  91. return response()->xml($response, 401);
  92. break;
  93. }
  94. }
  95. }