from datetime import datetime from typing import Optional from sqlalchemy import Boolean, DateTime, ForeignKey, String from sqlalchemy.orm import Mapped, mapped_column from app.db.base_class import Base, TimestampMixin class RefreshToken(Base, TimestampMixin): __tablename__ = "refresh_tokens" id: Mapped[int] = mapped_column(primary_key=True, index=True) user_id: Mapped[int] = mapped_column( ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True ) # SHA-256 hash of the opaque token (never store the raw token). token_hash: Mapped[str] = mapped_column(String(128), unique=True, index=True, nullable=False) expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False) revoked: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) device_info: Mapped[Optional[str]] = mapped_column(String(255), nullable=True) ip_address: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)