mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-25 22:20:43 +00:00
gateway-auth-core gains plan/ and credential/, which plan and decide key and JWT authentication on top of its route rules. The Python proxy now runs that core through python-bridge with a PythonAuthHost, which replaces handle_jwt.py and the key and JWT paths of user_api_key_auth.py. The core defines Token, MasterKey and KeyHash, and config gains the JWT auth settings. Route groups named in allowed_routes match as starlette's compile_path compiles them, like _allowed_routes_check, and a team's allowed and denied pass-through routes read as RouteLists. allow_user_auth, allow_requests_on_db_unavailable and enforce_rbac read through python-compat's IsTrue, StrBool and Truthy adapters, and StrBool reads text with flags::str_to_bool. Fixes that landed on main in the code this moves carry over: every user read the core makes from a JWT hands the raw email_verified claim to the host, which reads it with read_email_verified, the impersonation header applies after a master key, cached admin, virtual key or mapped JWT key is accepted, and the team member budget step uses the native budget decision. A JWT email claim that is not a string reads as no email, the same as a missing claim, instead of being stored as the number's text. The native extension size cap rises from 55 MB to 65 MB, since the auth core brings the Linux build to 56.85 MB.
66 lines
2.5 KiB
Python
66 lines
2.5 KiB
Python
from collections.abc import Callable
|
|
from datetime import datetime, timezone
|
|
from typing import Final
|
|
|
|
import pytest
|
|
from fastapi import Request
|
|
|
|
from litellm.caching.dual_cache import DualCache
|
|
from litellm.llms.custom_httpx.httpx_handler import HTTPHandler
|
|
from litellm.proxy._types import LiteLLM_JWTAuth
|
|
from litellm.proxy.auth.host.python import HttpCall, NoRegisteredAgents, PythonAuthHost, SharedFetches
|
|
from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache
|
|
from litellm.proxy.hooks.model_max_budget_limiter import PROXY_VirtualKeyModelMaxBudgetLimiter
|
|
from litellm.proxy.utils import ProxyLogging
|
|
from litellm.rust_bridge._native import RustIdentity
|
|
|
|
|
|
@pytest.fixture
|
|
def auth_host() -> Callable[..., PythonAuthHost]:
|
|
def build(api_key: str | None = None) -> PythonAuthHost:
|
|
cache: Final = UserApiKeyCache()
|
|
request: Final = Request({"type": "http", "method": "POST", "path": "/chat/completions", "headers": []})
|
|
return PythonAuthHost(
|
|
route="/chat/completions",
|
|
parent_otel_span=None,
|
|
http=HttpCall(request=request, request_data={}, start_time=datetime(2024, 1, 1, tzinfo=timezone.utc)),
|
|
prisma_client=None,
|
|
user_api_key_cache=cache,
|
|
proxy_logging_obj=ProxyLogging(user_api_key_cache=cache),
|
|
llm_router=None,
|
|
llm_model_list=None,
|
|
model_max_budget_limiter=PROXY_VirtualKeyModelMaxBudgetLimiter(dual_cache=DualCache()),
|
|
litellm_proxy_admin_name="default_user_id",
|
|
litellm_jwtauth=LiteLLM_JWTAuth(),
|
|
agent_lookup=NoRegisteredAgents(),
|
|
http_handler=HTTPHandler(),
|
|
shared_fetches=SharedFetches(),
|
|
api_key=api_key,
|
|
)
|
|
|
|
return build
|
|
|
|
|
|
@pytest.fixture
|
|
def jwt_identity() -> Callable[..., RustIdentity.Jwt]:
|
|
def build(**overrides: object) -> RustIdentity.Jwt:
|
|
fields: Final[dict[str, object]] = {
|
|
"claims": {"sub": "u"},
|
|
"is_proxy_admin": False,
|
|
"user_id": "u",
|
|
"user_email": "u@example.com",
|
|
"team_id": None,
|
|
"org_id": None,
|
|
"end_user_id": None,
|
|
"agent_id": None,
|
|
"managed_agent": None,
|
|
"user": None,
|
|
"team": None,
|
|
"membership": None,
|
|
"org": None,
|
|
"end_user": None,
|
|
**overrides,
|
|
}
|
|
return RustIdentity.Jwt(**fields) # pyright: ignore[reportArgumentType] # test double fields
|
|
|
|
return build
|