Clear pending two factor tokens also from configuration #48933
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Clearing pending two factor tokens removed them from the database, but not from the configuration. Due to that, if the password was reset again, the pending tokens were got again from the configuration and tried to be cleared again from the database, which caused an exception to be thrown.
The fix is split in two commits. The first one removes the token from the configuration as well as from the database, and the second commits handles the exception thrown if a token was already removed from the database but not from the configuration, as it could be the case on an existing instance.
Note that
invalidateTokenById
does not declare any exception to be thrown, and theDoesNotExistException
comes from thePublicKeyTokenMapper
. Therefore rather than catching the exception inclearTwoFactorPending
it might be better to catch it ininvalidateTokenById
(or to transform it into another exception, as right now a database exception is being exposed to the upper layers). However I am not familiar at all with this code and any possible side effect of doing that, so I just went the safe route (specially given that this should be backported) and caught it inclearTwoFactorPending
.How to test:
LostController::setException
:Result with this pull request:
The password is reset and the login page is shown again
Result without this pull request:
token does not exist is shown below the request password form. However, if the login page is open and the new password is used it works. In the logs it can be seen that the exception was thrown by
PublicKeyTokenMapper::getTokenById
, which was called byTwoFactorAuth\Manager::clearTwoFactorPending
, so at that point the password was already changed.