Blog | G5 Cyber Security

Wrong TOTP Tokens: Best Practices

TL;DR

Users sometimes enter incorrect Time-based One-Time Passwords (TOTPs). This guide explains how to handle these errors securely and improve the user experience. Focus on rate limiting, clear error messages, account recovery options, and logging for security monitoring.

Handling Incorrect TOTP Tokens

  1. Rate Limiting: Prevent brute-force attacks by limiting the number of incorrect TOTP attempts allowed within a specific timeframe.
from flask import Flask, session, request
import time

app = Flask(__name__)
app.secret_key = 'your_secret_key'

@app.route('/login', methods=['POST'])
def login():
    if 'failed_attempts' not in session:
        session['failed_attempts'] = 0
    session['failed_attempts'] += 1
    if session['failed_attempts'] >= 5:  # Allow 5 attempts
        return "Too many failed attempts. Try again later.", 429
    # ... your TOTP verification logic here...
    return "Login successful!"
  • Consider increasing the lockout duration with each subsequent attempt.
  • Clear Error Messages: Provide informative but not overly revealing error messages to users.
  • Account Recovery Options: Offer alternative recovery methods if a user loses access to their TOTP device.
  • Time Synchronization: Ensure your server’s time is accurately synchronized using NTP (Network Time Protocol). TOTP algorithms are highly sensitive to timing discrepancies.
  • Logging and Monitoring: Log failed TOTP attempts for security monitoring purposes.
  • Authenticator App Compatibility: Test your implementation with various popular authenticator apps (Google Authenticator, Authy, Microsoft Authenticator) to ensure compatibility and correct time drift handling.
  • User Education: Provide clear instructions on how to set up and use TOTP authentication, including information about backup codes and account recovery options.
  • Exit mobile version