Skip to content
Myr

Handle multiple auth headers correctly (#1240)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Handle multiple auth headers correctly

authored by Tom Christie · committed by GitHub · parent fa7661b · Browse files · 2 files+29 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+29 added−14 removed

  • tests:+15 added−6 removed
  • other:+14 added−8 removed

2 files+29 added−14 removed

modified:httpx/_auth.py+14 added-8 removed
110 unmodified lines
111112 response = yield request113114 if response.status_code != 401 or "www-authenticate" not in response.headers:115 # If the response is not a 401 WWW-Authenticate, then we don't115 # If the response is not a 401 then we don't116 # need to build an authenticated request.117 return118119 for auth_header in response.headers.get_list("www-authenticate"):120 if auth_header.lower().startswith("digest "):121 break122 else:123 # If the response does not include a 'WWW-Authenticate: Digest ...'124 # header, then we don't need to build an authenticated request.125 return126119 challenge = self._parse_challenge(request, response)127 challenge = self._parse_challenge(request, response, auth_header)128 request.headers["Authorization"] = self._build_auth_header(request, challenge)129 yield request130131 def _parse_challenge(124 self, request: Request, response: Response132 self, request: Request, response: Response, auth_header: str133 ) -> "_DigestAuthChallenge":134 """135 Returns a challenge from a Digest WWW-Authenticate header.136 These take the form of:137 `Digest realm="realm@host.com",qop="auth,auth-int",nonce="abc",opaque="xyz"`138 """131 header = response.headers["www-authenticate"]139 scheme, _, fields = auth_header.partition(" ")140133 scheme, _, fields = header.partition(" ")134 if scheme.lower() != "digest":135 message = "Header does not start with 'Digest'"136 raise ProtocolError(message, request=request)141 # This method should only ever have been called with a Digest auth header.142 assert scheme.lower() == "digest"143144 header_dict: typing.Dict[str, str] = {}145 for field in parse_http_list(fields):146 key, value = field.strip().split("=", 1)
118 unmodified lines
modified:tests/client/test_auth.py+15 added-6 removed
354 unmodified lines
355 assert response.json() == {"auth": None}356 assert len(response.history) == 0357358359def test_digest_auth_returns_no_auth_if_alternate_auth_scheme() -> None:360 url = "https://example.org/"361 auth = DigestAuth(username="tomchristie", password="password123")362 auth_header = b"Token ..."363364 client = httpx.Client(365 transport=SyncMockTransport(auth_header=auth_header, status_code=401)366 )367 response = client.get(url, auth=auth)368369 assert response.status_code == 401370 assert response.json() == {"auth": None}371 assert len(response.history) == 0372373374@pytest.mark.asyncio375async def test_digest_auth_200_response_including_digest_auth_header() -> None:376 url = "https://example.org/"377 auth = DigestAuth(username="tomchristie", password="password123")
155 unmodified lines
533@pytest.mark.parametrize(534 "auth_header",535 [536 b'Digest realm="httpx@example.org", qop="auth"', # missing fields522 b'realm="httpx@example.org", qop="auth"', # not starting with Digest523 b'DigestZ realm="httpx@example.org", qop="auth"'524 b'qop="auth,auth-int",nonce="abc",opaque="xyz"',537 b'Digest realm="httpx@example.org", qop="auth,au', # malformed fields list538 ],539)540@pytest.mark.asyncio
12 unmodified lines
553@pytest.mark.parametrize(554 "auth_header",555 [556 b'Digest realm="httpx@example.org", qop="auth"', # missing fields545 b'realm="httpx@example.org", qop="auth"', # not starting with Digest546 b'DigestZ realm="httpx@example.org", qop="auth"'547 b'qop="auth,auth-int",nonce="abc",opaque="xyz"',557 b'Digest realm="httpx@example.org", qop="auth,au', # malformed fields list558 ],559)560def test_sync_digest_auth_raises_protocol_error_on_malformed_header(
106 unmodified lines