]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128192: support HTTP sha-256 digest authentication as per RFC-7617 (GH-128193)
authorCalvin Bui <3604363+calvinbui@users.noreply.github.com>
Sat, 28 Dec 2024 21:05:34 +0000 (08:05 +1100)
committerGitHub <noreply@github.com>
Sat, 28 Dec 2024 21:05:34 +0000 (21:05 +0000)
support sha-256 digest authentication

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Doc/library/urllib.request.rst
Doc/whatsnew/3.14.rst
Lib/test/test_urllib2.py
Lib/urllib/request.py
Misc/ACKS
Misc/NEWS.d/next/Core_and_Builtins/2024-12-23-11-14-07.gh-issue-128192.02mEhD.rst [new file with mode: 0644]

index 3c07dc4adf434a5219a3400814a159221023f453..b3efde3f189566dc5d34b02f76e76710d6a5bbd4 100644 (file)
@@ -411,6 +411,9 @@ The following classes are provided:
    :ref:`http-password-mgr` for information on the interface that must be
    supported.
 
+   .. versionchanged:: 3.14
+      Added support for HTTP digest authentication algorithm ``SHA-256``.
+
 
 .. class:: HTTPDigestAuthHandler(password_mgr=None)
 
index 935c61c474e889a783a15f41b8baaed22c96fa91..2767fd3ca48b29121e4122a9515995a9711c273f 100644 (file)
@@ -646,6 +646,14 @@ unittest
   (Contributed by Jacob Walls in :gh:`80958`.)
 
 
+urllib
+------
+
+* Upgrade HTTP digest authentication algorithm for :mod:`urllib.request` by
+  supporting SHA-256 digest authentication as specified in :rfc:`7616`.
+  (Contributed by Calvin Bui in :gh:`128193`.)
+
+
 uuid
 ----
 
index 4a9e653515be5b2c9fc3a50c184eb95bead4f0da..96d91c1f1c2f8a0007a4976c1891ab4cc46606ce 100644 (file)
@@ -1962,10 +1962,29 @@ class MiscTests(unittest.TestCase):
 
         self.assertRaises(ValueError, _parse_proxy, 'file:/ftp.example.com'),
 
-    def test_unsupported_algorithm(self):
-        handler = AbstractDigestAuthHandler()
+
+class TestDigestAlgorithms(unittest.TestCase):
+    def setUp(self):
+        self.handler = AbstractDigestAuthHandler()
+
+    def test_md5_algorithm(self):
+        H, KD = self.handler.get_algorithm_impls('MD5')
+        self.assertEqual(H("foo"), "acbd18db4cc2f85cedef654fccc4a4d8")
+        self.assertEqual(KD("foo", "bar"), "4e99e8c12de7e01535248d2bac85e732")
+
+    def test_sha_algorithm(self):
+        H, KD = self.handler.get_algorithm_impls('SHA')
+        self.assertEqual(H("foo"), "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
+        self.assertEqual(KD("foo", "bar"), "54dcbe67d21d5eb39493d46d89ae1f412d3bd6de")
+
+    def test_sha256_algorithm(self):
+        H, KD = self.handler.get_algorithm_impls('SHA-256')
+        self.assertEqual(H("foo"), "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae")
+        self.assertEqual(KD("foo", "bar"), "a765a8beaa9d561d4c5cbed29d8f4e30870297fdfa9cb7d6e9848a95fec9f937")
+
+    def test_invalid_algorithm(self):
         with self.assertRaises(ValueError) as exc:
-            handler.get_algorithm_impls('invalid')
+            self.handler.get_algorithm_impls('invalid')
         self.assertEqual(
             str(exc.exception),
             "Unsupported digest authentication algorithm 'invalid'"
index c5a6a18a32bba10f10f536be2e43c831d3e04c93..0d1b594b8cf20b1ec980ff88cef26e94a1ba4cc5 100644 (file)
@@ -1048,7 +1048,7 @@ _randombytes = os.urandom
 
 
 class AbstractDigestAuthHandler:
-    # Digest authentication is specified in RFC 2617.
+    # Digest authentication is specified in RFC 2617/7616.
 
     # XXX The client does not inspect the Authentication-Info header
     # in a successful response.
@@ -1176,11 +1176,14 @@ class AbstractDigestAuthHandler:
         return base
 
     def get_algorithm_impls(self, algorithm):
+        # algorithm names taken from RFC 7616 Section 6.1
         # lambdas assume digest modules are imported at the top level
         if algorithm == 'MD5':
             H = lambda x: hashlib.md5(x.encode("ascii")).hexdigest()
-        elif algorithm == 'SHA':
+        elif algorithm == 'SHA':  # non-standard, retained for compatibility.
             H = lambda x: hashlib.sha1(x.encode("ascii")).hexdigest()
+        elif algorithm == 'SHA-256':
+            H = lambda x: hashlib.sha256(x.encode("ascii")).hexdigest()
         # XXX MD5-sess
         else:
             raise ValueError("Unsupported digest authentication "
index 086930666822ad75783a7113d88f16f7d6b00b69..c6e53317b37d786ea966797bcdd6e1b3cae76e7c 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -258,6 +258,7 @@ Colm Buckley
 Erik de Bueger
 Jan-Hein Bührman
 Marc Bürg
+Calvin Bui
 Lars Buitinck
 Artem Bulgakov
 Dick Bulterman
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-12-23-11-14-07.gh-issue-128192.02mEhD.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-23-11-14-07.gh-issue-128192.02mEhD.rst
new file mode 100644 (file)
index 0000000..b80ab71
--- /dev/null
@@ -0,0 +1,2 @@
+Upgrade HTTP digest authentication algorithm for :mod:`urllib.request` by
+supporting SHA-256 digest authentication as specified in :rfc:`7616`.