1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of jwt-auth |
5
|
|
|
* |
6
|
|
|
* (c) Sean Tymon <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tymon\JWTAuth; |
13
|
|
|
|
14
|
|
|
use Illuminate\Http\Request; |
15
|
|
|
use Illuminate\Auth\GuardHelpers; |
16
|
|
|
use Illuminate\Contracts\Auth\Guard; |
17
|
|
|
use Illuminate\Contracts\Auth\UserProvider; |
18
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
19
|
|
|
|
20
|
|
|
class JWTGuard implements Guard |
21
|
|
|
{ |
22
|
|
|
use GuardHelpers; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The user we last attempted to retrieve. |
26
|
|
|
* |
27
|
|
|
* @var \Illuminate\Contracts\Auth\Authenticatable |
28
|
|
|
*/ |
29
|
|
|
protected $lastAttempted; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The JWTAuth instance. |
33
|
|
|
* |
34
|
|
|
* @var \Tymon\JWTAuth\JWTAuth |
35
|
|
|
*/ |
36
|
|
|
protected $auth; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The request instance. |
40
|
|
|
* |
41
|
|
|
* @var \Illuminate\Http\Request |
42
|
|
|
*/ |
43
|
|
|
protected $request; |
44
|
|
|
|
45
|
|
|
public function __construct(JWTAuth $auth, UserProvider $provider, Request $request) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$this->auth = $auth; |
48
|
|
|
$this->provider = $provider; |
49
|
|
|
$this->request = $request; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the currently authenticated user. |
54
|
|
|
* |
55
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null |
56
|
|
|
*/ |
57
|
|
|
public function user() |
58
|
|
|
{ |
59
|
|
|
if (! is_null($this->user)) { |
60
|
|
|
return $this->user; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (! $this->requireToken()->check()) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$id = $this->auth->getPayload()->get('sub'); |
68
|
|
|
|
69
|
|
|
return $this->user = $this->provider->retrieveById($id); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Validate a user's credentials. |
74
|
|
|
* |
75
|
|
|
* @param array $credentials |
76
|
|
|
* |
77
|
|
|
* @return boolean |
78
|
|
|
*/ |
79
|
|
|
public function validate(array $credentials = [], $login = true) |
80
|
|
|
{ |
81
|
|
|
$this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials); |
82
|
|
|
|
83
|
|
|
if ($this->hasValidCredentials($user, $credentials)) { |
84
|
|
|
|
85
|
|
|
if ($login) { |
86
|
|
|
$this->setUser($user); |
|
|
|
|
87
|
|
|
|
88
|
|
|
return $this->auth->fromUser($user); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Logout the user, thus invalidating the token. |
99
|
|
|
* |
100
|
|
|
* @param boolean $forceForever |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function logout($forceForever = false) |
105
|
|
|
{ |
106
|
|
|
$this->requireToken()->invalidate($forceForever); |
107
|
|
|
|
108
|
|
|
$this->user = null; |
109
|
|
|
$this->auth->unsetToken(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the user provider used by the guard. |
114
|
|
|
* |
115
|
|
|
* @return \Illuminate\Contracts\Auth\UserProvider |
116
|
|
|
*/ |
117
|
|
|
public function getProvider() |
118
|
|
|
{ |
119
|
|
|
return $this->provider; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set the user provider used by the guard. |
124
|
|
|
* |
125
|
|
|
* @param \Illuminate\Contracts\Auth\UserProvider $provider |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function setProvider(UserProvider $provider) |
130
|
|
|
{ |
131
|
|
|
$this->provider = $provider; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Return the currently cached user. |
136
|
|
|
* |
137
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null |
138
|
|
|
*/ |
139
|
|
|
public function getUser() |
140
|
|
|
{ |
141
|
|
|
return $this->user; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the current request instance. |
146
|
|
|
* |
147
|
|
|
* @return \Symfony\Component\HttpFoundation\Request |
148
|
|
|
*/ |
149
|
|
|
public function getRequest() |
150
|
|
|
{ |
151
|
|
|
return $this->request ?: Request::createFromGlobals(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set the current request instance. |
156
|
|
|
* |
157
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
158
|
|
|
* |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
|
|
public function setRequest(Request $request) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$this->request = $request; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the last user we attempted to authenticate. |
170
|
|
|
* |
171
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable |
172
|
|
|
*/ |
173
|
|
|
public function getLastAttempted() |
174
|
|
|
{ |
175
|
|
|
return $this->lastAttempted; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Determine if the user matches the credentials. |
180
|
|
|
* |
181
|
|
|
* @param mixed $user |
182
|
|
|
* @param array $credentials |
183
|
|
|
* |
184
|
|
|
* @return boolean |
185
|
|
|
*/ |
186
|
|
|
protected function hasValidCredentials($user, $credentials) |
187
|
|
|
{ |
188
|
|
|
return ! is_null($user) && $this->provider->validateCredentials($user, $credentials); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Ensure that a token is available in the request |
193
|
|
|
* |
194
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
195
|
|
|
* |
196
|
|
|
* @return \Tymon\JWTAuth\JWTAuth |
197
|
|
|
*/ |
198
|
|
|
protected function requireToken() |
199
|
|
|
{ |
200
|
|
|
if (! $this->auth->getToken()) { |
201
|
|
|
throw new BadRequestHttpException($e->getMessage()); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this->auth; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|