mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
Implements RFC 8628-style device authorization so clients (argosy-launcher, grout) can pair by display instead of manually copying tokens. Device posts to an open /api/auth/device/init with its identifier and requested scopes; the server returns device_code + user_code + QR URL. User scans QR, lands at /pair/device, approves (optionally editing name/scopes/expiry); the device's next poll on /api/auth/device/token returns a ClientToken bound 1:1 to a newly- created (or deduped) Device record. Downstream endpoints (/play-sessions, /sync/negotiate) infer device_id from the bound token so the client doesn't have to ship it on every call. - Migrations 0080/0081: devices.client_device_identifier (unique per user) and client_tokens.device_id FK (ON DELETE SET NULL) - Five new endpoints under /api/auth/device (init/pending/approve/ deny/token) with Redis-backed state, per-IP rate limits, and RFC-compliant error codes (authorization_pending, slow_down, expired_token, access_denied) - HybridAuthBackend surfaces bound device_id on request.state and bumps devices.last_seen with a 5-minute debounce - /api/users/me returns current_device_id for bound tokens so a device can identify itself from its token alone - Frontend approval screen at /pair/device with editable scopes/ name/expiry (defaults to Never), 3s auto-close countdown - ClientApiTokens settings list shows bound-device chip - 20 i18n keys added to all 17 locales; generated models updated - 52 new tests across 13 classes; full suite 1334 passed Planning and review assisted by Claude Code.
34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import TIMESTAMP, ForeignKey, String
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from models.base import BaseModel
|
|
|
|
if TYPE_CHECKING:
|
|
from models.device import Device
|
|
from models.user import User
|
|
|
|
|
|
class ClientToken(BaseModel):
|
|
__tablename__ = "client_tokens"
|
|
__table_args__ = {"extend_existing": True}
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
|
name: Mapped[str] = mapped_column(String(255))
|
|
hashed_token: Mapped[str] = mapped_column(String(64), unique=True, index=True)
|
|
scopes: Mapped[str] = mapped_column(String(1000))
|
|
expires_at: Mapped[datetime | None] = mapped_column(TIMESTAMP(timezone=True))
|
|
last_used_at: Mapped[datetime | None] = mapped_column(TIMESTAMP(timezone=True))
|
|
|
|
device_id: Mapped[str | None] = mapped_column(
|
|
ForeignKey("devices.id", ondelete="SET NULL"), nullable=True
|
|
)
|
|
|
|
user: Mapped[User] = relationship(lazy="joined", back_populates="client_tokens")
|
|
device: Mapped[Device | None] = relationship(lazy="select")
|