Skip to content
This repository was archived by the owner on Mar 8, 2026. It is now read-only.

Commit 309c769

Browse files
committed
waring: harden internal auth error handling
1 parent 839e636 commit 309c769

1 file changed

Lines changed: 68 additions & 57 deletions

File tree

backend/app/core/service_auth.py

Lines changed: 68 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -109,69 +109,80 @@ async def get_service_context(request: Request) -> ServiceContext:
109109
Raises:
110110
HTTPException: 401 if token invalid, 403 if service not recognized
111111
"""
112-
# Extract token
113-
token = _extract_service_token(request)
114-
115-
if not token:
116-
logger.warning(
117-
"Service auth failed - no token",
118-
extra={"path": request.url.path, "method": request.method}
119-
)
120-
_log_service_auth_background(request, "No service token provided", success=False)
121-
raise HTTPException(
122-
status_code=status.HTTP_401_UNAUTHORIZED,
123-
detail="Service authentication required. Provide Bearer token.",
124-
headers={"WWW-Authenticate": "Bearer"},
112+
try:
113+
# Extract token
114+
token = _extract_service_token(request)
115+
116+
if not token:
117+
logger.warning(
118+
"Service auth failed - no token",
119+
extra={"path": request.url.path, "method": request.method}
120+
)
121+
_log_service_auth_background(request, "No service token provided", success=False)
122+
raise HTTPException(
123+
status_code=status.HTTP_401_UNAUTHORIZED,
124+
detail="Service authentication required. Provide Bearer token.",
125+
headers={"WWW-Authenticate": "Bearer"},
126+
)
127+
128+
# Validate token and get service name
129+
service_name = _validate_service_token(token)
130+
131+
if not service_name:
132+
logger.warning(
133+
"Service auth failed - invalid token",
134+
extra={"path": request.url.path, "method": request.method}
135+
)
136+
_log_service_auth_background(request, "Invalid service token", success=False)
137+
raise HTTPException(
138+
status_code=status.HTTP_401_UNAUTHORIZED,
139+
detail="Invalid service token",
140+
headers={"WWW-Authenticate": "Bearer"},
141+
)
142+
143+
# Get service definition
144+
service_def = SERVICE_DEFINITIONS.get(service_name)
145+
146+
if not service_def:
147+
logger.error(
148+
"Service auth failed - unknown service",
149+
extra={"service_name": service_name, "path": request.url.path}
150+
)
151+
_log_service_auth_background(request, f"Unknown service: {service_name}", success=False, service_name=service_name)
152+
raise HTTPException(
153+
status_code=status.HTTP_403_FORBIDDEN,
154+
detail=f"Service '{service_name}' not recognized"
155+
)
156+
157+
# Create service context
158+
context = ServiceContext(
159+
service_name=service_name,
160+
scopes=service_def["scopes"]
125161
)
126-
127-
# Validate token and get service name
128-
service_name = _validate_service_token(token)
129-
130-
if not service_name:
131-
logger.warning(
132-
"Service auth failed - invalid token",
133-
extra={"path": request.url.path, "method": request.method}
162+
163+
# Log successful authentication
164+
_log_service_auth_background(request, "Service authenticated", success=True, service_name=service_name)
165+
166+
logger.info(
167+
"Service authenticated",
168+
extra={
169+
"service_name": service_name,
170+
"scopes": list(context.scopes),
171+
"path": request.url.path,
172+
}
134173
)
135-
_log_service_auth_background(request, "Invalid service token", success=False)
174+
175+
return context
176+
except HTTPException:
177+
raise
178+
except Exception as e:
179+
logger.error("Service auth error", exc_info=True)
180+
_log_service_auth_background(request, "Service auth error", success=False)
136181
raise HTTPException(
137182
status_code=status.HTTP_401_UNAUTHORIZED,
138-
detail="Invalid service token",
183+
detail="Service authentication failed",
139184
headers={"WWW-Authenticate": "Bearer"},
140185
)
141-
142-
# Get service definition
143-
service_def = SERVICE_DEFINITIONS.get(service_name)
144-
145-
if not service_def:
146-
logger.error(
147-
"Service auth failed - unknown service",
148-
extra={"service_name": service_name, "path": request.url.path}
149-
)
150-
_log_service_auth_background(request, f"Unknown service: {service_name}", success=False, service_name=service_name)
151-
raise HTTPException(
152-
status_code=status.HTTP_403_FORBIDDEN,
153-
detail=f"Service '{service_name}' not recognized"
154-
)
155-
156-
# Create service context
157-
context = ServiceContext(
158-
service_name=service_name,
159-
scopes=service_def["scopes"]
160-
)
161-
162-
# Log successful authentication
163-
_log_service_auth_background(request, "Service authenticated", success=True, service_name=service_name)
164-
165-
logger.info(
166-
"Service authenticated",
167-
extra={
168-
"service_name": service_name,
169-
"scopes": list(context.scopes),
170-
"path": request.url.path,
171-
}
172-
)
173-
174-
return context
175186

176187

177188
async def require_service(

0 commit comments

Comments
 (0)