fix(sse): Fix critical SSE auth and URL issues
1. Fix SSE URL mismatch (CRITICAL):
- Frontend was connecting to /events instead of /events/stream
- Updated useProjectEvents.ts to use correct endpoint path
2. Fix SSE token authentication (CRITICAL):
- EventSource API doesn't support custom headers
- Added get_current_user_sse dependency that accepts tokens from:
- Authorization header (preferred, for non-EventSource clients)
- Query parameter 'token' (fallback for browser EventSource)
- Updated SSE endpoint to use new auth dependency
- Both auth methods now work correctly
Files changed:
- backend/app/api/dependencies/auth.py: +80 lines (new SSE auth)
- backend/app/api/routes/events.py: +23 lines (query param support)
- frontend/src/lib/hooks/useProjectEvents.ts: +5 lines (URL fix)
All 20 backend SSE tests pass.
All 17 frontend useProjectEvents tests pass.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -151,3 +151,83 @@ async def get_optional_current_user(
|
||||
return user
|
||||
except (TokenExpiredError, TokenInvalidError):
|
||||
return None
|
||||
|
||||
|
||||
async def get_current_user_sse(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
authorization: str | None = Header(None),
|
||||
token: str | None = None, # Query parameter - passed directly from route
|
||||
) -> User:
|
||||
"""
|
||||
Get the current authenticated user for SSE endpoints.
|
||||
|
||||
SSE (Server-Sent Events) via EventSource API doesn't support custom headers,
|
||||
so this dependency accepts tokens from either:
|
||||
1. Authorization header (preferred, for non-EventSource clients)
|
||||
2. Query parameter 'token' (fallback for EventSource compatibility)
|
||||
|
||||
Security note: Query parameter tokens appear in server logs and browser history.
|
||||
Consider implementing short-lived SSE-specific tokens for production if this
|
||||
is a concern. The current approach is acceptable for internal/trusted networks.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
authorization: Authorization header (Bearer token)
|
||||
token: Query parameter token (fallback for EventSource)
|
||||
|
||||
Returns:
|
||||
User: The authenticated user
|
||||
|
||||
Raises:
|
||||
HTTPException: If authentication fails
|
||||
"""
|
||||
# Try Authorization header first (preferred)
|
||||
auth_token = None
|
||||
if authorization:
|
||||
scheme, param = get_authorization_scheme_param(authorization)
|
||||
if scheme.lower() == "bearer" and param:
|
||||
auth_token = param
|
||||
|
||||
# Fall back to query parameter if no header token
|
||||
if not auth_token and token:
|
||||
auth_token = token
|
||||
|
||||
if not auth_token:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Not authenticated",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
try:
|
||||
# Decode token and get user ID
|
||||
token_data = get_token_data(auth_token)
|
||||
|
||||
# Get user from database
|
||||
result = await db.execute(select(User).where(User.id == token_data.user_id))
|
||||
user = result.scalar_one_or_none()
|
||||
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
||||
)
|
||||
|
||||
if not user.is_active:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Inactive user"
|
||||
)
|
||||
|
||||
return user
|
||||
|
||||
except TokenExpiredError:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Token expired",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
except TokenInvalidError:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Could not validate credentials",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user