1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace plunner\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Util\Debug; |
6
|
|
|
use Log; |
7
|
|
|
use Tymon\JWTAuth\Exceptions\JWTException; |
8
|
|
|
use Tymon\JWTAuth\Exceptions\TokenExpiredException; |
9
|
|
|
|
10
|
|
|
class GetUserAndRefresh extends BaseMiddleware |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Handle an incoming request. |
14
|
|
|
* If an user mode is set I don't check custom |
15
|
|
|
* |
16
|
|
|
* @param \Illuminate\Http\Request $request |
17
|
|
|
* @param \Closure $next |
18
|
|
|
* @param String $custom custom claims that must be equals (format: key1-ele1;key2-ele2) |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
public function handle($request, \Closure $next, $custom = '') |
22
|
|
|
{ |
23
|
|
|
$custom = $this->convertToArray($custom); |
24
|
|
|
Debug::info('headers: '.implode('-',$request->all())); |
|
|
|
|
25
|
|
|
if($token = $this->auth->setRequest($request)->getToken()) { |
|
|
|
|
26
|
|
|
}else if ($this->auth->getUserModel()){ |
27
|
|
|
$token = $this->auth->fromUser($this->auth->getUserModel(), $custom); |
28
|
|
|
}else { |
29
|
|
|
return $this->respond('tymon.jwt.absent', 'token_not_provided', 401); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
try { |
33
|
|
|
$user = $this->auth->authenticate($token, $custom); |
34
|
|
|
} catch (TokenExpiredException $e) { |
35
|
|
|
return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]); |
36
|
|
|
} catch(InvalidClaimException $e) { |
|
|
|
|
37
|
|
|
return $this->respond('tymon.jwt.invalid', 'claim_invalid', $e->getStatusCode(), [$e]); |
38
|
|
|
} catch (JWTException $e) { |
39
|
|
|
return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (! $user) { |
43
|
|
|
return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* refresh |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
$response = $next($request); |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
$this->events->fire('tymon.jwt.valid', $user); |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$newToken = $this->auth->refresh($token, $custom); |
57
|
|
|
} catch (TokenExpiredException $e) { |
58
|
|
|
return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]); |
59
|
|
|
} catch (JWTException $e) { |
60
|
|
|
return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// send the refreshed token back to the client |
64
|
|
|
$response->headers->set('Authorization', 'Bearer ' . $newToken); |
65
|
|
|
|
66
|
|
|
return $response; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.