Skip to content

Commit 5ea5b2d

Browse files
danxuliunickvergessen
authored andcommitted
fix: Handle exception when clearing previously removed two factor tokens
If a token was already removed from the database but not from the configuration clearing the tokens will try to remove it again from the database, which caused a DoesNotExistException to be thrown. Signed-off-by: Daniel Calviño Sánchez <[email protected]>
1 parent 381a2aa commit 5ea5b2d

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/private/Authentication/TwoFactorAuth/Manager.php

Lines changed: 5 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;
@@ -368,7 +369,10 @@ public function clearTwoFactorPending(string $userId) {
368369
foreach ($tokensNeeding2FA as $tokenId) {
369370
$this->config->deleteUserValue($userId, 'login_token_2fa', $tokenId);
370371

371-
$this->tokenProvider->invalidateTokenById($userId, (int)$tokenId);
372+
try {
373+
$this->tokenProvider->invalidateTokenById($userId, (int)$tokenId);
374+
} catch (DoesNotExistException $e) {
375+
}
372376
}
373377
}
374378
}

tests/lib/Authentication/TwoFactorAuth/ManagerTest.php

Lines changed: 32 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;
@@ -727,4 +728,35 @@ public function testClearTwoFactorPending() {
727728

728729
$this->manager->clearTwoFactorPending('theUserId');
729730
}
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+
}
730762
}

0 commit comments

Comments
 (0)