You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MFA TOTP Authenticator - "Failed to validate the code" #9783
When registering for MFA using TOTP code, it gives error "Failed to validate the code".
To Reproduce
Steps to reproduce the behavior:
sudo apt install pgadmin4-web
Create config_local.py to force MFA via TOTP.
########################################################################
# Two-factor Authentication Configuration
########################################################################
# Set it to True, to enable the two-factor authentication
MFA_ENABLED = True
# Set it to True, to ask the users to register forcefully for the
# two-authentication methods on logged-in.
MFA_FORCE_REGISTRATION = True
# pgAdmin supports Two-factor authentication by either sending an one-time code
# to an email, or using the TOTP based application like Google Authenticator.
MFA_SUPPORTED_METHODS = ["authenticator"]
########################################################################
Login via pgadmin4 web & scan TOTP QR code in Google authenticator.
Accessing pgAdmin over plain HTTP shouldn't be the cause here, as the session cookie isn't marked secure by default.
The most likely culprit is clock drift. During registration pgAdmin only accepts the code for the current 30-second step, with no allowance either side, so if the server's clock is out by more than a few seconds from your phone's, the code will never validate. Could you check that the server is synchronised (timedatectl should show "System clock synchronized: yes"), and that the phone is set to use automatic network time? If that turns out to be it, we could reasonably accept one step either side to make this more forgiving.
Thanks for confirming the clocks. Two things from my side:
Regardless of the cause, I'm going to make the code check accept one 30-second step either side of the current one, during both registration and login, which is common practice for TOTP and removes small clock differences from the picture.
During registration, the secret behind the QR code is kept in your pgAdmin session until you enter the code. If that session data is lost between the two requests, pgAdmin generates a fresh secret, so the code from the entry you scanned can never match. Could you check whether the QR code changes after you get "Failed to validate the code"? If it does, the session cookie probably isn't making it back to pgAdmin (a reverse proxy rewriting the /pgadmin4 path, or the browser rejecting the cookie, are the usual suspects), and it would help to know how pgAdmin is published in front of Apache. If it stays the same, please delete the existing entry from Google Authenticator, scan the code again and retry, and let us know which pgAdmin version you're on now.
Activity
dpage commentedon Oct 16, 2026
Accessing pgAdmin over plain HTTP shouldn't be the cause here, as the session cookie isn't marked secure by default.
The most likely culprit is clock drift. During registration pgAdmin only accepts the code for the current 30-second step, with no allowance either side, so if the server's clock is out by more than a few seconds from your phone's, the code will never validate. Could you check that the server is synchronised (
timedatectlshould show "System clock synchronized: yes"), and that the phone is set to use automatic network time? If that turns out to be it, we could reasonably accept one step either side to make this more forgiving.R-P-H commentedon Oct 21, 2026
My first assumption was the clocks being out of sync, so I remember confirming everything was synchronized, both server and phone, to network time.
dpage commentedon Nov 30, 2026
Thanks for confirming the clocks. Two things from my side:
Regardless of the cause, I'm going to make the code check accept one 30-second step either side of the current one, during both registration and login, which is common practice for TOTP and removes small clock differences from the picture.
During registration, the secret behind the QR code is kept in your pgAdmin session until you enter the code. If that session data is lost between the two requests, pgAdmin generates a fresh secret, so the code from the entry you scanned can never match. Could you check whether the QR code changes after you get "Failed to validate the code"? If it does, the session cookie probably isn't making it back to pgAdmin (a reverse proxy rewriting the
/pgadmin4path, or the browser rejecting the cookie, are the usual suspects), and it would help to know how pgAdmin is published in front of Apache. If it stays the same, please delete the existing entry from Google Authenticator, scan the code again and retry, and let us know which pgAdmin version you're on now.Make the TOTP window test work with pyotp 2.12. pgadmin-org#9783
Accept TOTP codes from one time step either side of the current one. p…
Make the TOTP window test work with pyotp 2.12. pgadmin-org#9783
Accept TOTP codes from one time step either side of the current one. #…
dpage commentedon Nov 30, 2026
Fixed in 43d359a (#10899).