Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
"""ORM models for the Phase 4B mobile responder PWA.
This file owns four conceptually small but operationally important
tables that back the PWA experience:
* ``passkey_credentials`` — stored WebAuthn credentials per user.
* ``passkey_challenges`` — short-lived register/auth challenges.
* ``oncall_status`` — per-user availability snapshot for one-tap
paging and rotation routing.
* ``agent_approvals`` — pending agent actions a human must approve
from the PWA before the agent can continue.
These are intentionally separate from the existing models so the
responder surface can evolve without impacting the rest of the API.
"""
from __future__ import annotations
import uuid
from datetime import UTC, datetime
from sqlalchemy import (
Boolean,
DateTime,
ForeignKey,
Index,
String,
Text,
)
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column
from app.db.database import Base
class PasskeyCredential(Base):
"""A single WebAuthn / FIDO2 credential bound to a user."""
__tablename__ = "passkey_credentials"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
tenant_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True)
user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
credential_id: Mapped[str] = mapped_column(Text, nullable=False, unique=True)
public_key: Mapped[str] = mapped_column(Text, nullable=False)
sign_count: Mapped[int] = mapped_column(default=0)
transports: Mapped[list] = mapped_column(JSONB, default=list)
device_name: Mapped[str] = mapped_column(String(120), nullable=False)
aaguid: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
is_discoverable: Mapped[bool] = mapped_column(Boolean, default=True)
last_used_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
revoked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
__table_args__ = (
Index("ix_passkeys_user_active", "user_id", "revoked_at"),
Index("ix_passkeys_tenant", "tenant_id"),
)
class PasskeyChallenge(Base):
"""Short-lived register or authenticate challenge."""
__tablename__ = "passkey_challenges"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
purpose: Mapped[str] = mapped_column(String(20), nullable=False)
challenge: Mapped[str] = mapped_column(Text, nullable=False, unique=True)
user_id: Mapped[uuid.UUID | None] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), nullable=True)
tenant_id: Mapped[uuid.UUID | None] = mapped_column(ForeignKey("tenants.id", ondelete="CASCADE"), nullable=True)
email_hint: Mapped[str | None] = mapped_column(String(255), nullable=True)
rp_id: Mapped[str] = mapped_column(String(255), nullable=False)
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
consumed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
__table_args__ = (
Index("ix_passkey_challenges_expiry", "expires_at"),
Index("ix_passkey_challenges_user", "user_id"),
)
class OnCallStatus(Base):
"""Mutable per-user on-call snapshot."""
__tablename__ = "oncall_status"
user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), primary_key=True)
tenant_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True)
status: Mapped[str] = mapped_column(String(20), default="offline")
schedule_ref: Mapped[str | None] = mapped_column(String(200), nullable=True)
rotation: Mapped[str | None] = mapped_column(String(80), nullable=True)
note: Mapped[str | None] = mapped_column(Text, nullable=True)
until: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(UTC),
onupdate=lambda: datetime.now(UTC),
)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
__table_args__ = (Index("ix_oncall_tenant_status", "tenant_id", "status"),)
class AgentApproval(Base):
"""A pending agent action awaiting human approval from the PWA."""
__tablename__ = "agent_approvals"
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
tenant_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True)
run_id: Mapped[uuid.UUID | None] = mapped_column(
UUID(as_uuid=True),
ForeignKey("investigation_runs.id", ondelete="SET NULL"),
nullable=True,
)
case_id: Mapped[str | None] = mapped_column(String(200), nullable=True)
alert_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), nullable=True)
requested_by: Mapped[str] = mapped_column(String(120), default="agent")
required_user_id: Mapped[uuid.UUID | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
required_topic: Mapped[str | None] = mapped_column(String(80), nullable=True)
title: Mapped[str] = mapped_column(String(200), nullable=False)
summary: Mapped[str] = mapped_column(Text, nullable=False)
risk_level: Mapped[str] = mapped_column(String(20), default="medium")
action: Mapped[dict] = mapped_column(JSONB, nullable=False, default=dict)
status: Mapped[str] = mapped_column(String(20), default="pending", index=True)
decided_by_id: Mapped[uuid.UUID | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
decided_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
decision_comment: Mapped[str | None] = mapped_column(Text, nullable=True)
expires_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC), index=True)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(UTC),
onupdate=lambda: datetime.now(UTC),
)
__table_args__ = (
Index("ix_agent_approvals_tenant_status", "tenant_id", "status"),
Index("ix_agent_approvals_run", "run_id"),
)