|
| 1 | +""" |
| 2 | +Shelly-specific DigestAuth that correctly handles empty opaque values. |
| 3 | +
|
| 4 | +httpx 0.28.1's DigestAuth uses ``if challenge.opaque:`` which treats b"" |
| 5 | +as falsy, omitting opaque from the Authorization header. RFC 7616 requires |
| 6 | +clients to return opaque unchanged — even when empty. Shelly Wall Display |
| 7 | +devices send ``opaque=""`` and reject responses that omit it. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import httpx |
| 13 | +from httpx._auth import _DigestAuthChallenge |
| 14 | +from httpx._models import Request |
| 15 | +from httpx._utils import to_str |
| 16 | + |
| 17 | + |
| 18 | +class ShellyDigestAuth(httpx.DigestAuth): |
| 19 | + # Pinned to httpx 0.28.1 — verify on upgrades. |
| 20 | + def _build_auth_header( |
| 21 | + self, request: Request, challenge: _DigestAuthChallenge |
| 22 | + ) -> str: |
| 23 | + hash_func = self._ALGORITHM_TO_HASH_FUNCTION[challenge.algorithm.upper()] |
| 24 | + |
| 25 | + def digest(data: bytes) -> bytes: |
| 26 | + return hash_func(data).hexdigest().encode() |
| 27 | + |
| 28 | + A1 = b":".join((self._username, challenge.realm, self._password)) |
| 29 | + |
| 30 | + path = request.url.raw_path |
| 31 | + A2 = b":".join((request.method.encode(), path)) |
| 32 | + HA2 = digest(A2) |
| 33 | + |
| 34 | + nc_value = b"%08x" % self._nonce_count |
| 35 | + cnonce = self._get_client_nonce(self._nonce_count, challenge.nonce) |
| 36 | + self._nonce_count += 1 |
| 37 | + |
| 38 | + HA1 = digest(A1) |
| 39 | + if challenge.algorithm.lower().endswith("-sess"): |
| 40 | + HA1 = digest(b":".join((HA1, challenge.nonce, cnonce))) |
| 41 | + |
| 42 | + qop = self._resolve_qop(challenge.qop, request=request) |
| 43 | + if qop is None: |
| 44 | + digest_data = [HA1, challenge.nonce, HA2] |
| 45 | + else: |
| 46 | + digest_data = [HA1, challenge.nonce, nc_value, cnonce, qop, HA2] |
| 47 | + |
| 48 | + format_args: dict[str, bytes] = { |
| 49 | + "username": self._username, |
| 50 | + "realm": challenge.realm, |
| 51 | + "nonce": challenge.nonce, |
| 52 | + "uri": path, |
| 53 | + "response": digest(b":".join(digest_data)), |
| 54 | + "algorithm": challenge.algorithm.encode(), |
| 55 | + } |
| 56 | + if challenge.opaque is not None: |
| 57 | + format_args["opaque"] = challenge.opaque |
| 58 | + if qop: |
| 59 | + format_args["qop"] = b"auth" |
| 60 | + format_args["nc"] = nc_value |
| 61 | + format_args["cnonce"] = cnonce |
| 62 | + |
| 63 | + return "Digest " + self._get_header_value(format_args) |
| 64 | + |
| 65 | + def _get_header_value(self, header_fields: dict[str, bytes]) -> str: |
| 66 | + NON_QUOTED_FIELDS = ("algorithm", "qop", "nc") |
| 67 | + QUOTED_TEMPLATE = '{}="{}"' |
| 68 | + NON_QUOTED_TEMPLATE = "{}={}" |
| 69 | + |
| 70 | + header_value = "" |
| 71 | + for i, (field, value) in enumerate(header_fields.items()): |
| 72 | + if i > 0: |
| 73 | + header_value += ", " |
| 74 | + template = ( |
| 75 | + QUOTED_TEMPLATE |
| 76 | + if field not in NON_QUOTED_FIELDS |
| 77 | + else NON_QUOTED_TEMPLATE |
| 78 | + ) |
| 79 | + header_value += template.format(field, to_str(value)) |
| 80 | + |
| 81 | + return header_value |
0 commit comments