mirror of
https://github.com/kennethreitz/httpbin.git
synced 2026-06-05 06:46:16 +00:00
add SHA-512 authentication
This commit is contained in:
+6
-4
@@ -7,7 +7,7 @@ import contextlib
|
||||
import six
|
||||
import json
|
||||
from werkzeug.http import parse_dict_header
|
||||
from hashlib import md5, sha256
|
||||
from hashlib import md5, sha256, sha512
|
||||
from six import BytesIO
|
||||
|
||||
import httpbin
|
||||
@@ -41,6 +41,8 @@ def _hash(data, algorithm):
|
||||
"""Encode binary data according to specified algorithm, use MD5 by default"""
|
||||
if algorithm == 'SHA-256':
|
||||
return sha256(data).hexdigest()
|
||||
elif algorithm == 'SHA-512':
|
||||
return sha512(data).hexdigest()
|
||||
else:
|
||||
return md5(data).hexdigest()
|
||||
|
||||
@@ -65,7 +67,7 @@ def _make_digest_auth_header(username, password, method, uri, nonce,
|
||||
assert nonce
|
||||
assert method
|
||||
assert uri
|
||||
assert algorithm in ('MD5', 'SHA-256', None)
|
||||
assert algorithm in ('MD5', 'SHA-256', 'SHA-512', None)
|
||||
|
||||
a1 = ':'.join([username, realm or '', password])
|
||||
ha1 = _hash(a1.encode('utf-8'), algorithm)
|
||||
@@ -282,7 +284,7 @@ class HttpbinTestCase(unittest.TestCase):
|
||||
username = 'user'
|
||||
password = 'passwd'
|
||||
for qop in None, 'auth', 'auth-int',:
|
||||
for algorithm in None, 'MD5', 'SHA-256':
|
||||
for algorithm in None, 'MD5', 'SHA-256', 'SHA-512':
|
||||
for body in None, b'', b'request payload':
|
||||
for stale_after in (None, 1, 4) if algorithm else (None,) :
|
||||
self._test_digest_auth(username, password, qop, algorithm, body, stale_after)
|
||||
@@ -371,7 +373,7 @@ class HttpbinTestCase(unittest.TestCase):
|
||||
username = 'user'
|
||||
password = 'passwd'
|
||||
for qop in None, 'auth', 'auth-int',:
|
||||
for algorithm in None, 'MD5', 'SHA-256':
|
||||
for algorithm in None, 'MD5', 'SHA-256', 'SHA-512':
|
||||
for body in None, b'', b'request payload':
|
||||
self._test_digest_auth_wrong_pass(username, password, qop, algorithm, body, 3)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user