2026-11-16 10:27:01 -03:00
|
|
|
import logging
|
|
|
|
|
|
2026-03-16 09:47:02 -03:00
|
|
|
import pyotp
|
|
|
|
|
|
2026-11-16 10:27:01 -03:00
|
|
|
logger = logging.getLogger("uvicorn.error")
|
|
|
|
|
|
2026-03-16 09:47:02 -03:00
|
|
|
|
|
|
|
|
def verify_totp(secret: str, token: str) -> bool:
|
|
|
|
|
normalized_secret = secret.strip()
|
|
|
|
|
normalized_token = token.strip().replace(" ", "")
|
|
|
|
|
if not normalized_secret or not normalized_token:
|
|
|
|
|
return False
|
2026-11-16 10:27:01 -03:00
|
|
|
try:
|
|
|
|
|
totp = pyotp.TOTP(normalized_secret)
|
|
|
|
|
except ValueError as error:
|
|
|
|
|
logger.warning("TOTP secret rejected (%s) — generate a new secret with at least 24 Base32 characters.", error)
|
|
|
|
|
return False
|
|
|
|
|
return totp.verify(normalized_token, valid_window=1)
|