PageRenderTime 24ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

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