Skip to content

Commit 399ca9e

Browse files
Merge pull request #62983 from nextcloud/backport/62982/stable34
[stable34] fix: Handle 2fa enforcement earlier
2 parents 9d33e3b + 3dc0861 commit 399ca9e

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
@@ -412,10 +412,8 @@ public function logClientIn($user,
412412
return false;
413413
}
414414

415-
if (!$isTokenPassword && $this->isTokenAuthEnforced()) {
416-
throw new PasswordLoginForbiddenException();
417-
}
418-
if (!$isTokenPassword && $this->isTwoFactorEnforced($user)) {
415+
if (!$isTokenPassword && ($this->isTokenAuthEnforced() || $this->isTwoFactorEnforced($user))) {
416+
$this->handleLoginFailed($throttler, $currentDelay, $remoteAddress, $user, $password);
419417
throw new PasswordLoginForbiddenException();
420418
}
421419

@@ -570,7 +568,8 @@ public function tryBasicAuthLogin(IRequest $request,
570568
// If credentials were provided, they need to be valid, otherwise we do boom
571569
throw new LoginException();
572570
} catch (PasswordLoginForbiddenException $ex) {
573-
// Nothing to do
571+
// If credentials were provided, they need to be valid, otherwise we do boom
572+
throw new LoginException(previous: $ex);
574573
}
575574
}
576575
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

@@ -551,7 +560,7 @@ public function testLogClientInNoTokenPasswordNo2fa(): void {
551560
->method('getRemoteAddress')
552561
->willReturn('192.168.0.1');
553562
$this->throttler
554-
->expects($this->once())
563+
->expects($this->exactly(2))
555564
->method('sleepDelayOrThrowOnMax')
556565
->with('192.168.0.1');
557566
$this->throttler
@@ -560,6 +569,15 @@ public function testLogClientInNoTokenPasswordNo2fa(): void {
560569
->with('192.168.0.1')
561570
->willReturn(0);
562571

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

0 commit comments

Comments
 (0)