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(proxy): surface OAuth error params in SSO callback (#32433)

Yassin Kortamcda99a08c8ed
diff --git a/litellm/proxy/management_endpoints/ui_sso.py b/litellm/proxy/management_endpoints/ui_sso.py
index 43fdd3ed05a..065464aa565 100644
--- a/litellm/proxy/management_endpoints/ui_sso.py
+++ b/litellm/proxy/management_endpoints/ui_sso.py
@@ -1746,6 +1746,18 @@ async def auth_callback(request: Request, state: Optional[str] = None):
"""Verify login"""
verbose_proxy_logger.info(f"Starting SSO callback with state: {state}")
+ oauth_error = request.query_params.get("error")
+ if oauth_error:
+ oauth_error_description = request.query_params.get("error_description")
+ verbose_proxy_logger.warning(
+ f"SSO callback received OAuth error: {oauth_error}, description: {oauth_error_description}"
+ )
+ raise HTTPException(
+ status_code=401,
+ detail=f"OAuth error: {oauth_error}"
+ + (f", error_description: {oauth_error_description}" if oauth_error_description else ""),
+ )
+
# Check if this is a CLI login (state starts with our CLI prefix)
from litellm.constants import LITELLM_CLI_SESSION_TOKEN_PREFIX
from litellm.proxy._types import LiteLLM_JWTAuth
diff --git a/tests/test_litellm/proxy/management_endpoints/test_ui_sso.py b/tests/test_litellm/proxy/management_endpoints/test_ui_sso.py
index 642f20906a0..045e15f8b8b 100644
--- a/tests/test_litellm/proxy/management_endpoints/test_ui_sso.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_ui_sso.py
@@ -2783,6 +2783,7 @@ class TestCLIKeyRegenerationFlow:
# Mock request
mock_request = MagicMock(spec=Request)
+ mock_request.query_params = {"code": "some-auth-code"}
cli_state = f"{LITELLM_CLI_SESSION_TOKEN_PREFIX}:cli-new-session-key-456"
@@ -2824,6 +2825,7 @@ class TestCLIKeyRegenerationFlow:
from litellm.proxy.management_endpoints.ui_sso import auth_callback
mock_request = MagicMock(spec=Request)
+ mock_request.query_params = {"code": "some-auth-code"}
cli_state = (
f"{LITELLM_CLI_SESSION_TOKEN_PREFIX}:cli-new-session-key-456:WXYZ-2345"
)
@@ -7254,3 +7256,61 @@ async def test_cli_poll_key_tolerates_missing_user_row():
assert result["status"] == "ready"
assert result["key"] == mock_jwt_token
assert result["user_id"] == "just-created-user"
+
+
+def _make_sso_callback_request(query_params: dict) -> MagicMock:
+ mock_request = MagicMock(spec=Request)
+ mock_request.query_params = query_params
+ return mock_request
+
+
+async def test_auth_callback_surfaces_oauth_error_with_description():
+ """
+ Regression: when the IdP denies access it redirects back with
+ ?error=...&error_description=... and no `code`. The callback must surface
+ that reason as a 401 instead of failing later on the missing `code` param.
+ """
+ from litellm.proxy.management_endpoints.ui_sso import auth_callback
+
+ mock_request = _make_sso_callback_request(
+ {"error": "access_denied", "error_description": "User is not assigned to the client application"}
+ )
+
+ with pytest.raises(HTTPException) as exc_info:
+ await auth_callback(request=mock_request, state=None)
+
+ assert exc_info.value.status_code == 401
+ assert "access_denied" in str(exc_info.value.detail)
+ assert "User is not assigned to the client application" in str(exc_info.value.detail)
+
+
+async def test_auth_callback_surfaces_oauth_error_without_description():
+ """error_description is optional in the OAuth error response; the 401 detail must not render 'None'."""
+ from litellm.proxy.management_endpoints.ui_sso import auth_callback
+
+ mock_request = _make_sso_callback_request({"error": "access_denied"})
+
+ with pytest.raises(HTTPException) as exc_info:
+ await auth_callback(request=mock_request, state=None)
+
+ assert exc_info.value.status_code == 401
+ assert "access_denied" in str(exc_info.value.detail)
+ assert "None" not in str(exc_info.value.detail)
+ assert "error_description" not in str(exc_info.value.detail)
+
+
+async def test_auth_callback_without_oauth_error_proceeds_to_normal_flow():
+ """Without an `error` query param the guard must not fire; the callback proceeds into the normal flow."""
+ from litellm.proxy.management_endpoints.ui_sso import auth_callback
+
+ mock_request = _make_sso_callback_request({"code": "some-auth-code"})
+
+ with patch("litellm.proxy.proxy_server.prisma_client", None):
+ with pytest.raises(HTTPException) as exc_info:
+ await auth_callback(request=mock_request, state=None)
+
+ assert exc_info.value.status_code == 500
+ assert "DB not connected" in str(exc_info.value.detail)