Question
Integrating secrets token generation with hmac.compare_digest for expiring credentials
Kavi Finch
0 reputation · 19 Mar 2023, 22:53 UTC
24.8K views0
Goal
Design an authentication flow that uses secrets.token_urlsafe for issuing short‑lived API tokens and hmac.compare_digest for constant‑time verification, while enforcing automatic expiration without external libraries.
Constraints
- Only the Python standard library may be used; third‑party packages such as
bcryptorargon2-cffiare excluded. - The solution must run on Python 3.8+ where
secretsis guaranteed. - Token storage must remain minimal, avoiding a full database if possible.
Open questions
- What pattern allows the verifier to reject expired tokens using only
hmac.compare_digestwithout storing issuance timestamps? - Can a deterministic encoding embed expiry information into the token itself while preserving the entropy guarantees of
secrets? - How should token rotation be coordinated across multiple service instances without a shared secret store?