Skip to content

Commit c80421c

Browse files
Merge pull request #63203 from nextcloud/backport/62985/stable27
[stable27] [stable32] fix: Handle 2fa enforcement earlier
2 parents 7216e34 + c35c4b5 commit c80421c

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

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

@@ -612,7 +610,8 @@ public function tryBasicAuthLogin(IRequest $request,
612610
// If credentials were provided, they need to be valid, otherwise we do boom
613611
throw new LoginException();
614612
} catch (PasswordLoginForbiddenException $ex) {
615-
// Nothing to do
613+
// If credentials were provided, they need to be valid, otherwise we do boom
614+
throw new LoginException(previous: $ex);
616615
}
617616
}
618617
return false;

tests/lib/User/SessionTest.php

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

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

@@ -554,7 +563,7 @@ public function testLogClientInNoTokenPasswordNo2fa() {
554563
->method('getRemoteAddress')
555564
->willReturn('192.168.0.1');
556565
$this->throttler
557-
->expects($this->once())
566+
->expects($this->exactly(2))
558567
->method('sleepDelayOrThrowOnMax')
559568
->with('192.168.0.1');
560569
$this->throttler
@@ -563,6 +572,15 @@ public function testLogClientInNoTokenPasswordNo2fa() {
563572
->with('192.168.0.1')
564573
->willReturn(0);
565574

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

0 commit comments

Comments
 (0)