Skip to content

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]

fix(user_api_key_auth.py): handle user custom auth set with no custom settings

Krrish Dholakia046c6c7149bd
diff --git a/enterprise/litellm_enterprise/proxy/auth/user_api_key_auth.py b/enterprise/litellm_enterprise/proxy/auth/user_api_key_auth.py
index 37bab50971e..35b4c2a1f3b 100644
--- a/enterprise/litellm_enterprise/proxy/auth/user_api_key_auth.py
+++ b/enterprise/litellm_enterprise/proxy/auth/user_api_key_auth.py
@@ -7,13 +7,16 @@ from litellm.proxy._types import UserAPIKeyAuth
async def enterprise_custom_auth(
- request: Request, api_key: str, user_custom_auth: Any
+ request: Request, api_key: str, user_custom_auth: Optional[Any]
) -> Optional[UserAPIKeyAuth]:
from litellm_enterprise.proxy.proxy_server import custom_auth_settings
- if custom_auth_settings is None:
+ if user_custom_auth is None:
return None
+ if custom_auth_settings is None:
+ return await user_custom_auth(request, api_key)
+
if custom_auth_settings["mode"] == "on":
return await user_custom_auth(request, api_key)
elif custom_auth_settings["mode"] == "off":
diff --git a/tests/enterprise/litellm_enterprise/proxy/auth/test_user_api_key_auth.py b/tests/enterprise/litellm_enterprise/proxy/auth/test_user_api_key_auth.py
new file mode 100644
index 00000000000..ed91b30030e
--- /dev/null
+++ b/tests/enterprise/litellm_enterprise/proxy/auth/test_user_api_key_auth.py
@@ -0,0 +1,41 @@
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+from fastapi import Request
+from litellm_enterprise.proxy.auth.user_api_key_auth import enterprise_custom_auth
+
+
+async def test_enterprise_custom_auth_none_user_auth():
+ # Test when user_custom_auth is None
+ request = MagicMock(spec=Request)
+ result = await enterprise_custom_auth(request, "test-api-key", None)
+ assert result is None
+
+
+async def test_enterprise_custom_auth_mode_on():
+ # Test when mode is "on"
+ mock_user_auth = AsyncMock(return_value={"user_id": "test-user"})
+ request = MagicMock(spec=Request)
+
+ with patch(
+ "litellm_enterprise.proxy.proxy_server.custom_auth_settings", {"mode": "on"}
+ ):
+ result = await enterprise_custom_auth(request, "test-api-key", mock_user_auth)
+ assert result == {"user_id": "test-user"}
+ mock_user_auth.assert_called_once_with(request, "test-api-key")
+
+
+async def test_enterprise_custom_auth_mode_auto_with_error():
+ # Test when mode is "auto" and user_auth raises an exception
+ mock_user_auth = AsyncMock(side_effect=Exception("Auth failed"))
+ request = MagicMock(spec=Request)
+
+ with patch(
+ "litellm_enterprise.proxy.proxy_server.custom_auth_settings", {"mode": "auto"}
+ ):
+ result = await enterprise_custom_auth(request, "test-api-key", mock_user_auth)
+ assert result is None
+ mock_user_auth.assert_called_once_with(request, "test-api-key")