From 55bad8d9ef442311e0a4c21f044c816381031885 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 14 Aug 2026 19:26:11 +0000 Subject: [PATCH] Implement authentication event logging in AuthMiddleware Replaces a TODO with actual authentication event logging in the `dispatch` method of `AuthMiddleware`. This adds logging for successful authentications, token validation failures, and unauthenticated access attempts to protected routes, including the client IP address and route path. Co-authored-by: manupawickramasinghe <73810867+manupawickramasinghe@users.noreply.github.com> --- archive/v1/src/api/middleware/auth.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/archive/v1/src/api/middleware/auth.py b/archive/v1/src/api/middleware/auth.py index 564cdef0cb..298255296a 100644 --- a/archive/v1/src/api/middleware/auth.py +++ b/archive/v1/src/api/middleware/auth.py @@ -57,6 +57,8 @@ async def dispatch(self, request: Request, call_next): # Extract and validate token token = self._extract_token(request) + client_ip = request.client.host if request.client else "unknown" + if token: try: # Verify token and add user info to request state @@ -64,10 +66,10 @@ async def dispatch(self, request: Request, call_next): request.state.user = user_data request.state.authenticated = True - logger.debug(f"Authenticated user: {user_data.get('id')}") + logger.info(f"Authentication successful for user: {user_data.get('id')} from IP: {client_ip} to {request.url.path}") except Exception as e: - logger.warning(f"Token validation failed: {e}") + logger.warning(f"Authentication failed from IP: {client_ip} to {request.url.path} - Token validation error: {e}") # For protected paths, return 401 if self._is_protected_path(request.url.path): @@ -88,6 +90,7 @@ async def dispatch(self, request: Request, call_next): else: # No token provided if self._is_protected_path(request.url.path): + logger.warning(f"Unauthenticated access attempt from IP: {client_ip} to protected path: {request.url.path}") return JSONResponse( status_code=401, content={ @@ -223,9 +226,6 @@ async def _verify_token(self, token: str) -> Dict[str, Any]: raise ValueError(f"JWT validation failed: {e}") except Exception as e: raise ValueError(f"Token verification error: {e}") - - # TODO: Wire up authentication event logging in dispatch() for - # security monitoring (login failures, token expiry, etc.). class TokenBlacklist: