@@ -96,6 +96,13 @@ class SessionGuard implements StatefulGuard, SupportsBasicAuth
9696 */
9797 protected $ timebox ;
9898
99+ /**
100+ * The number of microseconds that the timebox should wait for.
101+ *
102+ * @var int
103+ */
104+ protected $ timeboxDuration ;
105+
99106 /**
100107 * Indicates if passwords should be rehashed on login if needed.
101108 *
@@ -126,6 +133,7 @@ class SessionGuard implements StatefulGuard, SupportsBasicAuth
126133 * @param \Symfony\Component\HttpFoundation\Request|null $request
127134 * @param \Illuminate\Support\Timebox|null $timebox
128135 * @param bool $rehashOnLogin
136+ * @param int $timeboxDuration
129137 * @return void
130138 */
131139 public function __construct (
@@ -135,13 +143,15 @@ public function __construct(
135143 ?Request $ request = null ,
136144 ?Timebox $ timebox = null ,
137145 bool $ rehashOnLogin = true ,
146+ int $ timeboxDuration = 200000 ,
138147 ) {
139148 $ this ->name = $ name ;
140149 $ this ->session = $ session ;
141150 $ this ->request = $ request ;
142151 $ this ->provider = $ provider ;
143152 $ this ->timebox = $ timebox ?: new Timebox ;
144153 $ this ->rehashOnLogin = $ rehashOnLogin ;
154+ $ this ->timeboxDuration = $ timeboxDuration ;
145155 }
146156
147157 /**
@@ -291,9 +301,17 @@ public function onceUsingId($id)
291301 */
292302 public function validate (array $ credentials = [])
293303 {
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 );
295306
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 );
297315 }
298316
299317 /**
@@ -391,27 +409,31 @@ protected function failedBasicResponse()
391409 */
392410 public function attempt (array $ credentials = [], $ remember = false )
393411 {
394- $ this ->fireAttemptEvent ($ credentials , $ remember );
412+ return $ this ->timebox ->call (function ($ timebox ) use ($ credentials , $ remember ) {
413+ $ this ->fireAttemptEvent ($ credentials , $ remember );
395414
396- $ this ->lastAttempted = $ user = $ this ->provider ->retrieveByCredentials ($ credentials );
415+ $ this ->lastAttempted = $ user = $ this ->provider ->retrieveByCredentials ($ credentials );
397416
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 );
403422
404- $ this ->login ($ user , $ remember );
423+ $ this ->login ($ user , $ remember );
405424
406- return true ;
407- }
425+ $ timebox ->returnEarly ();
408426
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+ }
413429
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 );
415437 }
416438
417439 /**
@@ -424,24 +446,28 @@ public function attempt(array $credentials = [], $remember = false)
424446 */
425447 public function attemptWhen (array $ credentials = [], $ callbacks = null , $ remember = false )
426448 {
427- $ this ->fireAttemptEvent ($ credentials , $ remember );
449+ return $ this ->timebox ->call (function ($ timebox ) use ($ credentials , $ callbacks , $ remember ) {
450+ $ this ->fireAttemptEvent ($ credentials , $ remember );
428451
429- $ this ->lastAttempted = $ user = $ this ->provider ->retrieveByCredentials ($ credentials );
452+ $ this ->lastAttempted = $ user = $ this ->provider ->retrieveByCredentials ($ credentials );
430453
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 );
436459
437- $ this ->login ($ user , $ remember );
460+ $ this ->login ($ user , $ remember );
438461
439- return true ;
440- }
462+ $ timebox ->returnEarly ();
441463
442- $ this ->fireFailedEvent ($ user , $ credentials );
464+ return true ;
465+ }
443466
444- return false ;
467+ $ this ->fireFailedEvent ($ user , $ credentials );
468+
469+ return false ;
470+ }, $ this ->timeboxDuration );
445471 }
446472
447473 /**
@@ -453,17 +479,13 @@ public function attemptWhen(array $credentials = [], $callbacks = null, $remembe
453479 */
454480 protected function hasValidCredentials ($ user , $ credentials )
455481 {
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 );
461483
462- $ this ->fireValidatedEvent ($ user );
463- }
484+ if ($ validated ) {
485+ $ this ->fireValidatedEvent ($ user );
486+ }
464487
465- return $ validated ;
466- }, 200 * 1000 );
488+ return $ validated ;
467489 }
468490
469491 /**
0 commit comments