Skip to content

Commit 438eccd

Browse files
Merge pull request #63198 from nextcloud/backport/62985/stable31
[stable31] fix: Handle 2fa enforcement earlier
2 parents 11faf68 + 9c8759a commit 438eccd

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
@@ -402,10 +402,8 @@ public function logClientIn($user,
402402
return false;
403403
}
404404

405-
if (!$isTokenPassword && $this->isTokenAuthEnforced()) {
406-
throw new PasswordLoginForbiddenException();
407-
}
408-
if (!$isTokenPassword && $this->isTwoFactorEnforced($user)) {
405+
if (!$isTokenPassword && ($this->isTokenAuthEnforced() || $this->isTwoFactorEnforced($user))) {
406+
$this->handleLoginFailed($throttler, $currentDelay, $remoteAddress, $user, $password);
409407
throw new PasswordLoginForbiddenException();
410408
}
411409

@@ -586,7 +584,8 @@ public function tryBasicAuthLogin(IRequest $request,
586584
// If credentials were provided, they need to be valid, otherwise we do boom
587585
throw new LoginException();
588586
} catch (PasswordLoginForbiddenException $ex) {
589-
// Nothing to do
587+
// If credentials were provided, they need to be valid, otherwise we do boom
588+
throw new LoginException(previous: $ex);
590589
}
591590
}
592591
return false;

tests/lib/User/SessionTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public function testLogClientInNoTokenPasswordWith2fa(): void {
440440
->method('getRemoteAddress')
441441
->willReturn('192.168.0.1');
442442
$this->throttler
443-
->expects($this->once())
443+
->expects($this->exactly(2))
444444
->method('sleepDelayOrThrowOnMax')
445445
->with('192.168.0.1');
446446
$this->throttler
@@ -449,6 +449,15 @@ public function testLogClientInNoTokenPasswordWith2fa(): void {
449449
->with('192.168.0.1')
450450
->willReturn(0);
451451

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

@@ -552,7 +561,7 @@ public function testLogClientInNoTokenPasswordNo2fa(): void {
552561
->method('getRemoteAddress')
553562
->willReturn('192.168.0.1');
554563
$this->throttler
555-
->expects($this->once())
564+
->expects($this->exactly(2))
556565
->method('sleepDelayOrThrowOnMax')
557566
->with('192.168.0.1');
558567
$this->throttler
@@ -561,6 +570,15 @@ public function testLogClientInNoTokenPasswordNo2fa(): void {
561570
->with('192.168.0.1')
562571
->willReturn(0);
563572

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

0 commit comments

Comments
 (0)