-
-
Notifications
You must be signed in to change notification settings - Fork 862
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Exception heirachy * Exception heirarchy * Formatting tweaks * Update httpx/_exceptions.py Co-authored-by: Florimond Manca <[email protected]> * Update httpx/_exceptions.py Co-authored-by: Florimond Manca <[email protected]> * Update httpx/_exceptions.py Co-authored-by: Florimond Manca <[email protected]> * Update httpx/_exceptions.py Co-authored-by: Florimond Manca <[email protected]> Co-authored-by: Florimond Manca <[email protected]>
- Loading branch information
1 parent
2ba9c1e
commit 9409900
Showing
11 changed files
with
218 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,20 +116,24 @@ def auth_flow(self, request: Request) -> typing.Generator[Request, Response, Non | |
# need to build an authenticated request. | ||
return | ||
|
||
header = response.headers["www-authenticate"] | ||
challenge = self._parse_challenge(header) | ||
challenge = self._parse_challenge(request, response) | ||
request.headers["Authorization"] = self._build_auth_header(request, challenge) | ||
yield request | ||
|
||
def _parse_challenge(self, header: str) -> "_DigestAuthChallenge": | ||
def _parse_challenge( | ||
self, request: Request, response: Response | ||
) -> "_DigestAuthChallenge": | ||
""" | ||
Returns a challenge from a Digest WWW-Authenticate header. | ||
These take the form of: | ||
`Digest realm="[email protected]",qop="auth,auth-int",nonce="abc",opaque="xyz"` | ||
""" | ||
header = response.headers["www-authenticate"] | ||
|
||
scheme, _, fields = header.partition(" ") | ||
if scheme.lower() != "digest": | ||
raise ProtocolError("Header does not start with 'Digest'") | ||
message = "Header does not start with 'Digest'" | ||
raise ProtocolError(message, request=request) | ||
|
||
header_dict: typing.Dict[str, str] = {} | ||
for field in parse_http_list(fields): | ||
|
@@ -146,7 +150,8 @@ def _parse_challenge(self, header: str) -> "_DigestAuthChallenge": | |
realm=realm, nonce=nonce, qop=qop, opaque=opaque, algorithm=algorithm | ||
) | ||
except KeyError as exc: | ||
raise ProtocolError("Malformed Digest WWW-Authenticate header") from exc | ||
message = "Malformed Digest WWW-Authenticate header" | ||
raise ProtocolError(message, request=request) from exc | ||
|
||
def _build_auth_header( | ||
self, request: Request, challenge: "_DigestAuthChallenge" | ||
|
@@ -171,7 +176,7 @@ def digest(data: bytes) -> bytes: | |
if challenge.algorithm.lower().endswith("-sess"): | ||
HA1 = digest(b":".join((HA1, challenge.nonce, cnonce))) | ||
|
||
qop = self._resolve_qop(challenge.qop) | ||
qop = self._resolve_qop(challenge.qop, request=request) | ||
if qop is None: | ||
digest_data = [HA1, challenge.nonce, HA2] | ||
else: | ||
|
@@ -221,7 +226,9 @@ def _get_header_value(self, header_fields: typing.Dict[str, bytes]) -> str: | |
|
||
return header_value | ||
|
||
def _resolve_qop(self, qop: typing.Optional[bytes]) -> typing.Optional[bytes]: | ||
def _resolve_qop( | ||
self, qop: typing.Optional[bytes], request: Request | ||
) -> typing.Optional[bytes]: | ||
if qop is None: | ||
return None | ||
qops = re.split(b", ?", qop) | ||
|
@@ -231,7 +238,8 @@ def _resolve_qop(self, qop: typing.Optional[bytes]) -> typing.Optional[bytes]: | |
if qops == [b"auth-int"]: | ||
raise NotImplementedError("Digest auth-int support is not yet implemented") | ||
|
||
raise ProtocolError(f'Unexpected qop value "{qop!r}" in digest auth') | ||
message = f'Unexpected qop value "{qop!r}" in digest auth' | ||
raise ProtocolError(message, request=request) | ||
|
||
|
||
class _DigestAuthChallenge: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.