backend.utils.token_cleanup

Token cleanup utility.

Removes expired and revoked tokens from the database to keep the token tables lean. Intended to be called once at application startup and optionally on a recurring schedule.

Cleaned up tables:
  • models.UsedPasswordResetToken – entries older than 48 h

  • models.TokenBlacklist – entries whose expires_at is in the past

  • models.RefreshToken – revoked entries older than 48 h and all entries whose expires_at is in the past

backend.utils.token_cleanup.cleanup_expired_tokens(db)[Quellcode]

Delete expired and revoked tokens from the database.

Parameter:

db (Session) – Active SQLAlchemy database session.

Rückgabe:

A summary dictionary with the number of removed rows per category:

{
    "password_reset": int,
    "blacklist": int,
    "revoked_refresh": int,
    "expired_refresh": int,
}

Rückgabetyp:

dict

Verursacht:

Exception – Re-raises any database exception after rolling back the transaction.