1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UniSharp\JWT\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; |
8
|
|
|
use Tymon\JWTAuth\Exceptions\JWTException; |
9
|
|
|
use Tymon\JWTAuth\Exceptions\TokenBlacklistedException; |
10
|
|
|
use Tymon\JWTAuth\Exceptions\TokenExpiredException; |
11
|
|
|
use Tymon\JWTAuth\Facades\JWTAuth; |
12
|
|
|
|
13
|
|
|
class JWTRefresh |
14
|
|
|
{ |
15
|
|
|
protected $response; |
16
|
|
|
|
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
// |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Handle an incoming request. |
24
|
|
|
* |
25
|
|
|
* @param \Illuminate\Http\Request $request |
26
|
|
|
* @param \Closure $next |
27
|
|
|
* |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
|
|
public function handle($request, \Closure $next) |
31
|
|
|
{ |
32
|
|
|
$token = $this->authenticate($request); |
|
|
|
|
33
|
|
|
$response = $next($request); |
34
|
|
|
|
35
|
|
|
if ($token) { |
36
|
|
|
$response->header('Authorization', 'Bearer ' . $token); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $response; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Check the request for the presence of a token. |
44
|
|
|
* |
45
|
|
|
* @param \Illuminate\Http\Request $request |
46
|
|
|
* |
47
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function checkForToken(Request $request) |
52
|
|
|
{ |
53
|
|
|
if (!JWTAuth::parser()->setRequest($request)->hasToken()) { |
54
|
|
|
throw new UnauthorizedHttpException('jwt-auth', 'Token not provided'); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Attempt to authenticate a user via the token in the request. |
60
|
|
|
* |
61
|
|
|
* @param \Illuminate\Http\Request $request |
62
|
|
|
* |
63
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function authenticate(Request $request) |
68
|
|
|
{ |
69
|
|
|
if (Auth::user()) { |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->checkForToken($request); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
if (!JWTAuth::parseToken()->authenticate()) { |
77
|
|
|
throw new UnauthorizedHttpException('jwt-auth', 'User not found'); |
78
|
|
|
} |
79
|
|
|
} catch (TokenExpiredException $e) { |
80
|
|
|
// If the token is expired, then it will be refreshed and added to the headers |
81
|
|
|
try { |
82
|
|
|
return Auth::refresh(); |
83
|
|
|
} catch (TokenExpiredException $e) { |
84
|
|
|
throw new UnauthorizedHttpException('jwt-auth', 'Refresh token has expired.'); |
85
|
|
|
} |
86
|
|
|
} catch (TokenBlacklistedException $e) { |
87
|
|
|
throw new TokenBlacklistedException($e->getMessage(), 401); |
88
|
|
|
} catch (JWTException $e) { |
89
|
|
|
throw new UnauthorizedHttpException('jwt-auth', $e->getMessage(), $e, $e->getCode()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.