Skip to content

Commit cd12e73

Browse files
committed
fix: Handle 2fa enforcement earlier
Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent 6666934 commit cd12e73

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

lib/private/User/Session.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,8 @@ public function logClientIn($user,
418418
return false;
419419
}
420420

421-
if (!$isTokenPassword && $this->isTokenAuthEnforced()) {
422-
throw new PasswordLoginForbiddenException();
423-
}
424-
if (!$isTokenPassword && $this->isTwoFactorEnforced($user)) {
421+
if (!$isTokenPassword && ($this->isTokenAuthEnforced() || $this->isTwoFactorEnforced($user))) {
422+
$this->handleLoginFailed($throttler, $currentDelay, $remoteAddress, $user, $password);
425423
throw new PasswordLoginForbiddenException();
426424
}
427425

@@ -576,7 +574,8 @@ public function tryBasicAuthLogin(IRequest $request,
576574
// If credentials were provided, they need to be valid, otherwise we do boom
577575
throw new LoginException();
578576
} catch (PasswordLoginForbiddenException $ex) {
579-
// Nothing to do
577+
// If credentials were provided, they need to be valid, otherwise we do boom
578+
throw new LoginException(previous: $ex);
580579
}
581580
}
582581
return false;

tests/lib/User/SessionTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ public function testLogClientInNoTokenPasswordWith2fa(): void {
451451
->method('getRemoteAddress')
452452
->willReturn('192.168.0.1');
453453
$this->throttler
454-
->expects($this->once())
454+
->expects($this->exactly(2))
455455
->method('sleepDelayOrThrowOnMax')
456456
->with('192.168.0.1');
457457
$this->throttler
@@ -460,6 +460,15 @@ public function testLogClientInNoTokenPasswordWith2fa(): void {
460460
->with('192.168.0.1')
461461
->willReturn(0);
462462

463+
$this->throttler
464+
->expects($this->once())
465+
->method('registerAttempt')
466+
->with('login', '192.168.0.1', ['user' => 'john']);
467+
$this->dispatcher
468+
->expects($this->once())
469+
->method('dispatchTyped')
470+
->with(new LoginFailed('john', 'doe'));
471+
463472
$userSession->logClientIn('john', 'doe', $request, $this->throttler);
464473
}
465474

@@ -561,7 +570,7 @@ public function testLogClientInNoTokenPasswordNo2fa(): void {
561570
->method('getRemoteAddress')
562571
->willReturn('192.168.0.1');
563572
$this->throttler
564-
->expects($this->once())
573+
->expects($this->exactly(2))
565574
->method('sleepDelayOrThrowOnMax')
566575
->with('192.168.0.1');
567576
$this->throttler
@@ -570,6 +579,15 @@ public function testLogClientInNoTokenPasswordNo2fa(): void {
570579
->with('192.168.0.1')
571580
->willReturn(0);
572581

582+
$this->throttler
583+
->expects($this->once())
584+
->method('registerAttempt')
585+
->with('login', '192.168.0.1', ['user' => 'john']);
586+
$this->dispatcher
587+
->expects($this->once())
588+
->method('dispatchTyped')
589+
->with(new LoginFailed('john', 'doe'));
590+
573591
$userSession->logClientIn('john', 'doe', $request, $this->throttler);
574592
}
575593

0 commit comments

Comments
 (0)