Skip to content

Commit 6d8b2ca

Browse files
Merge pull request #63208 from nextcloud/backport/62985/stable22
[stable22] [stable32] fix: Handle 2fa enforcement earlier
2 parents 2c64cfa + c284816 commit 6d8b2ca

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
@@ -460,10 +460,8 @@ public function logClientIn($user,
460460
return false;
461461
}
462462

463-
if (!$isTokenPassword && $this->isTokenAuthEnforced()) {
464-
throw new PasswordLoginForbiddenException();
465-
}
466-
if (!$isTokenPassword && $this->isTwoFactorEnforced($user)) {
463+
if (!$isTokenPassword && ($this->isTokenAuthEnforced() || $this->isTwoFactorEnforced($user))) {
464+
$this->handleLoginFailed($throttler, $currentDelay, $remoteAddress, $user, $password);
467465
throw new PasswordLoginForbiddenException();
468466
}
469467

@@ -636,7 +634,8 @@ public function tryBasicAuthLogin(IRequest $request,
636634
// If credentials were provided, they need to be valid, otherwise we do boom
637635
throw new LoginException();
638636
} catch (PasswordLoginForbiddenException $ex) {
639-
// Nothing to do
637+
// If credentials were provided, they need to be valid, otherwise we do boom
638+
throw new LoginException(previous: $ex);
640639
}
641640
}
642641
return false;

tests/lib/User/SessionTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ public function testLogClientInNoTokenPasswordWith2fa() {
449449
->method('getRemoteAddress')
450450
->willReturn('192.168.0.1');
451451
$this->throttler
452-
->expects($this->once())
452+
->expects($this->exactly(2))
453453
->method('sleepDelayOrThrowOnMax')
454454
->with('192.168.0.1');
455455
$this->throttler
@@ -458,6 +458,15 @@ public function testLogClientInNoTokenPasswordWith2fa() {
458458
->with('192.168.0.1')
459459
->willReturn(0);
460460

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

@@ -561,7 +570,7 @@ public function testLogClientInNoTokenPasswordNo2fa() {
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() {
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)