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.
88 lines
2.9 KiB
Python
88 lines
2.9 KiB
Python
from typing import Final
|
|
|
|
from fastapi import HTTPException
|
|
|
|
import litellm
|
|
from litellm.proxy._types import (
|
|
ProxyErrorTypes,
|
|
ProxyException,
|
|
)
|
|
from litellm.proxy.auth import denial as auth_denial
|
|
from litellm.proxy.auth.auth_checks import TeamNotFoundError
|
|
from litellm.proxy.auth.auth_utils import is_invalid_virtual_key_error
|
|
from litellm.proxy.auth.model_access_denied import ModelAccessDeniedHTTPException
|
|
from litellm.rust_bridge._native import RustDenial
|
|
|
|
|
|
def test_proxy_denial_keeps_its_status_type_and_param() -> None:
|
|
error: Final = auth_denial.denial_error(
|
|
RustDenial.Proxy(status=401, message="expired", error_type="expired_key", param="sk-...abcd")
|
|
)
|
|
|
|
assert isinstance(error, ProxyException)
|
|
assert (error.message, error.type, error.param, error.code) == (
|
|
"expired",
|
|
ProxyErrorTypes.expired_key,
|
|
"sk-...abcd",
|
|
"401",
|
|
)
|
|
|
|
|
|
def test_invalid_virtual_key_denial_is_marked_for_log_routing() -> None:
|
|
error: Final = auth_denial.denial_error(
|
|
RustDenial.InvalidVirtualKey(status=401, detail="LiteLLM Virtual Key expected.")
|
|
)
|
|
|
|
assert isinstance(error, HTTPException)
|
|
assert (error.status_code, error.detail) == (401, "LiteLLM Virtual Key expected.")
|
|
assert is_invalid_virtual_key_error(error)
|
|
|
|
|
|
def test_plain_http_denial_is_not_marked_as_an_invalid_key() -> None:
|
|
error: Final = auth_denial.denial_error(
|
|
RustDenial.Http(status=403, detail="'allow_user_auth' not set or set to False")
|
|
)
|
|
|
|
assert isinstance(error, HTTPException)
|
|
assert error.status_code == 403
|
|
assert not is_invalid_virtual_key_error(error)
|
|
|
|
|
|
def test_model_access_denial_carries_the_internal_message() -> None:
|
|
error: Final = auth_denial.denial_error(
|
|
RustDenial.ModelAccess(status=401, detail="no access", internal_message="model=x")
|
|
)
|
|
|
|
assert isinstance(error, ModelAccessDeniedHTTPException)
|
|
assert (error.status_code, error.detail, error.internal_message) == (401, "no access", "model=x")
|
|
|
|
|
|
def test_plain_denial_is_a_bare_exception() -> None:
|
|
error: Final = auth_denial.denial_error(RustDenial.Plain(message="no team"))
|
|
|
|
assert type(error) is Exception
|
|
assert str(error) == "no team"
|
|
|
|
|
|
def test_budget_denial_is_the_budget_error_the_host_step_raised() -> None:
|
|
error: Final = auth_denial.denial_error(
|
|
RustDenial.BudgetExceeded(
|
|
current_cost=2.0, max_budget=1.0, message="over", entity_type="team_member", entity_id="u:t"
|
|
)
|
|
)
|
|
|
|
assert isinstance(error, litellm.BudgetExceededError)
|
|
assert (error.current_cost, error.max_budget, error.message, error.entity_type, error.entity_id) == (
|
|
2.0,
|
|
1.0,
|
|
"over",
|
|
"team_member",
|
|
"u:t",
|
|
)
|
|
|
|
|
|
def test_gone_session_team_is_a_team_not_found() -> None:
|
|
error: Final = auth_denial.denial_error(RustDenial.TeamNotFound(team_id="t"))
|
|
|
|
assert isinstance(error, TeamNotFoundError)
|
|
assert error.status_code == 404
|