from typing import Any from .auth_totp_hotp_code import check_totp_settings, hotp_from_key, is_totp_whole, totp_secret_key from .auth_totp_types import TotpSettings def check_totp_now(now: Any) -> None: if not is_totp_whole(now): raise ValueError("now must be a whole number of seconds from 0 to 2^53 - 1") def totp_code(secret: str, now: int, settings: TotpSettings) -> str: """The RFC 6238 code for the time step now falls in: HOTP at floor(now / period).""" check_totp_now(now) check_totp_settings(settings) key = totp_secret_key(secret) return hotp_from_key(key, now // settings.period, settings.digits, settings.algorithm)