1 | <?php |
||
12 | class JWTAuth |
||
13 | { |
||
14 | /** |
||
15 | * @var \Tymon\JWTAuth\JWTManager |
||
16 | */ |
||
17 | protected $manager; |
||
18 | |||
19 | /** |
||
20 | * @var \Tymon\JWTAuth\Providers\User\UserInterface |
||
21 | */ |
||
22 | protected $user; |
||
23 | |||
24 | /** |
||
25 | * @var \Tymon\JWTAuth\Providers\Auth\AuthInterface |
||
26 | */ |
||
27 | protected $auth; |
||
28 | |||
29 | /** |
||
30 | * @var \Illuminate\Http\Request |
||
31 | */ |
||
32 | protected $request; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $identifier = 'id'; |
||
38 | |||
39 | /** |
||
40 | * @var \Tymon\JWTAuth\Token |
||
41 | */ |
||
42 | protected $token; |
||
43 | |||
44 | |||
45 | /** |
||
46 | * @var \Illuminate\Contracts\Auth\Authenticatable |
||
47 | */ |
||
48 | protected $userModel = null; |
||
49 | |||
50 | /** |
||
51 | * @param \Tymon\JWTAuth\JWTManager $manager |
||
52 | * @param \Tymon\JWTAuth\Providers\User\UserInterface $user |
||
53 | * @param \Tymon\JWTAuth\Providers\Auth\AuthInterface $auth |
||
54 | * @param \Illuminate\Http\Request $request |
||
55 | */ |
||
56 | 54 | public function __construct(JWTManager $manager, UserInterface $user, AuthInterface $auth, Request $request) |
|
63 | |||
64 | /** |
||
65 | * @return \Illuminate\Contracts\Auth\Authenticatable |
||
66 | */ |
||
67 | public function getUserModel() |
||
68 | { |
||
69 | return $this->userModel; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param \Illuminate\Contracts\Auth\Authenticatable $userModel |
||
74 | */ |
||
75 | public function setUserModel(Authenticatable $userModel) |
||
79 | |||
80 | /** |
||
81 | * @param $userModel |
||
82 | */ |
||
83 | 12 | private function setUserModelAsObject ($userModel) |
|
87 | |||
88 | |||
89 | |||
90 | /** |
||
91 | * Find a user using the user identifier in the subject claim. |
||
92 | * |
||
93 | * @param bool|string $token |
||
94 | * |
||
95 | * @return mixed |
||
96 | */ |
||
97 | 9 | public function toUser($token = false) |
|
108 | |||
109 | /** |
||
110 | * Generate a token using the user identifier as the subject claim. |
||
111 | * |
||
112 | * @param mixed $user |
||
113 | * @param array $customClaims |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | 6 | public function fromUser($user, array $customClaims = []) |
|
125 | |||
126 | /** |
||
127 | * Attempt to authenticate the user and return the token. |
||
128 | * |
||
129 | * @param array $credentials |
||
130 | * @param array $customClaims |
||
131 | * |
||
132 | * @return false|string |
||
133 | * @throws JWTException |
||
134 | */ |
||
135 | 6 | public function attempt(array $credentials = [], array $customClaims = []) |
|
143 | |||
144 | /** |
||
145 | * Authenticate a user via a token. |
||
146 | * |
||
147 | * @param mixed $token |
||
148 | * @param Array $custom custom claims that must be equals (all custom fields indicated must be equals in token, this doesn't entail that the token must have only these claims) |
||
149 | * @return mixed |
||
150 | */ |
||
151 | 6 | public function authenticate($token = false, $custom = []) |
|
169 | |||
170 | /** |
||
171 | * Refresh an expired token. |
||
172 | * |
||
173 | * @param mixed $token |
||
174 | * @param Array $custom |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | 3 | public function refresh($token = false, $custom = []) |
|
184 | |||
185 | /** |
||
186 | * Invalidate a token (add it to the blacklist). |
||
187 | * |
||
188 | * @param mixed $token |
||
189 | * |
||
190 | * @return boolean |
||
191 | */ |
||
192 | 3 | public function invalidate($token = false) |
|
198 | |||
199 | /** |
||
200 | * Get the token. |
||
201 | * |
||
202 | * @return boolean|string |
||
203 | */ |
||
204 | 12 | public function getToken() |
|
216 | |||
217 | /** |
||
218 | * Get the raw Payload instance. |
||
219 | * |
||
220 | * @param mixed $token |
||
221 | * |
||
222 | * @return \Tymon\JWTAuth\Payload |
||
223 | */ |
||
224 | 15 | public function getPayload($token = false) |
|
230 | |||
231 | /** |
||
232 | * Parse the token from the request. |
||
233 | * @param string $method |
||
234 | * @param string $header |
||
235 | * @param string $query |
||
236 | * @return JWTAuth |
||
237 | * @throws JWTException |
||
238 | */ |
||
239 | 15 | public function parseToken($method = 'bearer', $header = 'authorization', $query = 'token') |
|
249 | |||
250 | /** |
||
251 | * Parse token from the authorization header. |
||
252 | * |
||
253 | * @param string $header |
||
254 | * @param string $method |
||
255 | * |
||
256 | * @return false|string |
||
257 | */ |
||
258 | 15 | protected function parseAuthHeader($header = 'authorization', $method = 'bearer') |
|
268 | |||
269 | /** |
||
270 | * Create a Payload instance. |
||
271 | * |
||
272 | * @param mixed $subject |
||
273 | * @param array $customClaims |
||
274 | * |
||
275 | * @return \Tymon\JWTAuth\Payload |
||
276 | */ |
||
277 | 6 | protected function makePayload($subject, array $customClaims = []) |
|
283 | |||
284 | /** |
||
285 | * Set the identifier. |
||
286 | * |
||
287 | * @param string $identifier |
||
288 | * |
||
289 | * @return $this |
||
290 | */ |
||
291 | 3 | public function setIdentifier($identifier) |
|
297 | |||
298 | /** |
||
299 | * Get the identifier. |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | 3 | public function getIdentifier() |
|
307 | |||
308 | /** |
||
309 | * Set the token. |
||
310 | * |
||
311 | * @param string $token |
||
312 | * |
||
313 | * @return $this |
||
314 | */ |
||
315 | 27 | public function setToken($token) |
|
321 | |||
322 | /** |
||
323 | * Ensure that a token is available. |
||
324 | * |
||
325 | * @param mixed $token |
||
326 | * |
||
327 | * @return JWTAuth |
||
328 | * |
||
329 | * @throws \Tymon\JWTAuth\Exceptions\JWTException |
||
330 | */ |
||
331 | 21 | protected function requireToken($token) |
|
339 | |||
340 | /** |
||
341 | * Set the request instance. |
||
342 | * |
||
343 | * @param Request $request |
||
344 | * @return $this |
||
345 | */ |
||
346 | 3 | public function setRequest(Request $request) |
|
352 | |||
353 | /** |
||
354 | * Get the JWTManager instance. |
||
355 | * |
||
356 | * @return \Tymon\JWTAuth\JWTManager |
||
357 | */ |
||
358 | 3 | public function manager() |
|
362 | |||
363 | /** |
||
364 | * Magically call the JWT Manager. |
||
365 | * |
||
366 | * @param string $method |
||
367 | * @param array $parameters |
||
368 | * |
||
369 | * @return mixed |
||
370 | * |
||
371 | * @throws \BadMethodCallException |
||
372 | */ |
||
373 | 3 | public function __call($method, $parameters) |
|
381 | } |
||
382 |