Skip to content

Commit 6da2ac9

Browse files
Merge pull request #63202 from nextcloud/backport/62985/stable28
[stable28] [stable32] fix: Handle 2fa enforcement earlier
2 parents 37c79bb + 09e61b8 commit 6da2ac9

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
@@ -430,10 +430,8 @@ public function logClientIn($user,
430430
return false;
431431
}
432432

433-
if (!$isTokenPassword && $this->isTokenAuthEnforced()) {
434-
throw new PasswordLoginForbiddenException();
435-
}
436-
if (!$isTokenPassword && $this->isTwoFactorEnforced($user)) {
433+
if (!$isTokenPassword && ($this->isTokenAuthEnforced() || $this->isTwoFactorEnforced($user))) {
434+
$this->handleLoginFailed($throttler, $currentDelay, $remoteAddress, $user, $password);
437435
throw new PasswordLoginForbiddenException();
438436
}
439437

@@ -614,7 +612,8 @@ public function tryBasicAuthLogin(IRequest $request,
614612
// If credentials were provided, they need to be valid, otherwise we do boom
615613
throw new LoginException();
616614
} catch (PasswordLoginForbiddenException $ex) {
617-
// Nothing to do
615+
// If credentials were provided, they need to be valid, otherwise we do boom
616+
throw new LoginException(previous: $ex);
618617
}
619618
}
620619
return false;

tests/lib/User/SessionTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ public function testLogClientInNoTokenPasswordWith2fa() {
441441
->method('getRemoteAddress')
442442
->willReturn('192.168.0.1');
443443
$this->throttler
444-
->expects($this->once())
444+
->expects($this->exactly(2))
445445
->method('sleepDelayOrThrowOnMax')
446446
->with('192.168.0.1');
447447
$this->throttler
@@ -450,6 +450,15 @@ public function testLogClientInNoTokenPasswordWith2fa() {
450450
->with('192.168.0.1')
451451
->willReturn(0);
452452

453+
$this->throttler
454+
->expects($this->once())
455+
->method('registerAttempt')
456+
->with('login', '192.168.0.1', ['user' => 'john']);
457+
$this->dispatcher
458+
->expects($this->once())
459+
->method('dispatchTyped')
460+
->with(new LoginFailed('john', 'doe'));
461+
453462
$userSession->logClientIn('john', 'doe', $request, $this->throttler);
454463
}
455464

@@ -553,7 +562,7 @@ public function testLogClientInNoTokenPasswordNo2fa() {
553562
->method('getRemoteAddress')
554563
->willReturn('192.168.0.1');
555564
$this->throttler
556-
->expects($this->once())
565+
->expects($this->exactly(2))
557566
->method('sleepDelayOrThrowOnMax')
558567
->with('192.168.0.1');
559568
$this->throttler
@@ -562,6 +571,15 @@ public function testLogClientInNoTokenPasswordNo2fa() {
562571
->with('192.168.0.1')
563572
->willReturn(0);
564573

574+
$this->throttler
575+
->expects($this->once())
576+
->method('registerAttempt')
577+
->with('login', '192.168.0.1', ['user' => 'john']);
578+
$this->dispatcher
579+
->expects($this->once())
580+
->method('dispatchTyped')
581+
->with(new LoginFailed('john', 'doe'));
582+
565583
$userSession->logClientIn('john', 'doe', $request, $this->throttler);
566584
}
567585

0 commit comments

Comments
 (0)