Files
romm/backend/utils/auth.py
Georges-Antoine Assi 0f75e6aaf4 fix(client-tokens): let a client token revoke itself without me.write (#5419)
* fix(client-tokens): let a client token revoke itself without me.write

A device that signs out or re-pairs had no way to revoke its own token
unless it held me.write, which also lets it mint tokens and approve
pairings. DELETE /api/client-tokens/{id} now skips the me.write check
when the id is the token authenticating the request.

Fixes #5274

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(client-tokens): code-review pass on self-revoke

Annotate raise_auth_error as NoReturn, trim the delete_token docstring, and
cover a me.write token revoking a sibling, the revoked token failing to
authenticate, and socket closure on self-revoke.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(client-tokens): revoke the calling token through DELETE /current

Self-revoke moves to an id-less route so a device never needs to know its
token id, and DELETE /{token_id} keeps me.write in its decorator and in
OpenAPI. The device-authorization token response now also returns token_id.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(client-tokens): simplify pass, reuse raise_auth_error for /current

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* revert(client-tokens): drop DELETE /current and the device-flow token_id

Self-revoke stays on DELETE /{token_id} with the in-handler check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(device-auth): return the issued token's id from the device token poll

A device paired through the device-authorization flow can now revoke its
own token with DELETE /api/client-tokens/{id}, as tokens from /exchange can.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Update docs/BACKEND_ARCHITECTURE.md

* docs: format client tokens table

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 19:05:52 -04:00

125 lines
4.2 KiB
Python

import uuid
from datetime import datetime, timezone
from http.cookies import SimpleCookie
from typing import Any, cast
from fastapi import Request
from starlette.requests import HTTPConnection
from ua_parser import Result as UAResult
from ua_parser import parse as parse_ua
from handler.auth.constants import SESSION_COOKIE_NAME
from handler.database import db_device_handler
from handler.redis_handler import async_cache
from logger.formatter import CYAN
from logger.formatter import highlight as hl
from logger.logger import log
from models.device import KNOWN_DEVICES, Device
from models.user import User
from utils import json_module
def token_device_id(conn: HTTPConnection) -> str | None:
"""The device the request's client token is bound to, if any."""
return getattr(conn.state, "device_id", None)
def request_client_token_id(conn: HTTPConnection) -> int | None:
"""The id of the client token that authenticated the request, if any."""
return getattr(conn.state, "client_token_id", None)
def current_device_id(conn: HTTPConnection) -> str | None:
"""The device a request comes from: its client token's, else its web session's."""
return token_device_id(conn) or (conn.scope.get("session") or {}).get("device_id")
async def get_session_from_environ(environ: dict[str, Any]) -> dict[str, Any]:
"""Resolve the auth session for a socket handshake.
Tries the session the middleware attached to the ASGI scope first; if it's
absent (scope not propagated to the mounted socket app), falls back to
parsing the session cookie and reading the session straight from Redis —
the same lookup RedisSessionMiddleware performs.
"""
scope = environ.get("asgi.scope", {})
session = scope.get("session")
if session:
return cast(dict[str, Any], session)
raw_cookie = environ.get("HTTP_COOKIE", "")
if not raw_cookie:
return {}
cookie: SimpleCookie = SimpleCookie()
cookie.load(raw_cookie)
morsel = cookie.get(SESSION_COOKIE_NAME)
if morsel is None:
return {}
session_data = await async_cache.get(f"session:{morsel.value}")
if not session_data:
return {}
try:
session = json_module.loads(session_data)
except Exception: # noqa: BLE001 - malformed session is "no session"
return {}
# The middleware adds the same key on the scope path.
return {**session, "session_id": morsel.value}
def _get_device_name(user_agent: UAResult) -> str | None:
"""Extract stable browser + OS family from a User-Agent string.
Returns something like "Chrome on Mac OS X" or "Firefox on Windows"
which won't change across browser version updates.
"""
browser = user_agent.user_agent.family if user_agent.user_agent else None
os = user_agent.os.family if user_agent.os else None
if browser and os:
return f"{browser} on {os}"
if browser:
return browser
return os or "Web Browser"
def create_or_find_web_device(request: Request, user: User) -> Device:
"""Find or create a web browser device for the given user.
Uses parsed browser/OS family + client IP as fingerprint to avoid
creating duplicate devices for the same browser.
"""
device_type = KNOWN_DEVICES["web"]
user_agent = parse_ua(request.headers.get("user-agent", ""))
client_version = user_agent.user_agent.major if user_agent.user_agent else None
ip_address = request.client.host if request.client else None
existing = db_device_handler.get_device_by_fingerprint(
user_id=user.id,
ip_address=ip_address,
platform=device_type.platform,
)
if existing:
db_device_handler.update_last_seen(device_id=existing.id, user_id=user.id)
return existing
device = Device(
id=str(uuid.uuid4()),
user_id=user.id,
name=_get_device_name(user_agent),
platform=device_type.platform,
client=device_type.client,
client_version=client_version,
sync_mode=device_type.sync_mode,
ip_address=ip_address,
last_seen=datetime.now(timezone.utc),
)
device = db_device_handler.add_device(device)
log.info(
f"Auto-created web device {device.id} for user {hl(user.username, color=CYAN)}"
)
return device