Support DigestAuth with "x-www-authenticate" in first response header #3307
-
The Fronius inverter for photovoltaic systems uses for its local REST API digest authentication. It uses in its initial response header Here an initial curl command with the response header:
httpx 0.27.2 fails to extract Lines 201 to 212 in 87713d2 My script import httpx
url = "http://192.168.1.141/config/emrs" # adjust IP to that one of your inverter
user = "technician"
pw = <INSERT PW>
auth = httpx.DigestAuth(user,pw)
with httpx.Client(auth=auth) as client:
r = client.get(url)
print(r.status_code)
print(r.json()['priorities'])
r = client.get(url)
print(r.status_code)
print(r.json()['priorities']) runs only with following quick diff ❯ git diff
diff --git a/httpx/_auth.py b/httpx/_auth.py
index b03971a..e4f1600 100644
--- a/httpx/_auth.py
+++ b/httpx/_auth.py
@@ -198,17 +198,27 @@ class DigestAuth(Auth):
response = yield request
- if response.status_code != 401 or "www-authenticate" not in response.headers:
+ auth_header_key = False
+ for key in [ "www-authenticate", "x-www-authenticate" ]:
+ if key in response.headers:
+ if auth_header_key == False:
+ auth_header_key = key
+ else:
+ message = "Malformed Digest WWW-Authenticate response header"
+ raise ProtocolError(message, request=request)
+
+ if response.status_code != 401 or auth_header_key == False:
# If the response is not a 401 then we don't
# need to build an authenticated request.
return
- for auth_header in response.headers.get_list("www-authenticate"):
+ for auth_header in response.headers.get_list(auth_header_key):
if auth_header.lower().startswith("digest "):
break
else:
# If the response does not include a 'WWW-Authenticate: Digest ...'
- # header, then we don't need to build an authenticated request.
+ # or 'X-WWW-Authenticate: Digest ...' header, then we don't need to
+ # build an authenticated request.
return
self._last_challenge = self._parse_challenge(request, response, auth_header) Note, this diff is only to show that it works. I did not follow any coding guidelines of this project. For reference, here a couple of projects which have written their custom digest authorization to handle the Fronius response header:
Software versions of the inverter:
I am not sure how common it is to use However, I still think supporting this deviation might be acceptable. If Fronius screwed this up more heavily than I think, I understand that httpx cannot support all quirks of all digest implementations. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I doubt it. Guidelines for these types of case...
|
Beta Was this translation helpful? Give feedback.
-
thanks for your quick response.
IMHO it is not fun. But I can imagine how you mean it. Apparently the prefix
Other occurrences:
which also handles this. It might be the case that old AngularJS apps might have introduced it and the Fronius backend was developed in AngularJS.
Since the probably well used node.js module urllib supports it, is there any chance that this might be more convincing than the Fronius inverter on its own? |
Beta Was this translation helpful? Give feedback.
fun
I doubt it.
Guidelines for these types of case...