1 | <?php |
||
21 | class JWTAuth |
||
22 | { |
||
23 | use CustomClaims; |
||
24 | |||
25 | /** |
||
26 | * @var \Tymon\JWTAuth\Manager |
||
27 | */ |
||
28 | protected $manager; |
||
29 | |||
30 | /** |
||
31 | * @var \Tymon\JWTAuth\Contracts\Providers\Auth |
||
32 | */ |
||
33 | protected $auth; |
||
34 | |||
35 | /** |
||
36 | * @var \Tymon\JWTAuth\Http\TokenParser |
||
37 | */ |
||
38 | protected $parser; |
||
39 | |||
40 | /** |
||
41 | * @var \Tymon\JWTAuth\Token |
||
42 | */ |
||
43 | protected $token; |
||
44 | |||
45 | /** |
||
46 | * @param \Tymon\JWTAuth\Manager $manager |
||
47 | * @param \Tymon\JWTAuth\Contracts\Providers\Auth $auth |
||
48 | * @param \Tymon\JWTAuth\Http\TokenParser $parser |
||
49 | */ |
||
50 | 38 | public function __construct(Manager $manager, Auth $auth, TokenParser $parser) |
|
51 | { |
||
52 | 38 | $this->manager = $manager; |
|
53 | 38 | $this->auth = $auth; |
|
54 | 38 | $this->parser = $parser; |
|
55 | 38 | } |
|
56 | |||
57 | /** |
||
58 | * Generate a token using the user identifier as the subject claim. |
||
59 | * |
||
60 | * @param \Tymon\JWTAuth\Contracts\JWTSubject $user |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | 4 | public function fromUser(JWTSubject $user) |
|
70 | |||
71 | /** |
||
72 | * Attempt to authenticate the user and return the token. |
||
73 | * |
||
74 | * @param array $credentials |
||
75 | * |
||
76 | * @return false|string |
||
77 | */ |
||
78 | 4 | public function attempt(array $credentials) |
|
86 | |||
87 | /** |
||
88 | * Authenticate a user via a token. |
||
89 | * |
||
90 | * @return \Tymon\JWTAuth\Contracts\JWTSubject|false |
||
91 | */ |
||
92 | 6 | public function authenticate() |
|
102 | |||
103 | /** |
||
104 | * Alias for authenticate(). |
||
105 | * |
||
106 | * @return \Tymon\JWTAuth\Contracts\JWTSubject|false |
||
107 | */ |
||
108 | 2 | public function toUser() |
|
112 | |||
113 | /** |
||
114 | * Refresh an expired token. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 2 | public function refresh() |
|
124 | |||
125 | /** |
||
126 | * Invalidate a token (add it to the blacklist). |
||
127 | * |
||
128 | * @param boolean $forceForever |
||
129 | * |
||
130 | * @return boolean |
||
131 | */ |
||
132 | 4 | public function invalidate($forceForever = false) |
|
138 | |||
139 | /** |
||
140 | * Alias to get the payload, and as a result checks that |
||
141 | * the token is valid i.e. not expired or blacklisted |
||
142 | * |
||
143 | * @throws JWTException |
||
144 | * |
||
145 | * @return \Tymon\JWTAuth\Payload |
||
146 | */ |
||
147 | 4 | public function checkOrFail() |
|
151 | |||
152 | /** |
||
153 | * Check that the token is valid |
||
154 | * |
||
155 | * @return boolean |
||
156 | */ |
||
157 | 4 | public function check() |
|
167 | |||
168 | /** |
||
169 | * Get the token. |
||
170 | * |
||
171 | * @return false|Token |
||
172 | */ |
||
173 | 6 | public function getToken() |
|
185 | |||
186 | /** |
||
187 | * Parse the token from the request. |
||
188 | * |
||
189 | * @throws \Tymon\JWTAuth\Exceptions\JWTException |
||
190 | * |
||
191 | * @return JWTAuth |
||
192 | */ |
||
193 | 12 | public function parseToken() |
|
194 | { |
||
195 | 12 | if (! $token = $this->parser->parseToken()) { |
|
196 | 4 | throw new JWTException('The token could not be parsed from the request'); |
|
197 | } |
||
198 | |||
199 | 8 | return $this->setToken($token); |
|
|
|||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Get the raw Payload instance. |
||
204 | * |
||
205 | * @return \Tymon\JWTAuth\Payload |
||
206 | */ |
||
207 | 10 | public function getPayload() |
|
213 | |||
214 | /** |
||
215 | * Create a Payload instance. |
||
216 | * |
||
217 | * @param \Tymon\JWTAuth\Contracts\JWTSubject $user |
||
218 | * |
||
219 | * @return \Tymon\JWTAuth\Payload |
||
220 | */ |
||
221 | 4 | public function makePayload(JWTSubject $user) |
|
222 | { |
||
223 | 4 | return $this->factory()->customClaims($this->getClaimsArray($user))->make(); |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Build the claims array and return it |
||
228 | * |
||
229 | * @param \Tymon\JWTAuth\Contracts\JWTSubject $user |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | 4 | protected function getClaimsArray(JWTSubject $user) |
|
241 | |||
242 | /** |
||
243 | * Get the authenticated user |
||
244 | * |
||
245 | * @return \Tymon\JWTAuth\Contracts\JWTSubject |
||
246 | */ |
||
247 | 4 | public function user() |
|
251 | |||
252 | /** |
||
253 | * Set the token. |
||
254 | * |
||
255 | * @param Token|string $token |
||
256 | * |
||
257 | * @return JWTAuth |
||
258 | */ |
||
259 | 18 | public function setToken($token) |
|
265 | |||
266 | /** |
||
267 | * Remove the current token. |
||
268 | */ |
||
269 | public function unsetToken() |
||
270 | { |
||
271 | $this->token = null; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Ensure that a token is available. |
||
276 | * |
||
277 | * @throws \Tymon\JWTAuth\Exceptions\JWTException |
||
278 | */ |
||
279 | 16 | protected function requireToken() |
|
285 | |||
286 | /** |
||
287 | * Set the request instance. |
||
288 | * |
||
289 | * @param \Illuminate\Http\Request $request |
||
290 | * |
||
291 | * @return JWTAuth |
||
292 | */ |
||
293 | 2 | public function setRequest(Request $request) |
|
299 | |||
300 | /** |
||
301 | * Get the Manager instance. |
||
302 | * |
||
303 | * @return \Tymon\JWTAuth\Manager |
||
304 | */ |
||
305 | 4 | public function manager() |
|
309 | |||
310 | /** |
||
311 | * Get the TokenParser instance |
||
312 | * |
||
313 | * @return \Tymon\JWTAuth\Http\TokenParser |
||
314 | */ |
||
315 | 2 | public function parser() |
|
319 | |||
320 | /** |
||
321 | * Get the Payload Factory |
||
322 | * |
||
323 | * @return \Tymon\JWTAuth\Factory |
||
324 | */ |
||
325 | 4 | public function factory() |
|
329 | |||
330 | /** |
||
331 | * Get the Blacklist |
||
332 | * |
||
333 | * @return \Tymon\JWTAuth\Blacklist |
||
334 | */ |
||
335 | public function blacklist() |
||
339 | |||
340 | /** |
||
341 | * Magically call the JWT Manager. |
||
342 | * |
||
343 | * @param string $method |
||
344 | * @param array $parameters |
||
345 | * |
||
346 | * @return mixed |
||
347 | * |
||
348 | * @throws \BadMethodCallException |
||
349 | */ |
||
350 | 2 | public function __call($method, $parameters) |
|
358 | } |
||
359 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.