2026-08-06 13:23:58 +02:00
|
|
|
"""Session model — PostgreSQL audit trail for sessions.
|
|
|
|
|
|
|
|
|
|
⚠️ Session-Tabelle dient als audit trail. Redis ist der Runtime-Session-Store.
|
|
|
|
|
Dies ist ein bewusstes Dual-System.
|
2026-08-06 13:43:47 +02:00
|
|
|
|
|
|
|
|
⚠️ Sessions sind nicht an IP/Device gebunden (Design-Entscheidung).
|
|
|
|
|
Bei Bedarf IP-Binding hinzufügen.
|
2026-08-06 13:23:58 +02:00
|
|
|
"""
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
2026-06-29 17:43:56 +02:00
|
|
|
from sqlalchemy import DateTime, ForeignKey, String, func
|
2026-06-29 00:10:10 +02:00
|
|
|
from sqlalchemy.dialects.postgresql import UUID as PGUUID
|
|
|
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
|
|
|
|
|
|
from app.core.db import Base, TenantMixin
|
2026-08-21 11:20:15 +02:00
|
|
|
from app.models.owned_mixin import OwnedMixin
|
2026-06-29 00:10:10 +02:00
|
|
|
|
|
|
|
|
|
2026-08-21 11:20:15 +02:00
|
|
|
class Session(Base, TenantMixin, OwnedMixin):
|
2026-06-29 00:10:10 +02:00
|
|
|
"""Immutable session audit record. Runtime session lookup uses Redis."""
|
|
|
|
|
|
|
|
|
|
__tablename__ = "sessions"
|
|
|
|
|
|
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
|
|
|
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
|
|
|
)
|
|
|
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
|
|
|
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
|
|
|
|
|
)
|
|
|
|
|
csrf_token: Mapped[str] = mapped_column(String(255), nullable=False)
|
2026-07-31 00:58:05 +02:00
|
|
|
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, index=True)
|
2026-06-29 00:10:10 +02:00
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
|
|
|
DateTime(timezone=True), nullable=False, server_default=func.now()
|
|
|
|
|
)
|