Skip to content

Commit cfb3f7e

Browse files
committed
Added method for parsing WWW-Authenticate header
1 parent 6f43d1f commit cfb3f7e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/mcp/client/auth.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import base64
88
import hashlib
99
import logging
10+
import re
1011
import secrets
1112
import string
1213
import time
@@ -33,6 +34,30 @@
3334
logger = logging.getLogger(__name__)
3435

3536

37+
def _extract_resource_metadata_from_www_auth(header_value: str) -> str | None:
38+
"""
39+
Parse WWW-Authenticate header to extract resource_metadata parameter.
40+
41+
According to RFC9728, the header format is:
42+
WWW-Authenticate: Bearer resource_metadata="https://example.com/.well-known/oauth-protected-resource"
43+
44+
Returns the resource_metadata URL if found, None otherwise.
45+
"""
46+
if not header_value:
47+
return None
48+
49+
# Look for resource_metadata parameter in the header
50+
# Pattern matches: resource_metadata="url" or resource_metadata=url (unquoted)
51+
pattern = r'resource_metadata=(?:"([^"]+)"|([^\s,]+))'
52+
match = re.search(pattern, header_value)
53+
54+
if match:
55+
# Return quoted value if present, otherwise unquoted value
56+
return match.group(1) or match.group(2)
57+
58+
return None
59+
60+
3661
class OAuthFlowError(Exception):
3762
"""Base exception for OAuth flow errors."""
3863

0 commit comments

Comments
 (0)