TOTP: troubleshoot time-based codes that fail
Learn how TOTP uses a shared secret and time step, then check clock drift, token setup, replay protection, and safe recovery.
TOTP (Time-based One-Time Password) makes a short code from a shared secret and a time step. RFC 6238 uses a 30-second step by default; a deployment can choose another value. The authenticator and verifier must agree on the secret, start time, hash algorithm, digits, and step. HMAC is a keyed message-authentication calculation; the same inputs give both sides the same code. A QR code often carries the enrollment secret, so treat it like a credential and remove exposed copies after setup.
If a code fails, first enable automatic date and time on the phone and check that the correct account/token was enrolled. Enter a newly displayed code; a slow login can cross a time-step boundary. After a device change, use the serviceʼs verified recovery flow to bind a new secret and invalidate the old enrollment. If only one account fails, check that accountʼs enrollment rather than scanning an old QR code again. The service should synchronize its own clock and explain a safe re-enrollment path without exposing whether an account exists.
The verifier may accept a small neighboring time window to allow clock and typing delay, but a wider window extends code validity and guessing opportunity. Keep it as narrow as the real use case permits, accept each successful code only once, rate-limit guesses per account, protect secrets, and collect codes over an authenticated protected channel. TOTP is not phishing-resistant: a fake login page can relay a code in real time. Consider WebAuthn when verifier-name binding is required. A code shown by an app alone does not automatically make a login multi-factor; factor assurance depends on how the authenticator is activated.
References
[IETF RFC 6238: TOTP](https://www.rfc-editor.org/rfc/rfc6238.html)
[NIST SP 800-63B-4: Authenticator requirements](https://pages.nist.gov/800-63-4/sp800-63b/authenticators/)
[NIST SP 800-63B-4 §§3.2.5, 3.2.7](https://pages.nist.gov/800-63-4/sp800-63b.html)