diff --git a/lib/private/User/Session.php b/lib/private/User/Session.php index 4018a9c2c674b..95d839c173d42 100644 --- a/lib/private/User/Session.php +++ b/lib/private/User/Session.php @@ -412,10 +412,8 @@ public function logClientIn($user, return false; } - if (!$isTokenPassword && $this->isTokenAuthEnforced()) { - throw new PasswordLoginForbiddenException(); - } - if (!$isTokenPassword && $this->isTwoFactorEnforced($user)) { + if (!$isTokenPassword && ($this->isTokenAuthEnforced() || $this->isTwoFactorEnforced($user))) { + $this->handleLoginFailed($throttler, $currentDelay, $remoteAddress, $user, $password); throw new PasswordLoginForbiddenException(); } @@ -570,7 +568,8 @@ public function tryBasicAuthLogin(IRequest $request, // If credentials were provided, they need to be valid, otherwise we do boom throw new LoginException(); } catch (PasswordLoginForbiddenException $ex) { - // Nothing to do + // If credentials were provided, they need to be valid, otherwise we do boom + throw new LoginException(previous: $ex); } } return false; diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php index a20bdb73506b3..3df957ecc6d74 100644 --- a/tests/lib/User/SessionTest.php +++ b/tests/lib/User/SessionTest.php @@ -440,7 +440,7 @@ public function testLogClientInNoTokenPasswordWith2fa(): void { ->method('getRemoteAddress') ->willReturn('192.168.0.1'); $this->throttler - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('sleepDelayOrThrowOnMax') ->with('192.168.0.1'); $this->throttler @@ -449,6 +449,15 @@ public function testLogClientInNoTokenPasswordWith2fa(): void { ->with('192.168.0.1') ->willReturn(0); + $this->throttler + ->expects($this->once()) + ->method('registerAttempt') + ->with('login', '192.168.0.1', ['user' => 'john']); + $this->dispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with(new LoginFailed('john', 'doe')); + $userSession->logClientIn('john', 'doe', $request, $this->throttler); } @@ -551,7 +560,7 @@ public function testLogClientInNoTokenPasswordNo2fa(): void { ->method('getRemoteAddress') ->willReturn('192.168.0.1'); $this->throttler - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('sleepDelayOrThrowOnMax') ->with('192.168.0.1'); $this->throttler @@ -560,6 +569,15 @@ public function testLogClientInNoTokenPasswordNo2fa(): void { ->with('192.168.0.1') ->willReturn(0); + $this->throttler + ->expects($this->once()) + ->method('registerAttempt') + ->with('login', '192.168.0.1', ['user' => 'john']); + $this->dispatcher + ->expects($this->once()) + ->method('dispatchTyped') + ->with(new LoginFailed('john', 'doe')); + $userSession->logClientIn('john', 'doe', $request, $this->throttler); }