@@ -205,12 +205,12 @@ def __init__(
205
205
)
206
206
self ._initialized = False
207
207
208
- def _extract_resource_metadata_from_www_auth (self , init_response : httpx .Response ) -> str | None :
208
+ def _extract_field_from_www_auth (self , init_response : httpx .Response , field_name : str ) -> str | None :
209
209
"""
210
- Extract protected resource metadata URL from WWW-Authenticate header as per RFC9728 .
210
+ Extract field from WWW-Authenticate header.
211
211
212
212
Returns:
213
- Resource metadata URL if found in WWW-Authenticate header, None otherwise
213
+ Field value if found in WWW-Authenticate header, None otherwise
214
214
"""
215
215
if not init_response or init_response .status_code != 401 :
216
216
return None
@@ -219,8 +219,8 @@ def _extract_resource_metadata_from_www_auth(self, init_response: httpx.Response
219
219
if not www_auth_header :
220
220
return None
221
221
222
- # Pattern matches: resource_metadata="url " or resource_metadata=url (unquoted)
223
- pattern = r'resource_metadata =(?:"([^"]+)"|([^\s,]+))'
222
+ # Pattern matches: field_name="value " or field_name=value (unquoted)
223
+ pattern = rf' { field_name } =(?:"([^"]+)"|([^\s,]+))'
224
224
match = re .search (pattern , www_auth_header )
225
225
226
226
if match :
@@ -229,29 +229,23 @@ def _extract_resource_metadata_from_www_auth(self, init_response: httpx.Response
229
229
230
230
return None
231
231
232
+ def _extract_resource_metadata_from_www_auth (self , init_response : httpx .Response ) -> str | None :
233
+ """
234
+ Extract protected resource metadata URL from WWW-Authenticate header as per RFC9728.
235
+
236
+ Returns:
237
+ Resource metadata URL if found in WWW-Authenticate header, None otherwise
238
+ """
239
+ return self ._extract_field_from_www_auth (init_response , "resource_metadata" )
240
+
232
241
def _extract_scope_from_www_auth (self , init_response : httpx .Response ) -> str | None :
233
242
"""
234
243
Extract scope parameter from WWW-Authenticate header as per RFC6750.
235
244
236
245
Returns:
237
246
Scope string if found in WWW-Authenticate header, None otherwise
238
247
"""
239
- if not init_response or init_response .status_code != 401 :
240
- return None
241
-
242
- www_auth_header = init_response .headers .get ("WWW-Authenticate" )
243
- if not www_auth_header :
244
- return None
245
-
246
- # Pattern matches: scope="value" or scope=value (unquoted)
247
- pattern = r'scope=(?:"([^"]+)"|([^\s,]+))'
248
- match = re .search (pattern , www_auth_header )
249
-
250
- if match :
251
- # Return quoted value if present, otherwise unquoted value
252
- return match .group (1 ) or match .group (2 )
253
-
254
- return None
248
+ return self ._extract_field_from_www_auth (init_response , "scope" )
255
249
256
250
async def _discover_protected_resource (self , init_response : httpx .Response ) -> httpx .Request :
257
251
# RFC9728: Try to extract resource_metadata URL from WWW-Authenticate header of the initial response
0 commit comments