-
Notifications
You must be signed in to change notification settings - Fork 630
Expand file tree
/
Copy pathauth_middleware.py
More file actions
235 lines (197 loc) · 10.2 KB
/
auth_middleware.py
File metadata and controls
235 lines (197 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# -*- coding: utf-8 -*-
"""Location: ./mcpgateway/middleware/auth_middleware.py
Copyright 2025
SPDX-License-Identifier: Apache-2.0
Authors: Mihai Criveti
Authentication Middleware for early user context extraction.
This middleware extracts user information from JWT tokens early in the request
lifecycle and stores it in request.state.user for use by other middleware
(like ObservabilityMiddleware) and route handlers.
Examples:
>>> from mcpgateway.middleware.auth_middleware import AuthContextMiddleware # doctest: +SKIP
>>> app.add_middleware(AuthContextMiddleware) # doctest: +SKIP
"""
# Standard
import logging
from typing import Callable
# Third-Party
from fastapi import HTTPException
from fastapi.security import HTTPAuthorizationCredentials
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
# First-Party
from mcpgateway.auth import get_current_user
from mcpgateway.config import settings
from mcpgateway.db import SessionLocal
from mcpgateway.middleware.path_filter import should_skip_auth_context
from mcpgateway.services.security_logger import get_security_logger
logger = logging.getLogger(__name__)
security_logger = get_security_logger()
# HTTPException detail strings that indicate security-critical rejections
# (revoked tokens, disabled accounts, fail-secure validation errors).
# Only these trigger a hard JSON deny in the auth middleware; all other
# 401/403s fall through to route-level auth for backwards compatibility.
_HARD_DENY_DETAILS = frozenset({"Token has been revoked", "Account disabled", "Token validation failed"})
def _should_log_auth_success() -> bool:
"""Check if successful authentication should be logged based on settings.
Returns:
True if security_logging_level is "all", False otherwise.
"""
return settings.security_logging_level == "all"
def _should_log_auth_failure() -> bool:
"""Check if failed authentication should be logged based on settings.
Returns:
True if security_logging_level is "all" or "failures_only", False for "high_severity".
"""
# Log failures for "all" and "failures_only" levels, not for "high_severity"
return settings.security_logging_level in ("all", "failures_only")
class AuthContextMiddleware(BaseHTTPMiddleware):
"""Middleware for extracting user authentication context early in request lifecycle.
This middleware attempts to authenticate requests using JWT tokens from cookies
or Authorization headers, and stores the user information in request.state.user
for downstream middleware and handlers to use.
Unlike route-level authentication dependencies, this runs for ALL requests,
allowing middleware like ObservabilityMiddleware to access user context.
Note:
Authentication failures are silent - requests continue as unauthenticated.
Route-level dependencies should still enforce authentication requirements.
"""
async def dispatch(self, request: Request, call_next: Callable) -> Response:
"""Process request and populate user context if authenticated.
Args:
request: Incoming HTTP request
call_next: Next middleware/handler in chain
Returns:
HTTP response
"""
# Skip for health checks and static files
if should_skip_auth_context(request.url.path):
return await call_next(request)
# Try to extract token from multiple sources
token = None
# 1. Try manual cookie reading
if request.cookies:
token = request.cookies.get("jwt_token") or request.cookies.get("access_token")
# 2. Try Authorization header
if not token:
auth_header = request.headers.get("authorization")
if auth_header and auth_header.startswith("Bearer "):
token = auth_header.replace("Bearer ", "")
# If no token found, continue without user context
if not token:
return await call_next(request)
# Check logging settings once upfront to avoid DB session when not needed
log_success = _should_log_auth_success()
log_failure = _should_log_auth_failure()
# Try to authenticate and populate user context
# Note: get_current_user manages its own DB sessions internally
# We only create a DB session here when security logging is enabled
try:
credentials = HTTPAuthorizationCredentials(scheme="Bearer", credentials=token)
user = await get_current_user(credentials, request=request)
# Note: EmailUser uses 'email' as primary key, not 'id'
# User is already detached (created with fresh session that was closed)
user_email = user.email
user_id = user_email # For EmailUser, email IS the ID
# Store user in request state for downstream use
request.state.user = user
logger.info(f"✓ Authenticated user: {user_email if user_email else user_id}")
# Log successful authentication (only if logging level is "all")
# DB session created only when needed
if log_success:
db = SessionLocal()
try:
security_logger.log_authentication_attempt(
user_id=user_id,
user_email=user_email,
auth_method="bearer_token",
success=True,
client_ip=request.client.host if request.client else "unknown",
user_agent=request.headers.get("user-agent"),
db=db,
)
db.commit()
except Exception as log_error:
logger.debug(f"Failed to log successful auth: {log_error}")
finally:
try:
db.close()
except Exception as close_error:
logger.debug(f"Failed to close database session: {close_error}")
except HTTPException as e:
if e.status_code in (401, 403) and e.detail in _HARD_DENY_DETAILS:
logger.info(f"✗ Auth rejected ({e.status_code}): {e.detail}")
if log_failure:
db = SessionLocal()
try:
security_logger.log_authentication_attempt(
user_id="unknown",
user_email=None,
auth_method="bearer_token",
success=False,
client_ip=request.client.host if request.client else "unknown",
user_agent=request.headers.get("user-agent"),
failure_reason=str(e.detail),
db=db,
)
db.commit()
except Exception as log_error:
logger.debug(f"Failed to log auth failure: {log_error}")
finally:
try:
db.close()
except Exception as close_error:
logger.debug(f"Failed to close database session: {close_error}")
# Browser/admin requests with stale cookies: let the request continue
# without user context so the RBAC layer can redirect to /admin/login.
# API requests: return a hard JSON 401/403 deny.
# Detection must match rbac.py's is_browser_request logic (Accept,
# HX-Request, and Referer: /admin) to avoid breaking admin UI flows.
accept_header = request.headers.get("accept", "")
is_htmx = request.headers.get("hx-request") == "true"
referer = request.headers.get("referer", "")
is_browser = "text/html" in accept_header or is_htmx or "/admin" in referer
if is_browser:
logger.debug("Browser request with rejected auth — continuing without user for redirect")
return await call_next(request)
# Include essential security headers since this response bypasses
# SecurityHeadersMiddleware (it returns before call_next).
resp_headers = dict(e.headers) if e.headers else {}
resp_headers.setdefault("X-Content-Type-Options", "nosniff")
resp_headers.setdefault("Referrer-Policy", "strict-origin-when-cross-origin")
return JSONResponse(
status_code=e.status_code,
content={"detail": e.detail},
headers=resp_headers,
)
# Non-security HTTP errors (e.g. 500 from a downstream service) — continue as anonymous
logger.info(f"✗ Auth context extraction failed (continuing as anonymous): {e}")
except Exception as e:
# Non-HTTP errors (network, decode, etc.) — continue as anonymous
logger.info(f"✗ Auth context extraction failed (continuing as anonymous): {e}")
# Log failed authentication attempt (based on logging level)
# DB session created only when needed
if log_failure:
db = SessionLocal()
try:
security_logger.log_authentication_attempt(
user_id="unknown",
user_email=None,
auth_method="bearer_token",
success=False,
client_ip=request.client.host if request.client else "unknown",
user_agent=request.headers.get("user-agent"),
failure_reason=str(e),
db=db,
)
db.commit()
except Exception as log_error:
logger.debug(f"Failed to log auth failure: {log_error}")
finally:
try:
db.close()
except Exception as close_error:
logger.debug(f"Failed to close database session: {close_error}")
# Continue with request
return await call_next(request)