aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Calviño Sánchez <danxuliu@gmail.com>2024-10-28 10:15:16 +0100
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>2024-11-05 10:50:29 +0000
commitfcefd37a5f7e24208a115214660d419cbdeae7f3 (patch)
tree08a9e085d2ddeb0c4ddf420724eabee036ac8a83
parentc7b5c3e02f1d2f9d7dc49ee094cc4420673f4e9b (diff)
downloadnextcloud-server-backport/48933/stable30.tar.gz
nextcloud-server-backport/48933/stable30.zip
fix: Handle exception when clearing previously removed two factor tokensbackport/48933/stable30
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>
-rw-r--r--lib/private/Authentication/TwoFactorAuth/Manager.php6
-rw-r--r--tests/lib/Authentication/TwoFactorAuth/ManagerTest.php32
2 files changed, 37 insertions, 1 deletions
diff --git a/lib/private/Authentication/TwoFactorAuth/Manager.php b/lib/private/Authentication/TwoFactorAuth/Manager.php
index f4b3b88c50b..39d886132dd 100644
--- a/lib/private/Authentication/TwoFactorAuth/Manager.php
+++ b/lib/private/Authentication/TwoFactorAuth/Manager.php
@@ -12,6 +12,7 @@ use BadMethodCallException;
use Exception;
use OC\Authentication\Token\IProvider as TokenProvider;
use OCP\Activity\IManager;
+use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Authentication\TwoFactorAuth\IActivatableAtLogin;
@@ -368,7 +369,10 @@ class Manager {
foreach ($tokensNeeding2FA as $tokenId) {
$this->config->deleteUserValue($userId, 'login_token_2fa', $tokenId);
- $this->tokenProvider->invalidateTokenById($userId, (int)$tokenId);
+ try {
+ $this->tokenProvider->invalidateTokenById($userId, (int)$tokenId);
+ } catch (DoesNotExistException $e) {
+ }
}
}
}
diff --git a/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php b/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php
index 4f4dbaccaff..a574299642a 100644
--- a/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php
+++ b/tests/lib/Authentication/TwoFactorAuth/ManagerTest.php
@@ -15,6 +15,7 @@ use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
use OC\Authentication\TwoFactorAuth\ProviderLoader;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
+use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\TwoFactorAuth\IActivatableAtLogin;
use OCP\Authentication\TwoFactorAuth\IProvider;
@@ -727,4 +728,35 @@ class ManagerTest extends TestCase {
$this->manager->clearTwoFactorPending('theUserId');
}
+
+ public function testClearTwoFactorPendingTokenDoesNotExist() {
+ $this->config->method('getUserKeys')
+ ->with('theUserId', 'login_token_2fa')
+ ->willReturn([
+ '42', '43', '44'
+ ]);
+
+ $this->config->expects($this->exactly(3))
+ ->method('deleteUserValue')
+ ->withConsecutive(
+ ['theUserId', 'login_token_2fa', '42'],
+ ['theUserId', 'login_token_2fa', '43'],
+ ['theUserId', 'login_token_2fa', '44'],
+ );
+
+ $this->tokenProvider->expects($this->exactly(3))
+ ->method('invalidateTokenById')
+ ->withConsecutive(
+ ['theUserId', 42],
+ ['theUserId', 43],
+ ['theUserId', 44],
+ )
+ ->willReturnCallback(function ($user, $tokenId) {
+ if ($tokenId === 43) {
+ throw new DoesNotExistException('token does not exist');
+ }
+ });
+
+ $this->manager->clearTwoFactorPending('theUserId');
+ }
}