auth_session.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from __future__ import annotations
  2. from datetime import datetime
  3. from sqlalchemy import DateTime, ForeignKey, Index, Integer, String, func
  4. from sqlalchemy.orm import Mapped, mapped_column
  5. from supply_infra.db.base import Base
  6. class AuthSession(Base):
  7. """Server-side browser session. Only a SHA-256 token hash is persisted."""
  8. __tablename__ = "auth_session"
  9. __table_args__ = (
  10. Index("idx_auth_session_user", "user_id", "expires_at"),
  11. Index("idx_auth_session_expiry", "expires_at"),
  12. )
  13. id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
  14. token_hash: Mapped[str] = mapped_column(
  15. String(64),
  16. nullable=False,
  17. unique=True,
  18. index=True,
  19. )
  20. user_id: Mapped[int] = mapped_column(
  21. Integer,
  22. ForeignKey("auth_user.id", ondelete="CASCADE"),
  23. nullable=False,
  24. )
  25. expires_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)
  26. ip_address: Mapped[str | None] = mapped_column(String(64), nullable=True)
  27. user_agent: Mapped[str | None] = mapped_column(String(512), nullable=True)
  28. created_at: Mapped[datetime] = mapped_column(
  29. DateTime,
  30. nullable=False,
  31. server_default=func.now(),
  32. )