mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-06 00:38:23 +00:00
- Port backend/docs/AUTH_TEST_PLAN.md and AUTH_UPGRADE.md from PR #1728 - Rename metadata.user_id → metadata.owner_id in AUTH_TEST_PLAN.md (4 occurrences from the original PR doc) - ruff auto-fix UP037 in sentinel type annotations: drop quotes around "str | None | _AutoSentinel" now that from __future__ import annotations makes them implicit string forms - ruff format: 2 files (app/gateway/app.py, runtime/user_context.py) Note on test coverage additions: - conftest.py autouse fixture was already added in commit 4 (had to be co-located with the repository changes to keep pre-existing persistence tests passing) - cross-user isolation E2E tests (test_owner_isolation.py) deferred — enforcement is already proven by the 98-test repository suite via the autouse fixture + explicit _AUTO sentinel exercises - New test cases (TC-API-17..20, TC-ATK-13, TC-MIG-01..07) listed in AUTH_TEST_PLAN.md are deferred to a follow-up PR — they are manual-QA test cases rather than pytest code, and the spec-level coverage is already met by test_user_context.py + the 98-test repository suite. Final test results: - Auth suite (test_auth*, test_langgraph_auth, test_ensure_admin, test_user_context): 186 passed - Persistence suite (test_run_event_store, test_run_repository, test_thread_meta_repo, test_feedback): 98 passed - Lint: ruff check + ruff format both clean
143 lines
5.1 KiB
Python
143 lines
5.1 KiB
Python
"""Request-scoped user context for owner-based authorization.
|
|
|
|
This module holds a :class:`~contextvars.ContextVar` that the gateway's
|
|
auth middleware sets after a successful authentication. Repository
|
|
methods read the contextvar via a sentinel default parameter, letting
|
|
routers stay free of ``owner_id`` boilerplate.
|
|
|
|
Three-state semantics for the repository ``owner_id`` parameter (the
|
|
consumer side of this module lives in ``deerflow.persistence.*``):
|
|
|
|
- ``_AUTO`` (module-private sentinel, default): read from contextvar;
|
|
raise :class:`RuntimeError` if unset.
|
|
- Explicit ``str``: use the provided value, overriding contextvar.
|
|
- Explicit ``None``: no WHERE clause — used only by migration scripts
|
|
and admin CLIs that intentionally bypass isolation.
|
|
|
|
Dependency direction
|
|
--------------------
|
|
``persistence`` (lower layer) reads from this module; ``gateway.auth``
|
|
(higher layer) writes to it. ``CurrentUser`` is defined here as a
|
|
:class:`typing.Protocol` so that ``persistence`` never needs to import
|
|
the concrete ``User`` class from ``gateway.auth.models``. Any object
|
|
with an ``.id: str`` attribute structurally satisfies the protocol.
|
|
|
|
Asyncio semantics
|
|
-----------------
|
|
``ContextVar`` is task-local under asyncio, not thread-local. Each
|
|
FastAPI request runs in its own task, so the context is naturally
|
|
isolated. ``asyncio.create_task`` and ``asyncio.to_thread`` inherit the
|
|
parent task's context, which is typically the intended behaviour; if
|
|
a background task must *not* see the foreground user, wrap it with
|
|
``contextvars.copy_context()`` to get a clean copy.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from contextvars import ContextVar, Token
|
|
from typing import Final, Protocol, runtime_checkable
|
|
|
|
|
|
@runtime_checkable
|
|
class CurrentUser(Protocol):
|
|
"""Structural type for the current authenticated user.
|
|
|
|
Any object with an ``.id: str`` attribute satisfies this protocol.
|
|
Concrete implementations live in ``app.gateway.auth.models.User``.
|
|
"""
|
|
|
|
id: str
|
|
|
|
|
|
_current_user: Final[ContextVar[CurrentUser | None]] = ContextVar("deerflow_current_user", default=None)
|
|
|
|
|
|
def set_current_user(user: CurrentUser) -> Token[CurrentUser | None]:
|
|
"""Set the current user for this async task.
|
|
|
|
Returns a reset token that should be passed to
|
|
:func:`reset_current_user` in a ``finally`` block to restore the
|
|
previous context.
|
|
"""
|
|
return _current_user.set(user)
|
|
|
|
|
|
def reset_current_user(token: Token[CurrentUser | None]) -> None:
|
|
"""Restore the context to the state captured by ``token``."""
|
|
_current_user.reset(token)
|
|
|
|
|
|
def get_current_user() -> CurrentUser | None:
|
|
"""Return the current user, or ``None`` if unset.
|
|
|
|
Safe to call in any context. Used by code paths that can proceed
|
|
without a user (e.g. migration scripts, public endpoints).
|
|
"""
|
|
return _current_user.get()
|
|
|
|
|
|
def require_current_user() -> CurrentUser:
|
|
"""Return the current user, or raise :class:`RuntimeError`.
|
|
|
|
Used by repository code that must not be called outside a
|
|
request-authenticated context. The error message is phrased so
|
|
that a caller debugging a stack trace can locate the offending
|
|
code path.
|
|
"""
|
|
user = _current_user.get()
|
|
if user is None:
|
|
raise RuntimeError("repository accessed without user context")
|
|
return user
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Sentinel-based owner_id resolution
|
|
# ---------------------------------------------------------------------------
|
|
#
|
|
# Repository methods accept an ``owner_id`` keyword-only argument that
|
|
# defaults to ``AUTO``. The three possible values drive distinct
|
|
# behaviours; see the docstring on :func:`resolve_owner_id`.
|
|
|
|
|
|
class _AutoSentinel:
|
|
"""Singleton marker meaning 'resolve owner_id from contextvar'."""
|
|
|
|
_instance: _AutoSentinel | None = None
|
|
|
|
def __new__(cls) -> _AutoSentinel:
|
|
if cls._instance is None:
|
|
cls._instance = super().__new__(cls)
|
|
return cls._instance
|
|
|
|
def __repr__(self) -> str:
|
|
return "<AUTO>"
|
|
|
|
|
|
AUTO: Final[_AutoSentinel] = _AutoSentinel()
|
|
|
|
|
|
def resolve_owner_id(
|
|
value: str | None | _AutoSentinel,
|
|
*,
|
|
method_name: str = "repository method",
|
|
) -> str | None:
|
|
"""Resolve the owner_id parameter passed to a repository method.
|
|
|
|
Three-state semantics:
|
|
|
|
- :data:`AUTO` (default): read from contextvar; raise
|
|
:class:`RuntimeError` if no user is in context. This is the
|
|
common case for request-scoped calls.
|
|
- Explicit ``str``: use the provided id verbatim, overriding any
|
|
contextvar value. Useful for tests and admin-override flows.
|
|
- Explicit ``None``: no filter — the repository should skip the
|
|
owner_id WHERE clause entirely. Reserved for migration scripts
|
|
and CLI tools that intentionally bypass isolation.
|
|
"""
|
|
if isinstance(value, _AutoSentinel):
|
|
user = _current_user.get()
|
|
if user is None:
|
|
raise RuntimeError(f"{method_name} called with owner_id=AUTO but no user context is set; pass an explicit owner_id, set the contextvar via auth middleware, or opt out with owner_id=None for migration/CLI paths.")
|
|
return user.id
|
|
return value
|