@@ -96,6 +96,13 @@ class SessionGuard implements StatefulGuard, SupportsBasicAuth
96
96
*/
97
97
protected $ timebox ;
98
98
99
+ /**
100
+ * The number of microseconds that the timebox should wait for.
101
+ *
102
+ * @var int
103
+ */
104
+ protected $ timeboxDuration ;
105
+
99
106
/**
100
107
* Indicates if passwords should be rehashed on login if needed.
101
108
*
@@ -126,6 +133,7 @@ class SessionGuard implements StatefulGuard, SupportsBasicAuth
126
133
* @param \Symfony\Component\HttpFoundation\Request|null $request
127
134
* @param \Illuminate\Support\Timebox|null $timebox
128
135
* @param bool $rehashOnLogin
136
+ * @param int $timeboxDuration
129
137
* @return void
130
138
*/
131
139
public function __construct (
@@ -135,13 +143,15 @@ public function __construct(
135
143
?Request $ request = null ,
136
144
?Timebox $ timebox = null ,
137
145
bool $ rehashOnLogin = true ,
146
+ int $ timeboxDuration = 200000 ,
138
147
) {
139
148
$ this ->name = $ name ;
140
149
$ this ->session = $ session ;
141
150
$ this ->request = $ request ;
142
151
$ this ->provider = $ provider ;
143
152
$ this ->timebox = $ timebox ?: new Timebox ;
144
153
$ this ->rehashOnLogin = $ rehashOnLogin ;
154
+ $ this ->timeboxDuration = $ timeboxDuration ;
145
155
}
146
156
147
157
/**
@@ -291,9 +301,17 @@ public function onceUsingId($id)
291
301
*/
292
302
public function validate (array $ credentials = [])
293
303
{
294
- $ this ->lastAttempted = $ user = $ this ->provider ->retrieveByCredentials ($ credentials );
304
+ return $ this ->timebox ->call (function ($ timebox ) use ($ credentials ) {
305
+ $ this ->lastAttempted = $ user = $ this ->provider ->retrieveByCredentials ($ credentials );
295
306
296
- return $ this ->hasValidCredentials ($ user , $ credentials );
307
+ $ validated = $ this ->hasValidCredentials ($ user , $ credentials );
308
+
309
+ if ($ validated ) {
310
+ $ timebox ->returnEarly ();
311
+ }
312
+
313
+ return $ validated ;
314
+ }, $ this ->timeboxDuration );
297
315
}
298
316
299
317
/**
@@ -391,27 +409,31 @@ protected function failedBasicResponse()
391
409
*/
392
410
public function attempt (array $ credentials = [], $ remember = false )
393
411
{
394
- $ this ->fireAttemptEvent ($ credentials , $ remember );
412
+ return $ this ->timebox ->call (function ($ timebox ) use ($ credentials , $ remember ) {
413
+ $ this ->fireAttemptEvent ($ credentials , $ remember );
395
414
396
- $ this ->lastAttempted = $ user = $ this ->provider ->retrieveByCredentials ($ credentials );
415
+ $ this ->lastAttempted = $ user = $ this ->provider ->retrieveByCredentials ($ credentials );
397
416
398
- // If an implementation of UserInterface was returned, we'll ask the provider
399
- // to validate the user against the given credentials, and if they are in
400
- // fact valid we'll log the users into the application and return true.
401
- if ($ this ->hasValidCredentials ($ user , $ credentials )) {
402
- $ this ->rehashPasswordIfRequired ($ user , $ credentials );
417
+ // If an implementation of UserInterface was returned, we'll ask the provider
418
+ // to validate the user against the given credentials, and if they are in
419
+ // fact valid we'll log the users into the application and return true.
420
+ if ($ this ->hasValidCredentials ($ user , $ credentials )) {
421
+ $ this ->rehashPasswordIfRequired ($ user , $ credentials );
403
422
404
- $ this ->login ($ user , $ remember );
423
+ $ this ->login ($ user , $ remember );
405
424
406
- return true ;
407
- }
425
+ $ timebox ->returnEarly ();
408
426
409
- // If the authentication attempt fails we will fire an event so that the user
410
- // may be notified of any suspicious attempts to access their account from
411
- // an unrecognized user. A developer may listen to this event as needed.
412
- $ this ->fireFailedEvent ($ user , $ credentials );
427
+ return true ;
428
+ }
413
429
414
- return false ;
430
+ // If the authentication attempt fails we will fire an event so that the user
431
+ // may be notified of any suspicious attempts to access their account from
432
+ // an unrecognized user. A developer may listen to this event as needed.
433
+ $ this ->fireFailedEvent ($ user , $ credentials );
434
+
435
+ return false ;
436
+ }, $ this ->timeboxDuration );
415
437
}
416
438
417
439
/**
@@ -424,24 +446,28 @@ public function attempt(array $credentials = [], $remember = false)
424
446
*/
425
447
public function attemptWhen (array $ credentials = [], $ callbacks = null , $ remember = false )
426
448
{
427
- $ this ->fireAttemptEvent ($ credentials , $ remember );
449
+ return $ this ->timebox ->call (function ($ timebox ) use ($ credentials , $ callbacks , $ remember ) {
450
+ $ this ->fireAttemptEvent ($ credentials , $ remember );
428
451
429
- $ this ->lastAttempted = $ user = $ this ->provider ->retrieveByCredentials ($ credentials );
452
+ $ this ->lastAttempted = $ user = $ this ->provider ->retrieveByCredentials ($ credentials );
430
453
431
- // This method does the exact same thing as attempt, but also executes callbacks after
432
- // the user is retrieved and validated. If one of the callbacks returns falsy we do
433
- // not login the user. Instead, we will fail the specific authentication attempt.
434
- if ($ this ->hasValidCredentials ($ user , $ credentials ) && $ this ->shouldLogin ($ callbacks , $ user )) {
435
- $ this ->rehashPasswordIfRequired ($ user , $ credentials );
454
+ // This method does the exact same thing as attempt, but also executes callbacks after
455
+ // the user is retrieved and validated. If one of the callbacks returns falsy we do
456
+ // not login the user. Instead, we will fail the specific authentication attempt.
457
+ if ($ this ->hasValidCredentials ($ user , $ credentials ) && $ this ->shouldLogin ($ callbacks , $ user )) {
458
+ $ this ->rehashPasswordIfRequired ($ user , $ credentials );
436
459
437
- $ this ->login ($ user , $ remember );
460
+ $ this ->login ($ user , $ remember );
438
461
439
- return true ;
440
- }
462
+ $ timebox ->returnEarly ();
441
463
442
- $ this ->fireFailedEvent ($ user , $ credentials );
464
+ return true ;
465
+ }
443
466
444
- return false ;
467
+ $ this ->fireFailedEvent ($ user , $ credentials );
468
+
469
+ return false ;
470
+ }, $ this ->timeboxDuration );
445
471
}
446
472
447
473
/**
@@ -453,17 +479,13 @@ public function attemptWhen(array $credentials = [], $callbacks = null, $remembe
453
479
*/
454
480
protected function hasValidCredentials ($ user , $ credentials )
455
481
{
456
- return $ this ->timebox ->call (function ($ timebox ) use ($ user , $ credentials ) {
457
- $ validated = ! is_null ($ user ) && $ this ->provider ->validateCredentials ($ user , $ credentials );
458
-
459
- if ($ validated ) {
460
- $ timebox ->returnEarly ();
482
+ $ validated = ! is_null ($ user ) && $ this ->provider ->validateCredentials ($ user , $ credentials );
461
483
462
- $ this ->fireValidatedEvent ($ user );
463
- }
484
+ if ($ validated ) {
485
+ $ this ->fireValidatedEvent ($ user );
486
+ }
464
487
465
- return $ validated ;
466
- }, 200 * 1000 );
488
+ return $ validated ;
467
489
}
468
490
469
491
/**
0 commit comments