Fix RFC 2069 mode digest authentication (#3045)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Fix RFC 2069 mode digest authentication * Update CHANGELOG.md
authored by Tereza Tomcova · committed by GitHub · parent ca51b45 · Browse files · 3 files·+170 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+170 added−3 removed
- tests:+165 added−0 removed
- other:+5 added−3 removed
Provenance
- Commit
99cba6a- Tree
6309516- Author
Tereza Tomcova <the-ress@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
ca51b45
3 files changed+170 added−3 removed
modified:httpx/_auth.py+4 added-3 removed
278 unmodified lines279 HA1 = digest(b":".join((HA1, challenge.nonce, cnonce)))280 281 qop = self._resolve_qop(challenge.qop, request=request)282 if qop is None:283+ # Following RFC 2069284 digest_data = [HA1, challenge.nonce, HA2]285 else:285- digest_data = [challenge.nonce, nc_value, cnonce, qop, HA2]286- key_digest = b":".join(digest_data)286+ # Following RFC 2617/7616287+ digest_data = [HA1, challenge.nonce, nc_value, cnonce, qop, HA2]288 289 format_args = {290 "username": self._username,291 "realm": challenge.realm,292 "nonce": challenge.nonce,293 "uri": path,293- "response": digest(b":".join((HA1, key_digest))),294+ "response": digest(b":".join(digest_data)),295 "algorithm": challenge.algorithm.encode(),296 }297 if challenge.opaque:298 format_args["opaque"] = challenge.opaque55 unmodified linesmodified:tests/test_auth.py+165 added
138 unmodified lines139 content=b"Hello, world!", status_code=200, request=request140 )141 with pytest.raises(StopIteration):142 flow.send(response)143+144+145+def test_digest_auth_rfc_2069():146+ # Example from https://datatracker.ietf.org/doc/html/rfc2069#section-2.4147+ # with corrected response from https://www.rfc-editor.org/errata/eid749148+149+ auth = httpx.DigestAuth(username="Mufasa", password="CircleOfLife")150+ request = httpx.Request("GET", "https://www.example.com/dir/index.html")151+152+ # The initial request should not include an auth header.153+ flow = auth.sync_auth_flow(request)154+ request = next(flow)155+ assert "Authorization" not in request.headers156+157+ # If a 401 response is returned, then a digest auth request is made.158+ headers = {159+ "WWW-Authenticate": (160+ 'Digest realm="testrealm@host.com", '161+ 'nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", '162+ 'opaque="5ccc069c403ebaf9f0171e9517f40e41"'163+ )164+ }165+ response = httpx.Response(166+ content=b"Auth required", status_code=401, headers=headers, request=request167+ )168+ request = flow.send(response)169+ assert request.headers["Authorization"].startswith("Digest")170+ assert 'username="Mufasa"' in request.headers["Authorization"]171+ assert 'realm="testrealm@host.com"' in request.headers["Authorization"]172+ assert (173+ 'nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093"' in request.headers["Authorization"]174+ )175+ assert 'uri="/dir/index.html"' in request.headers["Authorization"]176+ assert (177+ 'opaque="5ccc069c403ebaf9f0171e9517f40e41"' in request.headers["Authorization"]178+ )179+ assert (180+ 'response="1949323746fe6a43ef61f9606e7febea"'181+ in request.headers["Authorization"]182+ )183+184+ # No other requests are made.185+ response = httpx.Response(content=b"Hello, world!", status_code=200)186+ with pytest.raises(StopIteration):187+ flow.send(response)188+189+190+def test_digest_auth_rfc_7616_md5(monkeypatch):191+ # Example from https://datatracker.ietf.org/doc/html/rfc7616#section-3.9.1192+193+ def mock_get_client_nonce(nonce_count: int, nonce: bytes) -> bytes:194+ return "f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ".encode()195+196+ auth = httpx.DigestAuth(username="Mufasa", password="Circle of Life")197+ monkeypatch.setattr(auth, "_get_client_nonce", mock_get_client_nonce)198+199+ request = httpx.Request("GET", "https://www.example.com/dir/index.html")200+201+ # The initial request should not include an auth header.202+ flow = auth.sync_auth_flow(request)203+ request = next(flow)204+ assert "Authorization" not in request.headers205+206+ # If a 401 response is returned, then a digest auth request is made.207+ headers = {208+ "WWW-Authenticate": (209+ 'Digest realm="http-auth@example.org", '210+ 'qop="auth, auth-int", '211+ "algorithm=MD5, "212+ 'nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v", '213+ 'opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"'214+ )215+ }216+ response = httpx.Response(217+ content=b"Auth required", status_code=401, headers=headers, request=request218+ )219+ request = flow.send(response)220+ assert request.headers["Authorization"].startswith("Digest")221+ assert 'username="Mufasa"' in request.headers["Authorization"]222+ assert 'realm="http-auth@example.org"' in request.headers["Authorization"]223+ assert 'uri="/dir/index.html"' in request.headers["Authorization"]224+ assert "algorithm=MD5" in request.headers["Authorization"]225+ assert (226+ 'nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v"'227+ in request.headers["Authorization"]228+ )229+ assert "nc=00000001" in request.headers["Authorization"]230+ assert (231+ 'cnonce="f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ"'232+ in request.headers["Authorization"]233+ )234+ assert "qop=auth" in request.headers["Authorization"]235+ assert (236+ 'opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"'237+ in request.headers["Authorization"]238+ )239+ assert (240+ 'response="8ca523f5e9506fed4657c9700eebdbec"'241+ in request.headers["Authorization"]242+ )243+244+ # No other requests are made.245+ response = httpx.Response(content=b"Hello, world!", status_code=200)246+ with pytest.raises(StopIteration):247+ flow.send(response)248+249+250+def test_digest_auth_rfc_7616_sha_256(monkeypatch):251+ # Example from https://datatracker.ietf.org/doc/html/rfc7616#section-3.9.1252+253+ def mock_get_client_nonce(nonce_count: int, nonce: bytes) -> bytes:254+ return "f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ".encode()255+256+ auth = httpx.DigestAuth(username="Mufasa", password="Circle of Life")257+ monkeypatch.setattr(auth, "_get_client_nonce", mock_get_client_nonce)258+259+ request = httpx.Request("GET", "https://www.example.com/dir/index.html")260+261+ # The initial request should not include an auth header.262+ flow = auth.sync_auth_flow(request)263+ request = next(flow)264+ assert "Authorization" not in request.headers265+266+ # If a 401 response is returned, then a digest auth request is made.267+ headers = {268+ "WWW-Authenticate": (269+ 'Digest realm="http-auth@example.org", '270+ 'qop="auth, auth-int", '271+ "algorithm=SHA-256, "272+ 'nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v", '273+ 'opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"'274+ )275+ }276+ response = httpx.Response(277+ content=b"Auth required", status_code=401, headers=headers, request=request278+ )279+ request = flow.send(response)280+ assert request.headers["Authorization"].startswith("Digest")281+ assert 'username="Mufasa"' in request.headers["Authorization"]282+ assert 'realm="http-auth@example.org"' in request.headers["Authorization"]283+ assert 'uri="/dir/index.html"' in request.headers["Authorization"]284+ assert "algorithm=SHA-256" in request.headers["Authorization"]285+ assert (286+ 'nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v"'287+ in request.headers["Authorization"]288+ )289+ assert "nc=00000001" in request.headers["Authorization"]290+ assert (291+ 'cnonce="f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ"'292+ in request.headers["Authorization"]293+ )294+ assert "qop=auth" in request.headers["Authorization"]295+ assert (296+ 'opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"'297+ in request.headers["Authorization"]298+ )299+ assert (300+ 'response="753927fa0e85d155564e2e272a28d1802ca10daf4496794697cf8db5856cb6c1"'301+ in request.headers["Authorization"]302+ )303+304+ # No other requests are made.305+ response = httpx.Response(content=b"Hello, world!", status_code=200)306+ with pytest.raises(StopIteration):307+ flow.send(response)modified:CHANGELOG.md+1 added
7 unmodified lines8 9 ### Fixed10 11 * Respect the `http1` argument while configuring proxy transports. (#3023)12+* Fix RFC 2069 mode digest authentication. (#3045)13 14 ## 0.26.0 (20th December, 2023)15 16 ### Added1063 unmodified lines