Skip to content

Commit f9a5a3a

Browse files
calvinbuiZeroIntensitypicnixzgpshead
authored
gh-128192: support HTTP sha-256 digest authentication as per RFC-7617 (GH-128193)
support sha-256 digest authentication Co-authored-by: Peter Bierma <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Gregory P. Smith <[email protected]>
1 parent 492b224 commit f9a5a3a

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

Doc/library/urllib.request.rst

+3
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ The following classes are provided:
411411
:ref:`http-password-mgr` for information on the interface that must be
412412
supported.
413413

414+
.. versionchanged:: 3.14
415+
Added support for HTTP digest authentication algorithm ``SHA-256``.
416+
414417

415418
.. class:: HTTPDigestAuthHandler(password_mgr=None)
416419

Doc/whatsnew/3.14.rst

+8
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,14 @@ unittest
646646
(Contributed by Jacob Walls in :gh:`80958`.)
647647

648648

649+
urllib
650+
------
651+
652+
* Upgrade HTTP digest authentication algorithm for :mod:`urllib.request` by
653+
supporting SHA-256 digest authentication as specified in :rfc:`7616`.
654+
(Contributed by Calvin Bui in :gh:`128193`.)
655+
656+
649657
uuid
650658
----
651659

Lib/test/test_urllib2.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -1962,10 +1962,29 @@ def test_parse_proxy(self):
19621962

19631963
self.assertRaises(ValueError, _parse_proxy, 'file:/ftp.example.com'),
19641964

1965-
def test_unsupported_algorithm(self):
1966-
handler = AbstractDigestAuthHandler()
1965+
1966+
class TestDigestAlgorithms(unittest.TestCase):
1967+
def setUp(self):
1968+
self.handler = AbstractDigestAuthHandler()
1969+
1970+
def test_md5_algorithm(self):
1971+
H, KD = self.handler.get_algorithm_impls('MD5')
1972+
self.assertEqual(H("foo"), "acbd18db4cc2f85cedef654fccc4a4d8")
1973+
self.assertEqual(KD("foo", "bar"), "4e99e8c12de7e01535248d2bac85e732")
1974+
1975+
def test_sha_algorithm(self):
1976+
H, KD = self.handler.get_algorithm_impls('SHA')
1977+
self.assertEqual(H("foo"), "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
1978+
self.assertEqual(KD("foo", "bar"), "54dcbe67d21d5eb39493d46d89ae1f412d3bd6de")
1979+
1980+
def test_sha256_algorithm(self):
1981+
H, KD = self.handler.get_algorithm_impls('SHA-256')
1982+
self.assertEqual(H("foo"), "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae")
1983+
self.assertEqual(KD("foo", "bar"), "a765a8beaa9d561d4c5cbed29d8f4e30870297fdfa9cb7d6e9848a95fec9f937")
1984+
1985+
def test_invalid_algorithm(self):
19671986
with self.assertRaises(ValueError) as exc:
1968-
handler.get_algorithm_impls('invalid')
1987+
self.handler.get_algorithm_impls('invalid')
19691988
self.assertEqual(
19701989
str(exc.exception),
19711990
"Unsupported digest authentication algorithm 'invalid'"

Lib/urllib/request.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ def http_error_407(self, req, fp, code, msg, headers):
10481048

10491049

10501050
class AbstractDigestAuthHandler:
1051-
# Digest authentication is specified in RFC 2617.
1051+
# Digest authentication is specified in RFC 2617/7616.
10521052

10531053
# XXX The client does not inspect the Authentication-Info header
10541054
# in a successful response.
@@ -1176,11 +1176,14 @@ def get_authorization(self, req, chal):
11761176
return base
11771177

11781178
def get_algorithm_impls(self, algorithm):
1179+
# algorithm names taken from RFC 7616 Section 6.1
11791180
# lambdas assume digest modules are imported at the top level
11801181
if algorithm == 'MD5':
11811182
H = lambda x: hashlib.md5(x.encode("ascii")).hexdigest()
1182-
elif algorithm == 'SHA':
1183+
elif algorithm == 'SHA': # non-standard, retained for compatibility.
11831184
H = lambda x: hashlib.sha1(x.encode("ascii")).hexdigest()
1185+
elif algorithm == 'SHA-256':
1186+
H = lambda x: hashlib.sha256(x.encode("ascii")).hexdigest()
11841187
# XXX MD5-sess
11851188
else:
11861189
raise ValueError("Unsupported digest authentication "

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ Colm Buckley
258258
Erik de Bueger
259259
Jan-Hein Bührman
260260
Marc Bürg
261+
Calvin Bui
261262
Lars Buitinck
262263
Artem Bulgakov
263264
Dick Bulterman
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Upgrade HTTP digest authentication algorithm for :mod:`urllib.request` by
2+
supporting SHA-256 digest authentication as specified in :rfc:`7616`.

0 commit comments

Comments
 (0)