Server: basic implementation. Not tested => no python installed; bugfix in ESP (uvicorn on port 8090)
This commit is contained in:
1 parent
3694cdf207
commit
8a51e3d95e
38 files changed
+2442
-21
No files matched your search
@@ -0,0 +1,65 @@
|
||||
"""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):
|
||||
__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)
|
||||
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))
|
||||
Reference in new issue
Block a user