Skip to content
Myr

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

2 files+64 added−41 removed

modified:requests/auth.py+12 added
151 unmodified lines
152 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_utf8168169 KD = lambda s, d: hash_utf8("%s:%s" % (s, d))170171 if hash_utf8 is None:
134 unmodified lines
modified:tests/test_requests.py+52 added-41 removed
52 unmodified lines
535455class TestRequests:5657 digest_auth_algo = ('MD5', 'SHA-256', 'SHA-512')5859 def test_entry_points(self):6061 requests.session62 requests.session().get
520 unmodified lines
583 requests.sessions.get_netrc_auth = old_auth584585 def test_DIGEST_HTTP_200_OK_GET(self, httpbin):586587 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')590588 r = requests.get(url, auth=auth)589 assert r.status_code == 200591 r = requests.get(url, auth=auth)592 assert r.status_code == 200593591 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'])597594 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 == 200602603 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'604605 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'610611 r = requests.get(url, auth=auth)612 assert r.status_code == 200613614 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'615616 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'622623 def test_DIGEST_STREAM(self, httpbin):624625 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)628620 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''631623 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''634635 def test_DIGESTAUTH_WRONG_HTTP_401_GET(self, httpbin):636637 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)640631 r = requests.get(url, auth=auth)632 assert r.status_code == 401641 r = requests.get(url, auth=auth)642 assert r.status_code == 401643634 r = requests.get(url)635 assert r.status_code == 401644 r = requests.get(url)645 assert r.status_code == 401646637 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 == 401651652 def test_DIGESTAUTH_QUOTES_QOP_VALUE(self, httpbin):653654 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)657647 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']660661 def test_POSTBIN_GET_POST_FILES(self, httpbin):662663 url = httpbin('post')
1796 unmodified lines