Files
qtalker---/backend/app/models/auth_session.py

33 lines
962 B
Python

import hashlib
import secrets
import uuid
from datetime import datetime, timedelta, timezone
from sqlalchemy import DateTime, ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column
from app.db import Base
SESSION_TTL = timedelta(days=14)
class AuthSession(Base):
__tablename__ = "auth_sessions"
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"))
token_hash: Mapped[str] = mapped_column(String(64), unique=True, index=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)
)
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True))
def generate_session_token() -> tuple[str, str]:
raw = secrets.token_urlsafe(32)
return raw, hash_token(raw)
def hash_token(raw: str) -> str:
return hashlib.sha256(raw.encode()).hexdigest()