The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(auth): return 403 from the OAuth2 enterprise gate (#35838)
Yassin Kortam7ac108593140
diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.pyindex ecfc14a0f9d..9248576b599 100644--- a/litellm/proxy/auth/user_api_key_auth.py+++ b/litellm/proxy/auth/user_api_key_auth.py@@ -1192,9 +1192,12 @@ async def _user_api_key_auth_builder(from litellm.proxy.proxy_server import premium_userif premium_user is not True:- raise ValueError(- "Oauth2 token validation is only available for premium users"- + CommonProxyErrors.not_premium_user.value+ raise ProxyException(+ message="Oauth2 token validation is only available for premium users. "+ + CommonProxyErrors.not_premium_user.value,+ type=ProxyErrorTypes.auth_error,+ param="premium_user",+ code=status.HTTP_403_FORBIDDEN,)return await Oauth2Handler.check_oauth2_token(token=api_key)diff --git a/tests/test_litellm/proxy/auth/test_user_api_key_auth.py b/tests/test_litellm/proxy/auth/test_user_api_key_auth.pyindex 36e9ae5f992..bc5bb877bd0 100644--- a/tests/test_litellm/proxy/auth/test_user_api_key_auth.py+++ b/tests/test_litellm/proxy/auth/test_user_api_key_auth.py@@ -2066,12 +2066,49 @@ class TestJWTOAuth2Coexistence:)assert exc_info.value.type == ProxyErrorTypes.auth_error+ assert exc_info.value.code == "403"assert ("Oauth2 token validation is only available for premium users"in exc_info.value.message)mock_oauth2.assert_not_called()+ @pytest.mark.asyncio+ async def test_oauth2_disabled_unknown_key_stays_unauthorized(self):+ """+ The enterprise gate on the OAuth2 path is the only thing that turns 403+ here. With `enable_oauth2_auth` off, an unknown opaque key is an+ ordinary bad credential and must still be 401, so a blanket 403 is as+ wrong in this direction as the 401 was in the gated one.+ """+ opaque_token = "some-opaque-m2m-oauth2-token"++ mock_request = MagicMock()+ mock_request.url.path = "/v1/chat/completions"+ mock_request.headers = {"authorization": f"Bearer {opaque_token}"}+ mock_request.query_params = {}++ with (+ patch("litellm.proxy.proxy_server.general_settings", {}),+ patch("litellm.proxy.proxy_server.premium_user", False),+ patch("litellm.proxy.proxy_server.master_key", "sk-master"),+ patch("litellm.proxy.proxy_server.prisma_client", MagicMock()),+ patch("litellm.proxy.proxy_server.user_api_key_cache", DualCache()),+ patch(+ "litellm.proxy.auth.user_api_key_auth.Oauth2Handler.check_oauth2_token",+ new_callable=AsyncMock,+ ) as mock_oauth2,+ ):+ with pytest.raises(ProxyException) as exc_info:+ await user_api_key_auth(+ request=mock_request,+ api_key=f"Bearer {opaque_token}",+ )++ assert exc_info.value.code == "401"+ assert "premium" not in exc_info.value.message.lower()+ mock_oauth2.assert_not_called()+@pytest.mark.asyncioasync def test_both_enabled_jwt_token_skips_oauth2(self):"""