DEV Community

William Rodriguez
William Rodriguez

Posted on

Self-destructing secrets: TTL expiration and automated pruning.

Self-destructing secrets: TTL expiration and automated pruning.

Temporary authentication tokens shouldn't live forever in your database. wauth includes built-in Time-To-Live (TTL) support that automatically purges expired secrets upon access without background cron jobs.

Here is the exact production implementation for Time-To-Live (TTL) Automatic Expiration:

import time
from wauth import WAuth

auth = WAuth()

# Store temporary session token valid for 5 seconds
auth.set("TEMP_OTP", "948201", ttl=5)
print("Immediately after set:", auth.get("TEMP_OTP"))  # '948201'

# Wait for token expiration
time.sleep(6)

# Expired token automatically purges and returns None
print("After TTL expiry:", auth.get("TEMP_OTP"))  # None
Enter fullscreen mode Exit fullscreen mode

Why this changes developer velocity:

  • Native TTL Support: Set ttl=3600 directly in auth.set() to expire secrets after N seconds.
  • Lazy Deletion: Expired records are automatically purged and return None upon retrieval.
  • Zero Stale Credentials: Guarantees temporary credentials cannot be accessed past expiration.

Zero Pain:

  • Temporary tokens lingering in database tables indefinitely after their validity period expires
  • Writing custom cron jobs or background threads to prune expired session records
  • Accidental authorization grants caused by stale credentials remaining readable

Explore the verified open-source repository on GitHub or install it via:

pip install wauth
Enter fullscreen mode Exit fullscreen mode

Top comments (0)