Merge pull request #4410 from linuxlizard/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add SHA-256 and SHA-512 digest auth algorithms
authored by Ian Stapleton Cordasco · committed by GitHub · parents 80e2637, 371ee7c · Browse files · 2 files·+64 added−41 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+64 added−41 removed
- tests:+52 added−41 removed
- other:+12 added−0 removed
Provenance
- Commit
161acb6- Tree
36db33b- Author
Ian Stapleton Cordasco <graffatcolmingov@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
80e2637- Parent 2
371ee7c
2 files changed+64 added−41 removed
modified:requests/auth.py+12 added
151 unmodified lines152 if isinstance(x, str):153 x = x.encode('utf-8')154 return hashlib.sha1(x).hexdigest()155 hash_utf8 = sha_utf8156+ elif _algorithm == 'SHA-256':157+ def sha256_utf8(x):158+ if isinstance(x, str):159+ x = x.encode('utf-8')160+ return hashlib.sha256(x).hexdigest()161+ hash_utf8 = sha256_utf8162+ elif _algorithm == 'SHA-512':163+ def sha512_utf8(x):164+ if isinstance(x, str):165+ x = x.encode('utf-8')166+ return hashlib.sha512(x).hexdigest()167+ hash_utf8 = sha512_utf8168 169 KD = lambda s, d: hash_utf8("%s:%s" % (s, d))170 171 if hash_utf8 is None:134 unmodified linesmodified:tests/test_requests.py+52 added-41 removed
52 unmodified lines53 54 55 class TestRequests:56 57+ digest_auth_algo = ('MD5', 'SHA-256', 'SHA-512')58+59 def test_entry_points(self):60 61 requests.session62 requests.session().get520 unmodified lines583 requests.sessions.get_netrc_auth = old_auth584 585 def test_DIGEST_HTTP_200_OK_GET(self, httpbin):586 587+ for authtype in self.digest_auth_algo:585- auth = HTTPDigestAuth('user', 'pass')586- url = httpbin('digest-auth', 'auth', 'user', 'pass')588+ auth = HTTPDigestAuth('user', 'pass')589+ url = httpbin('digest-auth', 'auth', 'user', 'pass', authtype, 'never')590 588- r = requests.get(url, auth=auth)589- assert r.status_code == 200591+ r = requests.get(url, auth=auth)592+ assert r.status_code == 200593 591- r = requests.get(url)592- assert r.status_code == 401594+ r = requests.get(url)595+ assert r.status_code == 401596+ print(r.headers['WWW-Authenticate'])597 594- s = requests.session()595- s.auth = HTTPDigestAuth('user', 'pass')596- r = s.get(url)597- assert r.status_code == 200598+ s = requests.session()599+ s.auth = HTTPDigestAuth('user', 'pass')600+ r = s.get(url)601+ assert r.status_code == 200602 603 def test_DIGEST_AUTH_RETURNS_COOKIE(self, httpbin):600- url = httpbin('digest-auth', 'auth', 'user', 'pass')601- auth = HTTPDigestAuth('user', 'pass')602- r = requests.get(url)603- assert r.cookies['fake'] == 'fake_value'604 605- r = requests.get(url, auth=auth)606- assert r.status_code == 200605+ for authtype in self.digest_auth_algo:606+ url = httpbin('digest-auth', 'auth', 'user', 'pass', authtype)607+ auth = HTTPDigestAuth('user', 'pass')608+ r = requests.get(url)609+ assert r.cookies['fake'] == 'fake_value'610 611+ r = requests.get(url, auth=auth)612+ assert r.status_code == 200613+614 def test_DIGEST_AUTH_SETS_SESSION_COOKIES(self, httpbin):609- url = httpbin('digest-auth', 'auth', 'user', 'pass')610- auth = HTTPDigestAuth('user', 'pass')611- s = requests.Session()612- s.get(url, auth=auth)613- assert s.cookies['fake'] == 'fake_value'615 616+ for authtype in self.digest_auth_algo:617+ url = httpbin('digest-auth', 'auth', 'user', 'pass', authtype)618+ auth = HTTPDigestAuth('user', 'pass')619+ s = requests.Session()620+ s.get(url, auth=auth)621+ assert s.cookies['fake'] == 'fake_value'622+623 def test_DIGEST_STREAM(self, httpbin):624 625+ for authtype in self.digest_auth_algo:617- auth = HTTPDigestAuth('user', 'pass')618- url = httpbin('digest-auth', 'auth', 'user', 'pass')626+ auth = HTTPDigestAuth('user', 'pass')627+ url = httpbin('digest-auth', 'auth', 'user', 'pass', authtype)628 620- r = requests.get(url, auth=auth, stream=True)621- assert r.raw.read() != b''629+ r = requests.get(url, auth=auth, stream=True)630+ assert r.raw.read() != b''631 623- r = requests.get(url, auth=auth, stream=False)624- assert r.raw.read() == b''632+ r = requests.get(url, auth=auth, stream=False)633+ assert r.raw.read() == b''634 635 def test_DIGESTAUTH_WRONG_HTTP_401_GET(self, httpbin):636 637+ for authtype in self.digest_auth_algo:628- auth = HTTPDigestAuth('user', 'wrongpass')629- url = httpbin('digest-auth', 'auth', 'user', 'pass')638+ auth = HTTPDigestAuth('user', 'wrongpass')639+ url = httpbin('digest-auth', 'auth', 'user', 'pass', authtype)640 631- r = requests.get(url, auth=auth)632- assert r.status_code == 401641+ r = requests.get(url, auth=auth)642+ assert r.status_code == 401643 634- r = requests.get(url)635- assert r.status_code == 401644+ r = requests.get(url)645+ assert r.status_code == 401646 637- s = requests.session()638- s.auth = auth639- r = s.get(url)640- assert r.status_code == 401647+ s = requests.session()648+ s.auth = auth649+ r = s.get(url)650+ assert r.status_code == 401651 652 def test_DIGESTAUTH_QUOTES_QOP_VALUE(self, httpbin):653 654+ for authtype in self.digest_auth_algo:644- auth = HTTPDigestAuth('user', 'pass')645- url = httpbin('digest-auth', 'auth', 'user', 'pass')655+ auth = HTTPDigestAuth('user', 'pass')656+ url = httpbin('digest-auth', 'auth', 'user', 'pass', authtype)657 647- r = requests.get(url, auth=auth)648- assert '"auth"' in r.request.headers['Authorization']658+ r = requests.get(url, auth=auth)659+ assert '"auth"' in r.request.headers['Authorization']660 661 def test_POSTBIN_GET_POST_FILES(self, httpbin):662 663 url = httpbin('post')1796 unmodified lines