Skip to content

MFA TOTP Authenticator - "Failed to validate the code" #9783

Done

Description

@R-P-H

When registering for MFA using TOTP code, it gives error "Failed to validate the code".

To Reproduce

Steps to reproduce the behavior:

  1. sudo apt install pgadmin4-web
  2. 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"]
########################################################################
  1. Login via pgadmin4 web & scan TOTP QR code in Google authenticator.
  2. Enter TOTP code from authenticator.
  3. Get error message "Failed to validate the code".
  • OS: Ubuntu 22.04.5 LTS, Linux-6.8.0-106-generic-x86_64-with-glibc2.35
  • pgAdmin version: 9.13
  • Mode: Server
  • Browser (if running in server mode): Chrome 146.0.0.0
  • Package type: DEB
  • Python version: 3.10.12

I am accessing /pgadmin4 web via an insecure IP address.

Activity

  1. dpage commented on Oct 16, 2026

    @dpage
    Contributor

    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.

  2. added and removed on Oct 16, 2026
  3. R-P-H commented on Oct 21, 2026

    @R-P-H
    Author

    My first assumption was the clocks being out of sync, so I remember confirming everything was synchronized, both server and phone, to network time.

  4. dpage commented on Nov 30, 2026

    @dpage
    Contributor

    Thanks for confirming the clocks. Two things from my side:

    1. 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.

    2. 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.

  5. self-assigned this
    on Nov 30, 2026
  6. dpage commented on Nov 30, 2026

    @dpage
    Contributor

    Fixed in 43d359a (#10899).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions