security_event.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """
  2. Security event model for tracking suspicious activity.
  3. """
  4. from datetime import datetime
  5. from sqlalchemy import DateTime, JSON, String, Text
  6. from sqlalchemy.orm import Mapped, mapped_column
  7. from app.core.database import Base
  8. class SecurityEvent(Base):
  9. """Security events (brute-force, flooding, suspicious activity)."""
  10. __tablename__ = "security_events"
  11. id: Mapped[int] = mapped_column(primary_key=True)
  12. timestamp: Mapped[datetime] = mapped_column(DateTime(timezone=True), index=True)
  13. # Event type
  14. event_type: Mapped[str] = mapped_column(
  15. String(50), index=True, nullable=False
  16. ) # login_bruteforce, device_token_bruteforce, registration_flood, etc.
  17. # Severity
  18. severity: Mapped[str] = mapped_column(
  19. String(20), nullable=False
  20. ) # low, medium, high, critical
  21. # Source
  22. ip_address: Mapped[str | None] = mapped_column(String(45), index=True)
  23. user_agent: Mapped[str | None] = mapped_column(Text)
  24. endpoint: Mapped[str | None] = mapped_column(String(255))
  25. # Details
  26. description: Mapped[str] = mapped_column(Text, nullable=False)
  27. event_metadata: Mapped[dict | None] = mapped_column(JSON) # Additional context
  28. # Resolution
  29. resolved: Mapped[bool] = mapped_column(default=False, index=True)
  30. resolved_at: Mapped[datetime | None] = mapped_column()
  31. resolved_by: Mapped[int | None] = mapped_column() # user_id