Skip to content

Commit 8ecef36

Browse files
committed
remove unused helper functions in atest http server
1 parent 5eee821 commit 8ecef36

File tree

1 file changed

+0
-167
lines changed

1 file changed

+0
-167
lines changed

atests/http_server/helpers.py

Lines changed: 0 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -256,144 +256,6 @@ def status_code(code):
256256
return r
257257

258258

259-
# =============================================================================
260-
# UNUSED: Authentication functions below are no longer used (Flask-HTTPAuth is used instead)
261-
# These functions remain for reference but can be removed in the future
262-
# =============================================================================
263-
264-
def check_basic_auth(user, passwd):
265-
"""Checks user authentication using HTTP Basic Auth.
266-
267-
UNUSED: Replaced by Flask-HTTPAuth's @basic_auth.verify_password decorator
268-
"""
269-
270-
auth = request.authorization
271-
return auth and auth.username == user and auth.password == passwd
272-
273-
274-
# Digest auth helpers
275-
# qop is a quality of protection
276-
277-
278-
def H(data, algorithm):
279-
"""UNUSED: Hash function for digest auth (replaced by Flask-HTTPAuth)"""
280-
if algorithm == 'SHA-256':
281-
return sha256(data).hexdigest()
282-
elif algorithm == 'SHA-512':
283-
return sha512(data).hexdigest()
284-
else:
285-
return md5(data).hexdigest()
286-
287-
288-
def HA1(realm, username, password, algorithm):
289-
"""Create HA1 hash by realm, username, password
290-
291-
HA1 = md5(A1) = MD5(username:realm:password)
292-
293-
UNUSED: Replaced by Flask-HTTPAuth
294-
"""
295-
if not realm:
296-
realm = u''
297-
return H(b":".join([username.encode('utf-8'),
298-
realm.encode('utf-8'),
299-
password.encode('utf-8')]), algorithm)
300-
301-
302-
def HA2(credentials, request, algorithm):
303-
"""Create HA2 md5 hash
304-
305-
If the qop directive's value is "auth" or is unspecified, then HA2:
306-
HA2 = md5(A2) = MD5(method:digestURI)
307-
If the qop directive's value is "auth-int" , then HA2 is
308-
HA2 = md5(A2) = MD5(method:digestURI:MD5(entityBody))
309-
310-
UNUSED: Replaced by Flask-HTTPAuth
311-
"""
312-
if credentials.get("qop") == "auth" or credentials.get('qop') is None:
313-
return H(b":".join([request['method'].encode('utf-8'), request['uri'].encode('utf-8')]), algorithm)
314-
elif credentials.get("qop") == "auth-int":
315-
for k in 'method', 'uri', 'body':
316-
if k not in request:
317-
raise ValueError("%s required" % k)
318-
A2 = b":".join([request['method'].encode('utf-8'),
319-
request['uri'].encode('utf-8'),
320-
H(request['body'], algorithm).encode('utf-8')])
321-
return H(A2, algorithm)
322-
raise ValueError
323-
324-
325-
def response(credentials, password, request):
326-
"""Compile digest auth response
327-
328-
If the qop directive's value is "auth" or "auth-int" , then compute the response as follows:
329-
RESPONSE = MD5(HA1:nonce:nonceCount:clienNonce:qop:HA2)
330-
Else if the qop directive is unspecified, then compute the response as follows:
331-
RESPONSE = MD5(HA1:nonce:HA2)
332-
333-
Arguments:
334-
- `credentials`: credentials dict
335-
- `password`: request user password
336-
- `request`: request dict
337-
338-
UNUSED: Replaced by Flask-HTTPAuth
339-
"""
340-
response = None
341-
algorithm = credentials.get('algorithm')
342-
HA1_value = HA1(
343-
credentials.get('realm'),
344-
credentials.get('username'),
345-
password,
346-
algorithm
347-
)
348-
HA2_value = HA2(credentials, request, algorithm)
349-
if credentials.get('qop') is None:
350-
response = H(b":".join([
351-
HA1_value.encode('utf-8'),
352-
credentials.get('nonce', '').encode('utf-8'),
353-
HA2_value.encode('utf-8')
354-
]), algorithm)
355-
elif credentials.get('qop') == 'auth' or credentials.get('qop') == 'auth-int':
356-
for k in 'nonce', 'nc', 'cnonce', 'qop':
357-
if k not in credentials:
358-
raise ValueError("%s required for response H" % k)
359-
response = H(b":".join([HA1_value.encode('utf-8'),
360-
credentials.get('nonce').encode('utf-8'),
361-
credentials.get('nc').encode('utf-8'),
362-
credentials.get('cnonce').encode('utf-8'),
363-
credentials.get('qop').encode('utf-8'),
364-
HA2_value.encode('utf-8')]), algorithm)
365-
else:
366-
raise ValueError("qop value are wrong")
367-
368-
return response
369-
370-
371-
def check_digest_auth(user, passwd):
372-
"""Check user authentication using HTTP Digest auth
373-
374-
UNUSED: Replaced by Flask-HTTPAuth's @digest_auth.get_password decorator
375-
"""
376-
377-
if request.headers.get('Authorization'):
378-
credentials = Authorization.from_header(request.headers.get('Authorization'))
379-
if not credentials:
380-
return
381-
request_uri = request.script_root + request.path
382-
if request.query_string:
383-
request_uri += '?' + request.query_string
384-
response_hash = response(credentials, passwd, dict(uri=request_uri,
385-
body=request.data,
386-
method=request.method))
387-
if credentials.get('response') == response_hash:
388-
return True
389-
return False
390-
391-
392-
def secure_cookie():
393-
"""Return true if cookie should have secure attribute"""
394-
return request.environ['wsgi.url_scheme'] == 'https'
395-
396-
397259
def __parse_request_range(range_header_text):
398260
""" Return a tuple describing the byte range requested in a GET request
399261
If the range is open ended on the left or right side, then a value of None
@@ -471,32 +333,3 @@ def next_stale_after_value(stale_after):
471333
return str(stal_after_count)
472334
except ValueError:
473335
return 'never'
474-
475-
476-
def digest_challenge_response(app, qop, algorithm, stale=False):
477-
"""Generate digest authentication challenge response.
478-
479-
UNUSED: Replaced by Flask-HTTPAuth which handles challenge generation automatically
480-
"""
481-
response = app.make_response('')
482-
response.status_code = 401
483-
484-
# RFC2616 Section4.2: HTTP headers are ASCII. That means
485-
# request.remote_addr was originally ASCII, so I should be able to
486-
# encode it back to ascii. Also, RFC2617 says about nonces: "The
487-
# contents of the nonce are implementation dependent"
488-
nonce = H(b''.join([
489-
getattr(request, 'remote_addr', u'').encode('ascii'),
490-
b':',
491-
str(time.time()).encode('ascii'),
492-
b':',
493-
os.urandom(10)
494-
]), algorithm)
495-
opaque = H(os.urandom(10), algorithm)
496-
497-
auth = WWWAuthenticate("digest")
498-
auth.set_digest('me@kennethreitz.com', nonce, opaque=opaque,
499-
qop=('auth', 'auth-int') if qop is None else (qop,), algorithm=algorithm)
500-
auth.stale = stale
501-
response.headers['WWW-Authenticate'] = auth.to_header()
502-
return response

0 commit comments

Comments
 (0)