docs: record auth hash alignment and validation results

This commit is contained in:
Gabriel20xx committed 2026-10-20 15:26:58 +00:00
1 parent 22887fef03
commit f7819904b9
51 files changed
+1792 -542

No files matched your search

@@ -36,19 +36,24 @@ def totp_provisioning_uri(issuer: str, account: str, secret: str) -> str:
def verify_totp(secret: str, token: str, now: float | None = None) -> bool:
return verify_totp_step(secret, token, now) is not None
def verify_totp_step(secret: str, token: str, now: float | None = None) -> int | None:
normalized_secret = _normalize_secret(secret)
normalized_token = _normalize_token(token)
if not normalized_secret or len(normalized_token) != TOTP_DIGITS:
return False
return None
try:
key = base64.b32decode(normalized_secret + "=" * ((8 - len(normalized_secret) % 8) % 8))
except (ValueError, binascii.Error):
return False
return None
if not key:
return False
return None
period = int((time.time() if now is None else now) // TOTP_PERIOD_SECONDS)
for offset in range(-TOTP_WINDOW, TOTP_WINDOW + 1):
# Prefer the current step if a six-digit collision occurs in the window.
for offset in (0, -1, 1):
counter = period + offset
if counter < 0:
continue
@@ -62,8 +67,8 @@ def verify_totp(secret: str, token: str, now: float | None = None) -> bool:
)
candidate = f"{value % 1_000_000:06d}"
if hmac.compare_digest(candidate, normalized_token):
return True
return False
return counter
return None
def _normalize_secret(value: str) -> str: