"""Users and sessions. Devices are NOT users (SPEC 3.1). A device authenticates with a bearer token scoped to /ingest, /heartbeat, /status, /config, /commands/*/ack, /firmware/* and /stream/*. It can never read the gallery. """ from __future__ import annotations import uuid from datetime import datetime from sqlalchemy import ( Boolean, CheckConstraint, DateTime, ForeignKey, String, Uuid, func, ) from sqlalchemy.orm import Mapped, mapped_column from .base import Base ROLES = ("admin", "editor", "viewer") THEMES = ("light", "dark", "blue") class User(Base): __tablename__ = "users" __table_args__ = ( CheckConstraint("role IN " + str(ROLES), name="role_valid"), CheckConstraint("theme IN " + str(THEMES), name="theme_valid"), ) id: Mapped[int] = mapped_column(primary_key=True) username: Mapped[str] = mapped_column(String(64), unique=True) email: Mapped[str | None] = mapped_column(String(255)) # Argon2id. Populated in Phase 3; nothing writes it yet. password_hash: Mapped[str | None] = mapped_column(String(255)) role: Mapped[str] = mapped_column(String(16), default="viewer", server_default="'viewer'") totp_secret: Mapped[str | None] = mapped_column(String(64)) is_active: Mapped[bool] = mapped_column(Boolean, default=True, server_default="true") theme: Mapped[str] = mapped_column(String(16), default="light", server_default="'light'") locale: Mapped[str] = mapped_column(String(8), default="de", server_default="'de'") created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) last_login_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True)) class Session(Base): """A signed-in browser. ``id`` is internal and never leaves the server. The cookie carries a separate high-entropy token of which only the SHA-256 is stored, exactly as device tokens work (api/deps.py). Using ``id`` as the cookie value would be simpler and would mean a database dump handed over every live session. ``csrf_token`` is per session rather than global, so the double-submit check cannot be satisfied by an attacker who can merely set a cookie on the domain -- they would also need this session's token, which is what they do not have. """ __tablename__ = "sessions" id: Mapped[uuid.UUID] = mapped_column(Uuid(as_uuid=True), primary_key=True, default=uuid.uuid4) user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True) # SHA-256 hex of the cookie value. Unique so a collision is a constraint # error rather than two users sharing a session, and indexed because this # is the lookup on every authenticated request. token_hash: Mapped[str] = mapped_column(String(64), unique=True, index=True) csrf_token: Mapped[str] = mapped_column(String(64)) created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now()) expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), index=True) ip: Mapped[str | None] = mapped_column(String(45)) user_agent: Mapped[str | None] = mapped_column(String(255))