Skip to content

Commit 442d036

Browse files
authored
Merge pull request #49098 from nextcloud/backport/48933/stable30
[stable30] Clear pending two factor tokens also from configuration
2 parents 56ce5d0 + fcefd37 commit 442d036

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

lib/private/Authentication/TwoFactorAuth/Manager.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Exception;
1313
use OC\Authentication\Token\IProvider as TokenProvider;
1414
use OCP\Activity\IManager;
15+
use OCP\AppFramework\Db\DoesNotExistException;
1516
use OCP\AppFramework\Utility\ITimeFactory;
1617
use OCP\Authentication\Exceptions\InvalidTokenException;
1718
use OCP\Authentication\TwoFactorAuth\IActivatableAtLogin;
@@ -366,7 +367,12 @@ public function clearTwoFactorPending(string $userId) {
366367
$tokensNeeding2FA = $this->config->getUserKeys($userId, 'login_token_2fa');
367368

368369
foreach ($tokensNeeding2FA as $tokenId) {
369-
$this->tokenProvider->invalidateTokenById($userId, (int)$tokenId);
370+
$this->config->deleteUserValue($userId, 'login_token_2fa', $tokenId);
371+
372+
try {
373+
$this->tokenProvider->invalidateTokenById($userId, (int)$tokenId);
374+
} catch (DoesNotExistException $e) {
375+
}
370376
}
371377
}
372378
}

tests/lib/Authentication/TwoFactorAuth/ManagerTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OC\Authentication\TwoFactorAuth\ProviderLoader;
1616
use OCP\Activity\IEvent;
1717
use OCP\Activity\IManager;
18+
use OCP\AppFramework\Db\DoesNotExistException;
1819
use OCP\AppFramework\Utility\ITimeFactory;
1920
use OCP\Authentication\TwoFactorAuth\IActivatableAtLogin;
2021
use OCP\Authentication\TwoFactorAuth\IProvider;
@@ -701,4 +702,61 @@ public function testNeedsSecondFactorAppPassword() {
701702

702703
$this->assertFalse($this->manager->needsSecondFactor($user));
703704
}
705+
706+
public function testClearTwoFactorPending() {
707+
$this->config->method('getUserKeys')
708+
->with('theUserId', 'login_token_2fa')
709+
->willReturn([
710+
'42', '43', '44'
711+
]);
712+
713+
$this->config->expects($this->exactly(3))
714+
->method('deleteUserValue')
715+
->withConsecutive(
716+
['theUserId', 'login_token_2fa', '42'],
717+
['theUserId', 'login_token_2fa', '43'],
718+
['theUserId', 'login_token_2fa', '44'],
719+
);
720+
721+
$this->tokenProvider->expects($this->exactly(3))
722+
->method('invalidateTokenById')
723+
->withConsecutive(
724+
['theUserId', 42],
725+
['theUserId', 43],
726+
['theUserId', 44],
727+
);
728+
729+
$this->manager->clearTwoFactorPending('theUserId');
730+
}
731+
732+
public function testClearTwoFactorPendingTokenDoesNotExist() {
733+
$this->config->method('getUserKeys')
734+
->with('theUserId', 'login_token_2fa')
735+
->willReturn([
736+
'42', '43', '44'
737+
]);
738+
739+
$this->config->expects($this->exactly(3))
740+
->method('deleteUserValue')
741+
->withConsecutive(
742+
['theUserId', 'login_token_2fa', '42'],
743+
['theUserId', 'login_token_2fa', '43'],
744+
['theUserId', 'login_token_2fa', '44'],
745+
);
746+
747+
$this->tokenProvider->expects($this->exactly(3))
748+
->method('invalidateTokenById')
749+
->withConsecutive(
750+
['theUserId', 42],
751+
['theUserId', 43],
752+
['theUserId', 44],
753+
)
754+
->willReturnCallback(function ($user, $tokenId) {
755+
if ($tokenId === 43) {
756+
throw new DoesNotExistException('token does not exist');
757+
}
758+
});
759+
760+
$this->manager->clearTwoFactorPending('theUserId');
761+
}
704762
}

0 commit comments

Comments
 (0)