set api_key to be the default namespace if the server returns no attributes

This commit is contained in:
Ashwin Bharambe 2025-03-19 20:36:58 -07:00
parent 2692d73d7d
commit 6762e3f911
2 changed files with 7 additions and 3 deletions

View file

@ -172,8 +172,10 @@ class AuthenticationMiddleware:
if auth_response.access_attributes:
user_attributes = auth_response.access_attributes.model_dump(exclude_none=True)
else:
logger.warning("Authentication response did not contain any attributes")
user_attributes = {}
logger.warning("No access attributes, setting namespace to api_key by default")
user_attributes = {
"namespaces": [api_key],
}
scope["user_attributes"] = user_attributes
logger.debug(f"Authentication successful: {len(user_attributes)} attributes")

View file

@ -201,4 +201,6 @@ async def test_auth_middleware_no_attributes(mock_middleware, mock_scope):
await middleware(mock_scope, mock_receive, mock_send)
assert "user_attributes" in mock_scope
assert mock_scope["user_attributes"] == {}
attributes = mock_scope["user_attributes"]
assert "namespaces" in attributes
assert attributes["namespaces"] == ["test-api-key"]