fix(oauth): fix invalid_scope and invalid_token errors for user OAuth clients

Three bugs fixed for social-login users connecting Claude.ai via OAuth:

1. User OAuth clients now allow 'admin' scope by default — previously
   defaulted to ['read', 'write'], causing invalid_scope when Claude.ai
   requests 'admin' scope during authorization.

2. Fix JWT tokens with 'aud' claim being rejected by middleware — when
   Claude.ai sends RFC 8707 resource parameter, the issued JWT gets an
   aud claim. validate_access_token() now sets verify_aud=False to
   prevent PyJWT InvalidAudienceError from silently marking valid tokens
   as invalid in _is_valid_token().

3. Fix NameError in user_mcp_handler tools/call scope check — key_info
   was only defined in the mhu_ auth path but referenced in tools/call
   for both paths. Replaced with key_scopes variable set by both mhu_
   and JWT auth paths.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
airano
2026-02-26 00:37:43 +03:30
parent 4a5381d765
commit a806671e2d
3 changed files with 8 additions and 2 deletions

View File

@@ -210,6 +210,9 @@ async def user_mcp_handler(request: Request) -> Response:
api_key = auth_header[7:] # Strip "Bearer "
# Shared scope tracking — set by whichever auth path succeeds
key_scopes: list[str] = []
# Try mhu_ API key first, then fall back to OAuth JWT token
if api_key.startswith("mhu_"):
try:
@@ -234,6 +237,7 @@ async def user_mcp_handler(request: Request) -> Response:
_jsonrpc_error(None, -32600, "API key does not match user"),
status_code=403,
)
key_scopes = key_info.get("scopes", "read").split()
else:
# Try OAuth JWT token (issued after consent flow via GitHub/Google login)
try:
@@ -258,6 +262,7 @@ async def user_mcp_handler(request: Request) -> Response:
_jsonrpc_error(None, -32600, "Token user mismatch"),
status_code=403,
)
key_scopes = jwt_payload.get("scope", "read").split()
except pyjwt.ExpiredSignatureError:
return JSONResponse(
_jsonrpc_error(None, -32600, "Token expired"),
@@ -360,7 +365,7 @@ async def user_mcp_handler(request: Request) -> Response:
return JSONResponse(_jsonrpc_error(req_id, -32601, f"Tool '{tool_name}' not found"))
required_scope = tool_def.required_scope
key_scopes = key_info.get("scopes", "").split()
# key_scopes is set during authentication (both mhu_ and JWT paths)
scope_hierarchy = {"read": 1, "write": 2, "admin": 3}
required_level = scope_hierarchy.get(required_scope, 0)