Skip to content

Commit

Permalink
Merge pull request #49096 from nextcloud/backport/48933/stable28
Browse files Browse the repository at this point in the history
[stable28] Clear pending two factor tokens also from configuration
  • Loading branch information
st3iny authored Nov 13, 2024
2 parents 7900683 + b39c5d8 commit 1b0926e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/private/Authentication/TwoFactorAuth/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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;
Expand Down Expand Up @@ -385,7 +386,12 @@ public function clearTwoFactorPending(string $userId) {
$tokensNeeding2FA = $this->config->getUserKeys($userId, 'login_token_2fa');

foreach ($tokensNeeding2FA as $tokenId) {
$this->tokenProvider->invalidateTokenById($userId, (int)$tokenId);
$this->config->deleteUserValue($userId, 'login_token_2fa', $tokenId);

try {
$this->tokenProvider->invalidateTokenById($userId, (int)$tokenId);
} catch (DoesNotExistException $e) {
}
}
}
}
58 changes: 58 additions & 0 deletions tests/lib/Authentication/TwoFactorAuth/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -715,4 +716,61 @@ public function testNeedsSecondFactorAppPassword() {

$this->assertFalse($this->manager->needsSecondFactor($user));
}

public function testClearTwoFactorPending() {
$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],
);

$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');
}
}

0 comments on commit 1b0926e

Please sign in to comment.