Skip to content

Commit 4d9cc7d

Browse files
danxuliubackportbot[bot]
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 <danxuliu@gmail.com>
1 parent 44ed719 commit 4d9cc7d

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
@@ -31,6 +31,7 @@
3131
use Exception;
3232
use OC\Authentication\Token\IProvider as TokenProvider;
3333
use OCP\Activity\IManager;
34+
use OCP\AppFramework\Db\DoesNotExistException;
3435
use OCP\AppFramework\Utility\ITimeFactory;
3536
use OCP\Authentication\Exceptions\InvalidTokenException;
3637
use OCP\Authentication\TwoFactorAuth\IActivatableAtLogin;
@@ -387,7 +388,10 @@ public function clearTwoFactorPending(string $userId) {
387388
foreach ($tokensNeeding2FA as $tokenId) {
388389
$this->config->deleteUserValue($userId, 'login_token_2fa', $tokenId);
389390

390-
$this->tokenProvider->invalidateTokenById($userId, (int)$tokenId);
391+
try {
392+
$this->tokenProvider->invalidateTokenById($userId, (int)$tokenId);
393+
} catch (DoesNotExistException $e) {
394+
}
391395
}
392396
}
393397
}

tests/lib/Authentication/TwoFactorAuth/ManagerTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OC\Authentication\TwoFactorAuth\ProviderLoader;
3030
use OCP\Activity\IEvent;
3131
use OCP\Activity\IManager;
32+
use OCP\AppFramework\Db\DoesNotExistException;
3233
use OCP\AppFramework\Utility\ITimeFactory;
3334
use OCP\Authentication\TwoFactorAuth\IActivatableAtLogin;
3435
use OCP\Authentication\TwoFactorAuth\IProvider;
@@ -741,4 +742,35 @@ public function testClearTwoFactorPending() {
741742

742743
$this->manager->clearTwoFactorPending('theUserId');
743744
}
745+
746+
public function testClearTwoFactorPendingTokenDoesNotExist() {
747+
$this->config->method('getUserKeys')
748+
->with('theUserId', 'login_token_2fa')
749+
->willReturn([
750+
'42', '43', '44'
751+
]);
752+
753+
$this->config->expects($this->exactly(3))
754+
->method('deleteUserValue')
755+
->withConsecutive(
756+
['theUserId', 'login_token_2fa', '42'],
757+
['theUserId', 'login_token_2fa', '43'],
758+
['theUserId', 'login_token_2fa', '44'],
759+
);
760+
761+
$this->tokenProvider->expects($this->exactly(3))
762+
->method('invalidateTokenById')
763+
->withConsecutive(
764+
['theUserId', 42],
765+
['theUserId', 43],
766+
['theUserId', 44],
767+
)
768+
->willReturnCallback(function ($user, $tokenId) {
769+
if ($tokenId === 43) {
770+
throw new DoesNotExistException('token does not exist');
771+
}
772+
});
773+
774+
$this->manager->clearTwoFactorPending('theUserId');
775+
}
744776
}

0 commit comments

Comments
 (0)