| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Auth;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Auth\Events\Attempting;
|
|
|
6 |
use Illuminate\Auth\Events\Authenticated;
|
|
|
7 |
use Illuminate\Auth\Events\CurrentDeviceLogout;
|
|
|
8 |
use Illuminate\Auth\Events\Failed;
|
|
|
9 |
use Illuminate\Auth\Events\Login;
|
|
|
10 |
use Illuminate\Auth\Events\Logout;
|
|
|
11 |
use Illuminate\Auth\Events\OtherDeviceLogout;
|
|
|
12 |
use Illuminate\Auth\Events\Validated;
|
|
|
13 |
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
|
14 |
use Illuminate\Contracts\Auth\StatefulGuard;
|
|
|
15 |
use Illuminate\Contracts\Auth\SupportsBasicAuth;
|
|
|
16 |
use Illuminate\Contracts\Auth\UserProvider;
|
|
|
17 |
use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar;
|
|
|
18 |
use Illuminate\Contracts\Events\Dispatcher;
|
|
|
19 |
use Illuminate\Contracts\Session\Session;
|
|
|
20 |
use Illuminate\Support\Arr;
|
|
|
21 |
use Illuminate\Support\Facades\Hash;
|
|
|
22 |
use Illuminate\Support\Str;
|
|
|
23 |
use Illuminate\Support\Timebox;
|
|
|
24 |
use Illuminate\Support\Traits\Macroable;
|
|
|
25 |
use InvalidArgumentException;
|
|
|
26 |
use RuntimeException;
|
|
|
27 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
28 |
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
|
|
29 |
|
|
|
30 |
class SessionGuard implements StatefulGuard, SupportsBasicAuth
|
|
|
31 |
{
|
|
|
32 |
use GuardHelpers, Macroable;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* The name of the guard. Typically "web".
|
|
|
36 |
*
|
|
|
37 |
* Corresponds to guard name in authentication configuration.
|
|
|
38 |
*
|
|
|
39 |
* @var string
|
|
|
40 |
*/
|
|
|
41 |
protected $name;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* The user we last attempted to retrieve.
|
|
|
45 |
*
|
|
|
46 |
* @var \Illuminate\Contracts\Auth\Authenticatable
|
|
|
47 |
*/
|
|
|
48 |
protected $lastAttempted;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Indicates if the user was authenticated via a recaller cookie.
|
|
|
52 |
*
|
|
|
53 |
* @var bool
|
|
|
54 |
*/
|
|
|
55 |
protected $viaRemember = false;
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* The number of minutes that the "remember me" cookie should be valid for.
|
|
|
59 |
*
|
|
|
60 |
* @var int
|
|
|
61 |
*/
|
|
|
62 |
protected $rememberDuration = 576000;
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* The session used by the guard.
|
|
|
66 |
*
|
|
|
67 |
* @var \Illuminate\Contracts\Session\Session
|
|
|
68 |
*/
|
|
|
69 |
protected $session;
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* The Illuminate cookie creator service.
|
|
|
73 |
*
|
|
|
74 |
* @var \Illuminate\Contracts\Cookie\QueueingFactory
|
|
|
75 |
*/
|
|
|
76 |
protected $cookie;
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* The request instance.
|
|
|
80 |
*
|
|
|
81 |
* @var \Symfony\Component\HttpFoundation\Request
|
|
|
82 |
*/
|
|
|
83 |
protected $request;
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* The event dispatcher instance.
|
|
|
87 |
*
|
|
|
88 |
* @var \Illuminate\Contracts\Events\Dispatcher
|
|
|
89 |
*/
|
|
|
90 |
protected $events;
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* The timebox instance.
|
|
|
94 |
*
|
|
|
95 |
* @var \Illuminate\Support\Timebox
|
|
|
96 |
*/
|
|
|
97 |
protected $timebox;
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Indicates if the logout method has been called.
|
|
|
101 |
*
|
|
|
102 |
* @var bool
|
|
|
103 |
*/
|
|
|
104 |
protected $loggedOut = false;
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Indicates if a token user retrieval has been attempted.
|
|
|
108 |
*
|
|
|
109 |
* @var bool
|
|
|
110 |
*/
|
|
|
111 |
protected $recallAttempted = false;
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Create a new authentication guard.
|
|
|
115 |
*
|
|
|
116 |
* @param string $name
|
|
|
117 |
* @param \Illuminate\Contracts\Auth\UserProvider $provider
|
|
|
118 |
* @param \Illuminate\Contracts\Session\Session $session
|
|
|
119 |
* @param \Symfony\Component\HttpFoundation\Request|null $request
|
|
|
120 |
* @param \Illuminate\Support\Timebox|null $timebox
|
|
|
121 |
* @return void
|
|
|
122 |
*/
|
|
|
123 |
public function __construct($name,
|
|
|
124 |
UserProvider $provider,
|
|
|
125 |
Session $session,
|
|
|
126 |
Request $request = null,
|
|
|
127 |
Timebox $timebox = null)
|
|
|
128 |
{
|
|
|
129 |
$this->name = $name;
|
|
|
130 |
$this->session = $session;
|
|
|
131 |
$this->request = $request;
|
|
|
132 |
$this->provider = $provider;
|
|
|
133 |
$this->timebox = $timebox ?: new Timebox;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Get the currently authenticated user.
|
|
|
138 |
*
|
|
|
139 |
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
140 |
*/
|
|
|
141 |
public function user()
|
|
|
142 |
{
|
|
|
143 |
if ($this->loggedOut) {
|
|
|
144 |
return;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
// If we've already retrieved the user for the current request we can just
|
|
|
148 |
// return it back immediately. We do not want to fetch the user data on
|
|
|
149 |
// every call to this method because that would be tremendously slow.
|
|
|
150 |
if (! is_null($this->user)) {
|
|
|
151 |
return $this->user;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
$id = $this->session->get($this->getName());
|
|
|
155 |
|
|
|
156 |
// First we will try to load the user using the identifier in the session if
|
|
|
157 |
// one exists. Otherwise we will check for a "remember me" cookie in this
|
|
|
158 |
// request, and if one exists, attempt to retrieve the user using that.
|
|
|
159 |
if (! is_null($id) && $this->user = $this->provider->retrieveById($id)) {
|
|
|
160 |
$this->fireAuthenticatedEvent($this->user);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
// If the user is null, but we decrypt a "recaller" cookie we can attempt to
|
|
|
164 |
// pull the user data on that cookie which serves as a remember cookie on
|
|
|
165 |
// the application. Once we have a user we can return it to the caller.
|
|
|
166 |
if (is_null($this->user) && ! is_null($recaller = $this->recaller())) {
|
|
|
167 |
$this->user = $this->userFromRecaller($recaller);
|
|
|
168 |
|
|
|
169 |
if ($this->user) {
|
|
|
170 |
$this->updateSession($this->user->getAuthIdentifier());
|
|
|
171 |
|
|
|
172 |
$this->fireLoginEvent($this->user, true);
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
return $this->user;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* Pull a user from the repository by its "remember me" cookie token.
|
|
|
181 |
*
|
|
|
182 |
* @param \Illuminate\Auth\Recaller $recaller
|
|
|
183 |
* @return mixed
|
|
|
184 |
*/
|
|
|
185 |
protected function userFromRecaller($recaller)
|
|
|
186 |
{
|
|
|
187 |
if (! $recaller->valid() || $this->recallAttempted) {
|
|
|
188 |
return;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
// If the user is null, but we decrypt a "recaller" cookie we can attempt to
|
|
|
192 |
// pull the user data on that cookie which serves as a remember cookie on
|
|
|
193 |
// the application. Once we have a user we can return it to the caller.
|
|
|
194 |
$this->recallAttempted = true;
|
|
|
195 |
|
|
|
196 |
$this->viaRemember = ! is_null($user = $this->provider->retrieveByToken(
|
|
|
197 |
$recaller->id(), $recaller->token()
|
|
|
198 |
));
|
|
|
199 |
|
|
|
200 |
return $user;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Get the decrypted recaller cookie for the request.
|
|
|
205 |
*
|
|
|
206 |
* @return \Illuminate\Auth\Recaller|null
|
|
|
207 |
*/
|
|
|
208 |
protected function recaller()
|
|
|
209 |
{
|
|
|
210 |
if (is_null($this->request)) {
|
|
|
211 |
return;
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
if ($recaller = $this->request->cookies->get($this->getRecallerName())) {
|
|
|
215 |
return new Recaller($recaller);
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* Get the ID for the currently authenticated user.
|
|
|
221 |
*
|
|
|
222 |
* @return int|string|null
|
|
|
223 |
*/
|
|
|
224 |
public function id()
|
|
|
225 |
{
|
|
|
226 |
if ($this->loggedOut) {
|
|
|
227 |
return;
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
return $this->user()
|
|
|
231 |
? $this->user()->getAuthIdentifier()
|
|
|
232 |
: $this->session->get($this->getName());
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
* Log a user into the application without sessions or cookies.
|
|
|
237 |
*
|
|
|
238 |
* @param array $credentials
|
|
|
239 |
* @return bool
|
|
|
240 |
*/
|
|
|
241 |
public function once(array $credentials = [])
|
|
|
242 |
{
|
|
|
243 |
$this->fireAttemptEvent($credentials);
|
|
|
244 |
|
|
|
245 |
if ($this->validate($credentials)) {
|
|
|
246 |
$this->setUser($this->lastAttempted);
|
|
|
247 |
|
|
|
248 |
return true;
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
return false;
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
/**
|
|
|
255 |
* Log the given user ID into the application without sessions or cookies.
|
|
|
256 |
*
|
|
|
257 |
* @param mixed $id
|
|
|
258 |
* @return \Illuminate\Contracts\Auth\Authenticatable|false
|
|
|
259 |
*/
|
|
|
260 |
public function onceUsingId($id)
|
|
|
261 |
{
|
|
|
262 |
if (! is_null($user = $this->provider->retrieveById($id))) {
|
|
|
263 |
$this->setUser($user);
|
|
|
264 |
|
|
|
265 |
return $user;
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
return false;
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
/**
|
|
|
272 |
* Validate a user's credentials.
|
|
|
273 |
*
|
|
|
274 |
* @param array $credentials
|
|
|
275 |
* @return bool
|
|
|
276 |
*/
|
|
|
277 |
public function validate(array $credentials = [])
|
|
|
278 |
{
|
|
|
279 |
$this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);
|
|
|
280 |
|
|
|
281 |
return $this->hasValidCredentials($user, $credentials);
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
/**
|
|
|
285 |
* Attempt to authenticate using HTTP Basic Auth.
|
|
|
286 |
*
|
|
|
287 |
* @param string $field
|
|
|
288 |
* @param array $extraConditions
|
|
|
289 |
* @return \Symfony\Component\HttpFoundation\Response|null
|
|
|
290 |
*
|
|
|
291 |
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
|
|
|
292 |
*/
|
|
|
293 |
public function basic($field = 'email', $extraConditions = [])
|
|
|
294 |
{
|
|
|
295 |
if ($this->check()) {
|
|
|
296 |
return;
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
// If a username is set on the HTTP basic request, we will return out without
|
|
|
300 |
// interrupting the request lifecycle. Otherwise, we'll need to generate a
|
|
|
301 |
// request indicating that the given credentials were invalid for login.
|
|
|
302 |
if ($this->attemptBasic($this->getRequest(), $field, $extraConditions)) {
|
|
|
303 |
return;
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
return $this->failedBasicResponse();
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
/**
|
|
|
310 |
* Perform a stateless HTTP Basic login attempt.
|
|
|
311 |
*
|
|
|
312 |
* @param string $field
|
|
|
313 |
* @param array $extraConditions
|
|
|
314 |
* @return \Symfony\Component\HttpFoundation\Response|null
|
|
|
315 |
*
|
|
|
316 |
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
|
|
|
317 |
*/
|
|
|
318 |
public function onceBasic($field = 'email', $extraConditions = [])
|
|
|
319 |
{
|
|
|
320 |
$credentials = $this->basicCredentials($this->getRequest(), $field);
|
|
|
321 |
|
|
|
322 |
if (! $this->once(array_merge($credentials, $extraConditions))) {
|
|
|
323 |
return $this->failedBasicResponse();
|
|
|
324 |
}
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
/**
|
|
|
328 |
* Attempt to authenticate using basic authentication.
|
|
|
329 |
*
|
|
|
330 |
* @param \Symfony\Component\HttpFoundation\Request $request
|
|
|
331 |
* @param string $field
|
|
|
332 |
* @param array $extraConditions
|
|
|
333 |
* @return bool
|
|
|
334 |
*/
|
|
|
335 |
protected function attemptBasic(Request $request, $field, $extraConditions = [])
|
|
|
336 |
{
|
|
|
337 |
if (! $request->getUser()) {
|
|
|
338 |
return false;
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
return $this->attempt(array_merge(
|
|
|
342 |
$this->basicCredentials($request, $field), $extraConditions
|
|
|
343 |
));
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
/**
|
|
|
347 |
* Get the credential array for an HTTP Basic request.
|
|
|
348 |
*
|
|
|
349 |
* @param \Symfony\Component\HttpFoundation\Request $request
|
|
|
350 |
* @param string $field
|
|
|
351 |
* @return array
|
|
|
352 |
*/
|
|
|
353 |
protected function basicCredentials(Request $request, $field)
|
|
|
354 |
{
|
|
|
355 |
return [$field => $request->getUser(), 'password' => $request->getPassword()];
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
/**
|
|
|
359 |
* Get the response for basic authentication.
|
|
|
360 |
*
|
|
|
361 |
* @return void
|
|
|
362 |
*
|
|
|
363 |
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
|
|
|
364 |
*/
|
|
|
365 |
protected function failedBasicResponse()
|
|
|
366 |
{
|
|
|
367 |
throw new UnauthorizedHttpException('Basic', 'Invalid credentials.');
|
|
|
368 |
}
|
|
|
369 |
|
|
|
370 |
/**
|
|
|
371 |
* Attempt to authenticate a user using the given credentials.
|
|
|
372 |
*
|
|
|
373 |
* @param array $credentials
|
|
|
374 |
* @param bool $remember
|
|
|
375 |
* @return bool
|
|
|
376 |
*/
|
|
|
377 |
public function attempt(array $credentials = [], $remember = false)
|
|
|
378 |
{
|
|
|
379 |
$this->fireAttemptEvent($credentials, $remember);
|
|
|
380 |
|
|
|
381 |
$this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);
|
|
|
382 |
|
|
|
383 |
// If an implementation of UserInterface was returned, we'll ask the provider
|
|
|
384 |
// to validate the user against the given credentials, and if they are in
|
|
|
385 |
// fact valid we'll log the users into the application and return true.
|
|
|
386 |
if ($this->hasValidCredentials($user, $credentials)) {
|
|
|
387 |
$this->login($user, $remember);
|
|
|
388 |
|
|
|
389 |
return true;
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
// If the authentication attempt fails we will fire an event so that the user
|
|
|
393 |
// may be notified of any suspicious attempts to access their account from
|
|
|
394 |
// an unrecognized user. A developer may listen to this event as needed.
|
|
|
395 |
$this->fireFailedEvent($user, $credentials);
|
|
|
396 |
|
|
|
397 |
return false;
|
|
|
398 |
}
|
|
|
399 |
|
|
|
400 |
/**
|
|
|
401 |
* Attempt to authenticate a user with credentials and additional callbacks.
|
|
|
402 |
*
|
|
|
403 |
* @param array $credentials
|
|
|
404 |
* @param array|callable|null $callbacks
|
|
|
405 |
* @param bool $remember
|
|
|
406 |
* @return bool
|
|
|
407 |
*/
|
|
|
408 |
public function attemptWhen(array $credentials = [], $callbacks = null, $remember = false)
|
|
|
409 |
{
|
|
|
410 |
$this->fireAttemptEvent($credentials, $remember);
|
|
|
411 |
|
|
|
412 |
$this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);
|
|
|
413 |
|
|
|
414 |
// This method does the exact same thing as attempt, but also executes callbacks after
|
|
|
415 |
// the user is retrieved and validated. If one of the callbacks returns falsy we do
|
|
|
416 |
// not login the user. Instead, we will fail the specific authentication attempt.
|
|
|
417 |
if ($this->hasValidCredentials($user, $credentials) && $this->shouldLogin($callbacks, $user)) {
|
|
|
418 |
$this->login($user, $remember);
|
|
|
419 |
|
|
|
420 |
return true;
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
$this->fireFailedEvent($user, $credentials);
|
|
|
424 |
|
|
|
425 |
return false;
|
|
|
426 |
}
|
|
|
427 |
|
|
|
428 |
/**
|
|
|
429 |
* Determine if the user matches the credentials.
|
|
|
430 |
*
|
|
|
431 |
* @param mixed $user
|
|
|
432 |
* @param array $credentials
|
|
|
433 |
* @return bool
|
|
|
434 |
*/
|
|
|
435 |
protected function hasValidCredentials($user, $credentials)
|
|
|
436 |
{
|
|
|
437 |
return $this->timebox->call(function ($timebox) use ($user, $credentials) {
|
|
|
438 |
$validated = ! is_null($user) && $this->provider->validateCredentials($user, $credentials);
|
|
|
439 |
|
|
|
440 |
if ($validated) {
|
|
|
441 |
$timebox->returnEarly();
|
|
|
442 |
|
|
|
443 |
$this->fireValidatedEvent($user);
|
|
|
444 |
}
|
|
|
445 |
|
|
|
446 |
return $validated;
|
|
|
447 |
}, 200 * 1000);
|
|
|
448 |
}
|
|
|
449 |
|
|
|
450 |
/**
|
|
|
451 |
* Determine if the user should login by executing the given callbacks.
|
|
|
452 |
*
|
|
|
453 |
* @param array|callable|null $callbacks
|
|
|
454 |
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
455 |
* @return bool
|
|
|
456 |
*/
|
|
|
457 |
protected function shouldLogin($callbacks, AuthenticatableContract $user)
|
|
|
458 |
{
|
|
|
459 |
foreach (Arr::wrap($callbacks) as $callback) {
|
|
|
460 |
if (! $callback($user, $this)) {
|
|
|
461 |
return false;
|
|
|
462 |
}
|
|
|
463 |
}
|
|
|
464 |
|
|
|
465 |
return true;
|
|
|
466 |
}
|
|
|
467 |
|
|
|
468 |
/**
|
|
|
469 |
* Log the given user ID into the application.
|
|
|
470 |
*
|
|
|
471 |
* @param mixed $id
|
|
|
472 |
* @param bool $remember
|
|
|
473 |
* @return \Illuminate\Contracts\Auth\Authenticatable|false
|
|
|
474 |
*/
|
|
|
475 |
public function loginUsingId($id, $remember = false)
|
|
|
476 |
{
|
|
|
477 |
if (! is_null($user = $this->provider->retrieveById($id))) {
|
|
|
478 |
$this->login($user, $remember);
|
|
|
479 |
|
|
|
480 |
return $user;
|
|
|
481 |
}
|
|
|
482 |
|
|
|
483 |
return false;
|
|
|
484 |
}
|
|
|
485 |
|
|
|
486 |
/**
|
|
|
487 |
* Log a user into the application.
|
|
|
488 |
*
|
|
|
489 |
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
490 |
* @param bool $remember
|
|
|
491 |
* @return void
|
|
|
492 |
*/
|
|
|
493 |
public function login(AuthenticatableContract $user, $remember = false)
|
|
|
494 |
{
|
|
|
495 |
$this->updateSession($user->getAuthIdentifier());
|
|
|
496 |
|
|
|
497 |
// If the user should be permanently "remembered" by the application we will
|
|
|
498 |
// queue a permanent cookie that contains the encrypted copy of the user
|
|
|
499 |
// identifier. We will then decrypt this later to retrieve the users.
|
|
|
500 |
if ($remember) {
|
|
|
501 |
$this->ensureRememberTokenIsSet($user);
|
|
|
502 |
|
|
|
503 |
$this->queueRecallerCookie($user);
|
|
|
504 |
}
|
|
|
505 |
|
|
|
506 |
// If we have an event dispatcher instance set we will fire an event so that
|
|
|
507 |
// any listeners will hook into the authentication events and run actions
|
|
|
508 |
// based on the login and logout events fired from the guard instances.
|
|
|
509 |
$this->fireLoginEvent($user, $remember);
|
|
|
510 |
|
|
|
511 |
$this->setUser($user);
|
|
|
512 |
}
|
|
|
513 |
|
|
|
514 |
/**
|
|
|
515 |
* Update the session with the given ID.
|
|
|
516 |
*
|
|
|
517 |
* @param string $id
|
|
|
518 |
* @return void
|
|
|
519 |
*/
|
|
|
520 |
protected function updateSession($id)
|
|
|
521 |
{
|
|
|
522 |
$this->session->put($this->getName(), $id);
|
|
|
523 |
|
|
|
524 |
$this->session->migrate(true);
|
|
|
525 |
}
|
|
|
526 |
|
|
|
527 |
/**
|
|
|
528 |
* Create a new "remember me" token for the user if one doesn't already exist.
|
|
|
529 |
*
|
|
|
530 |
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
531 |
* @return void
|
|
|
532 |
*/
|
|
|
533 |
protected function ensureRememberTokenIsSet(AuthenticatableContract $user)
|
|
|
534 |
{
|
|
|
535 |
if (empty($user->getRememberToken())) {
|
|
|
536 |
$this->cycleRememberToken($user);
|
|
|
537 |
}
|
|
|
538 |
}
|
|
|
539 |
|
|
|
540 |
/**
|
|
|
541 |
* Queue the recaller cookie into the cookie jar.
|
|
|
542 |
*
|
|
|
543 |
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
544 |
* @return void
|
|
|
545 |
*/
|
|
|
546 |
protected function queueRecallerCookie(AuthenticatableContract $user)
|
|
|
547 |
{
|
|
|
548 |
$this->getCookieJar()->queue($this->createRecaller(
|
|
|
549 |
$user->getAuthIdentifier().'|'.$user->getRememberToken().'|'.$user->getAuthPassword()
|
|
|
550 |
));
|
|
|
551 |
}
|
|
|
552 |
|
|
|
553 |
/**
|
|
|
554 |
* Create a "remember me" cookie for a given ID.
|
|
|
555 |
*
|
|
|
556 |
* @param string $value
|
|
|
557 |
* @return \Symfony\Component\HttpFoundation\Cookie
|
|
|
558 |
*/
|
|
|
559 |
protected function createRecaller($value)
|
|
|
560 |
{
|
|
|
561 |
return $this->getCookieJar()->make($this->getRecallerName(), $value, $this->getRememberDuration());
|
|
|
562 |
}
|
|
|
563 |
|
|
|
564 |
/**
|
|
|
565 |
* Log the user out of the application.
|
|
|
566 |
*
|
|
|
567 |
* @return void
|
|
|
568 |
*/
|
|
|
569 |
public function logout()
|
|
|
570 |
{
|
|
|
571 |
$user = $this->user();
|
|
|
572 |
|
|
|
573 |
$this->clearUserDataFromStorage();
|
|
|
574 |
|
|
|
575 |
if (! is_null($this->user) && ! empty($user->getRememberToken())) {
|
|
|
576 |
$this->cycleRememberToken($user);
|
|
|
577 |
}
|
|
|
578 |
|
|
|
579 |
// If we have an event dispatcher instance, we can fire off the logout event
|
|
|
580 |
// so any further processing can be done. This allows the developer to be
|
|
|
581 |
// listening for anytime a user signs out of this application manually.
|
|
|
582 |
if (isset($this->events)) {
|
|
|
583 |
$this->events->dispatch(new Logout($this->name, $user));
|
|
|
584 |
}
|
|
|
585 |
|
|
|
586 |
// Once we have fired the logout event we will clear the users out of memory
|
|
|
587 |
// so they are no longer available as the user is no longer considered as
|
|
|
588 |
// being signed into this application and should not be available here.
|
|
|
589 |
$this->user = null;
|
|
|
590 |
|
|
|
591 |
$this->loggedOut = true;
|
|
|
592 |
}
|
|
|
593 |
|
|
|
594 |
/**
|
|
|
595 |
* Log the user out of the application on their current device only.
|
|
|
596 |
*
|
|
|
597 |
* This method does not cycle the "remember" token.
|
|
|
598 |
*
|
|
|
599 |
* @return void
|
|
|
600 |
*/
|
|
|
601 |
public function logoutCurrentDevice()
|
|
|
602 |
{
|
|
|
603 |
$user = $this->user();
|
|
|
604 |
|
|
|
605 |
$this->clearUserDataFromStorage();
|
|
|
606 |
|
|
|
607 |
// If we have an event dispatcher instance, we can fire off the logout event
|
|
|
608 |
// so any further processing can be done. This allows the developer to be
|
|
|
609 |
// listening for anytime a user signs out of this application manually.
|
|
|
610 |
if (isset($this->events)) {
|
|
|
611 |
$this->events->dispatch(new CurrentDeviceLogout($this->name, $user));
|
|
|
612 |
}
|
|
|
613 |
|
|
|
614 |
// Once we have fired the logout event we will clear the users out of memory
|
|
|
615 |
// so they are no longer available as the user is no longer considered as
|
|
|
616 |
// being signed into this application and should not be available here.
|
|
|
617 |
$this->user = null;
|
|
|
618 |
|
|
|
619 |
$this->loggedOut = true;
|
|
|
620 |
}
|
|
|
621 |
|
|
|
622 |
/**
|
|
|
623 |
* Remove the user data from the session and cookies.
|
|
|
624 |
*
|
|
|
625 |
* @return void
|
|
|
626 |
*/
|
|
|
627 |
protected function clearUserDataFromStorage()
|
|
|
628 |
{
|
|
|
629 |
$this->session->remove($this->getName());
|
|
|
630 |
|
|
|
631 |
$this->getCookieJar()->unqueue($this->getRecallerName());
|
|
|
632 |
|
|
|
633 |
if (! is_null($this->recaller())) {
|
|
|
634 |
$this->getCookieJar()->queue(
|
|
|
635 |
$this->getCookieJar()->forget($this->getRecallerName())
|
|
|
636 |
);
|
|
|
637 |
}
|
|
|
638 |
}
|
|
|
639 |
|
|
|
640 |
/**
|
|
|
641 |
* Refresh the "remember me" token for the user.
|
|
|
642 |
*
|
|
|
643 |
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
644 |
* @return void
|
|
|
645 |
*/
|
|
|
646 |
protected function cycleRememberToken(AuthenticatableContract $user)
|
|
|
647 |
{
|
|
|
648 |
$user->setRememberToken($token = Str::random(60));
|
|
|
649 |
|
|
|
650 |
$this->provider->updateRememberToken($user, $token);
|
|
|
651 |
}
|
|
|
652 |
|
|
|
653 |
/**
|
|
|
654 |
* Invalidate other sessions for the current user.
|
|
|
655 |
*
|
|
|
656 |
* The application must be using the AuthenticateSession middleware.
|
|
|
657 |
*
|
|
|
658 |
* @param string $password
|
|
|
659 |
* @param string $attribute
|
|
|
660 |
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
661 |
*
|
|
|
662 |
* @throws \Illuminate\Auth\AuthenticationException
|
|
|
663 |
*/
|
|
|
664 |
public function logoutOtherDevices($password, $attribute = 'password')
|
|
|
665 |
{
|
|
|
666 |
if (! $this->user()) {
|
|
|
667 |
return;
|
|
|
668 |
}
|
|
|
669 |
|
|
|
670 |
$result = $this->rehashUserPassword($password, $attribute);
|
|
|
671 |
|
|
|
672 |
if ($this->recaller() ||
|
|
|
673 |
$this->getCookieJar()->hasQueued($this->getRecallerName())) {
|
|
|
674 |
$this->queueRecallerCookie($this->user());
|
|
|
675 |
}
|
|
|
676 |
|
|
|
677 |
$this->fireOtherDeviceLogoutEvent($this->user());
|
|
|
678 |
|
|
|
679 |
return $result;
|
|
|
680 |
}
|
|
|
681 |
|
|
|
682 |
/**
|
|
|
683 |
* Rehash the current user's password.
|
|
|
684 |
*
|
|
|
685 |
* @param string $password
|
|
|
686 |
* @param string $attribute
|
|
|
687 |
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
688 |
*
|
|
|
689 |
* @throws \InvalidArgumentException
|
|
|
690 |
*/
|
|
|
691 |
protected function rehashUserPassword($password, $attribute)
|
|
|
692 |
{
|
|
|
693 |
if (! Hash::check($password, $this->user()->{$attribute})) {
|
|
|
694 |
throw new InvalidArgumentException('The given password does not match the current password.');
|
|
|
695 |
}
|
|
|
696 |
|
|
|
697 |
return tap($this->user()->forceFill([
|
|
|
698 |
$attribute => Hash::make($password),
|
|
|
699 |
]))->save();
|
|
|
700 |
}
|
|
|
701 |
|
|
|
702 |
/**
|
|
|
703 |
* Register an authentication attempt event listener.
|
|
|
704 |
*
|
|
|
705 |
* @param mixed $callback
|
|
|
706 |
* @return void
|
|
|
707 |
*/
|
|
|
708 |
public function attempting($callback)
|
|
|
709 |
{
|
|
|
710 |
$this->events?->listen(Events\Attempting::class, $callback);
|
|
|
711 |
}
|
|
|
712 |
|
|
|
713 |
/**
|
|
|
714 |
* Fire the attempt event with the arguments.
|
|
|
715 |
*
|
|
|
716 |
* @param array $credentials
|
|
|
717 |
* @param bool $remember
|
|
|
718 |
* @return void
|
|
|
719 |
*/
|
|
|
720 |
protected function fireAttemptEvent(array $credentials, $remember = false)
|
|
|
721 |
{
|
|
|
722 |
$this->events?->dispatch(new Attempting($this->name, $credentials, $remember));
|
|
|
723 |
}
|
|
|
724 |
|
|
|
725 |
/**
|
|
|
726 |
* Fires the validated event if the dispatcher is set.
|
|
|
727 |
*
|
|
|
728 |
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
729 |
* @return void
|
|
|
730 |
*/
|
|
|
731 |
protected function fireValidatedEvent($user)
|
|
|
732 |
{
|
|
|
733 |
$this->events?->dispatch(new Validated($this->name, $user));
|
|
|
734 |
}
|
|
|
735 |
|
|
|
736 |
/**
|
|
|
737 |
* Fire the login event if the dispatcher is set.
|
|
|
738 |
*
|
|
|
739 |
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
740 |
* @param bool $remember
|
|
|
741 |
* @return void
|
|
|
742 |
*/
|
|
|
743 |
protected function fireLoginEvent($user, $remember = false)
|
|
|
744 |
{
|
|
|
745 |
$this->events?->dispatch(new Login($this->name, $user, $remember));
|
|
|
746 |
}
|
|
|
747 |
|
|
|
748 |
/**
|
|
|
749 |
* Fire the authenticated event if the dispatcher is set.
|
|
|
750 |
*
|
|
|
751 |
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
752 |
* @return void
|
|
|
753 |
*/
|
|
|
754 |
protected function fireAuthenticatedEvent($user)
|
|
|
755 |
{
|
|
|
756 |
$this->events?->dispatch(new Authenticated($this->name, $user));
|
|
|
757 |
}
|
|
|
758 |
|
|
|
759 |
/**
|
|
|
760 |
* Fire the other device logout event if the dispatcher is set.
|
|
|
761 |
*
|
|
|
762 |
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
763 |
* @return void
|
|
|
764 |
*/
|
|
|
765 |
protected function fireOtherDeviceLogoutEvent($user)
|
|
|
766 |
{
|
|
|
767 |
$this->events?->dispatch(new OtherDeviceLogout($this->name, $user));
|
|
|
768 |
}
|
|
|
769 |
|
|
|
770 |
/**
|
|
|
771 |
* Fire the failed authentication attempt event with the given arguments.
|
|
|
772 |
*
|
|
|
773 |
* @param \Illuminate\Contracts\Auth\Authenticatable|null $user
|
|
|
774 |
* @param array $credentials
|
|
|
775 |
* @return void
|
|
|
776 |
*/
|
|
|
777 |
protected function fireFailedEvent($user, array $credentials)
|
|
|
778 |
{
|
|
|
779 |
$this->events?->dispatch(new Failed($this->name, $user, $credentials));
|
|
|
780 |
}
|
|
|
781 |
|
|
|
782 |
/**
|
|
|
783 |
* Get the last user we attempted to authenticate.
|
|
|
784 |
*
|
|
|
785 |
* @return \Illuminate\Contracts\Auth\Authenticatable
|
|
|
786 |
*/
|
|
|
787 |
public function getLastAttempted()
|
|
|
788 |
{
|
|
|
789 |
return $this->lastAttempted;
|
|
|
790 |
}
|
|
|
791 |
|
|
|
792 |
/**
|
|
|
793 |
* Get a unique identifier for the auth session value.
|
|
|
794 |
*
|
|
|
795 |
* @return string
|
|
|
796 |
*/
|
|
|
797 |
public function getName()
|
|
|
798 |
{
|
|
|
799 |
return 'login_'.$this->name.'_'.sha1(static::class);
|
|
|
800 |
}
|
|
|
801 |
|
|
|
802 |
/**
|
|
|
803 |
* Get the name of the cookie used to store the "recaller".
|
|
|
804 |
*
|
|
|
805 |
* @return string
|
|
|
806 |
*/
|
|
|
807 |
public function getRecallerName()
|
|
|
808 |
{
|
|
|
809 |
return 'remember_'.$this->name.'_'.sha1(static::class);
|
|
|
810 |
}
|
|
|
811 |
|
|
|
812 |
/**
|
|
|
813 |
* Determine if the user was authenticated via "remember me" cookie.
|
|
|
814 |
*
|
|
|
815 |
* @return bool
|
|
|
816 |
*/
|
|
|
817 |
public function viaRemember()
|
|
|
818 |
{
|
|
|
819 |
return $this->viaRemember;
|
|
|
820 |
}
|
|
|
821 |
|
|
|
822 |
/**
|
|
|
823 |
* Get the number of minutes the remember me cookie should be valid for.
|
|
|
824 |
*
|
|
|
825 |
* @return int
|
|
|
826 |
*/
|
|
|
827 |
protected function getRememberDuration()
|
|
|
828 |
{
|
|
|
829 |
return $this->rememberDuration;
|
|
|
830 |
}
|
|
|
831 |
|
|
|
832 |
/**
|
|
|
833 |
* Set the number of minutes the remember me cookie should be valid for.
|
|
|
834 |
*
|
|
|
835 |
* @param int $minutes
|
|
|
836 |
* @return $this
|
|
|
837 |
*/
|
|
|
838 |
public function setRememberDuration($minutes)
|
|
|
839 |
{
|
|
|
840 |
$this->rememberDuration = $minutes;
|
|
|
841 |
|
|
|
842 |
return $this;
|
|
|
843 |
}
|
|
|
844 |
|
|
|
845 |
/**
|
|
|
846 |
* Get the cookie creator instance used by the guard.
|
|
|
847 |
*
|
|
|
848 |
* @return \Illuminate\Contracts\Cookie\QueueingFactory
|
|
|
849 |
*
|
|
|
850 |
* @throws \RuntimeException
|
|
|
851 |
*/
|
|
|
852 |
public function getCookieJar()
|
|
|
853 |
{
|
|
|
854 |
if (! isset($this->cookie)) {
|
|
|
855 |
throw new RuntimeException('Cookie jar has not been set.');
|
|
|
856 |
}
|
|
|
857 |
|
|
|
858 |
return $this->cookie;
|
|
|
859 |
}
|
|
|
860 |
|
|
|
861 |
/**
|
|
|
862 |
* Set the cookie creator instance used by the guard.
|
|
|
863 |
*
|
|
|
864 |
* @param \Illuminate\Contracts\Cookie\QueueingFactory $cookie
|
|
|
865 |
* @return void
|
|
|
866 |
*/
|
|
|
867 |
public function setCookieJar(CookieJar $cookie)
|
|
|
868 |
{
|
|
|
869 |
$this->cookie = $cookie;
|
|
|
870 |
}
|
|
|
871 |
|
|
|
872 |
/**
|
|
|
873 |
* Get the event dispatcher instance.
|
|
|
874 |
*
|
|
|
875 |
* @return \Illuminate\Contracts\Events\Dispatcher
|
|
|
876 |
*/
|
|
|
877 |
public function getDispatcher()
|
|
|
878 |
{
|
|
|
879 |
return $this->events;
|
|
|
880 |
}
|
|
|
881 |
|
|
|
882 |
/**
|
|
|
883 |
* Set the event dispatcher instance.
|
|
|
884 |
*
|
|
|
885 |
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
|
|
886 |
* @return void
|
|
|
887 |
*/
|
|
|
888 |
public function setDispatcher(Dispatcher $events)
|
|
|
889 |
{
|
|
|
890 |
$this->events = $events;
|
|
|
891 |
}
|
|
|
892 |
|
|
|
893 |
/**
|
|
|
894 |
* Get the session store used by the guard.
|
|
|
895 |
*
|
|
|
896 |
* @return \Illuminate\Contracts\Session\Session
|
|
|
897 |
*/
|
|
|
898 |
public function getSession()
|
|
|
899 |
{
|
|
|
900 |
return $this->session;
|
|
|
901 |
}
|
|
|
902 |
|
|
|
903 |
/**
|
|
|
904 |
* Return the currently cached user.
|
|
|
905 |
*
|
|
|
906 |
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
907 |
*/
|
|
|
908 |
public function getUser()
|
|
|
909 |
{
|
|
|
910 |
return $this->user;
|
|
|
911 |
}
|
|
|
912 |
|
|
|
913 |
/**
|
|
|
914 |
* Set the current user.
|
|
|
915 |
*
|
|
|
916 |
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
|
|
917 |
* @return $this
|
|
|
918 |
*/
|
|
|
919 |
public function setUser(AuthenticatableContract $user)
|
|
|
920 |
{
|
|
|
921 |
$this->user = $user;
|
|
|
922 |
|
|
|
923 |
$this->loggedOut = false;
|
|
|
924 |
|
|
|
925 |
$this->fireAuthenticatedEvent($user);
|
|
|
926 |
|
|
|
927 |
return $this;
|
|
|
928 |
}
|
|
|
929 |
|
|
|
930 |
/**
|
|
|
931 |
* Get the current request instance.
|
|
|
932 |
*
|
|
|
933 |
* @return \Symfony\Component\HttpFoundation\Request
|
|
|
934 |
*/
|
|
|
935 |
public function getRequest()
|
|
|
936 |
{
|
|
|
937 |
return $this->request ?: Request::createFromGlobals();
|
|
|
938 |
}
|
|
|
939 |
|
|
|
940 |
/**
|
|
|
941 |
* Set the current request instance.
|
|
|
942 |
*
|
|
|
943 |
* @param \Symfony\Component\HttpFoundation\Request $request
|
|
|
944 |
* @return $this
|
|
|
945 |
*/
|
|
|
946 |
public function setRequest(Request $request)
|
|
|
947 |
{
|
|
|
948 |
$this->request = $request;
|
|
|
949 |
|
|
|
950 |
return $this;
|
|
|
951 |
}
|
|
|
952 |
|
|
|
953 |
/**
|
|
|
954 |
* Get the timebox instance used by the guard.
|
|
|
955 |
*
|
|
|
956 |
* @return \Illuminate\Support\Timebox
|
|
|
957 |
*/
|
|
|
958 |
public function getTimebox()
|
|
|
959 |
{
|
|
|
960 |
return $this->timebox;
|
|
|
961 |
}
|
|
|
962 |
}
|