2014-03-10 05:18:05 +00:00
|
|
|
# This file is part of CherryPy <http://www.cherrypy.org/>
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:ts=4:sw=4:expandtab:fileencoding=utf-8
|
|
|
|
|
|
|
|
__doc__ = """An implementation of the server-side of HTTP Digest Access
|
2014-06-05 01:28:59 +00:00
|
|
|
Authentication, which is described in :rfc:`2617`.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
Example usage, using the built-in get_ha1_dict_plain function which uses a dict
|
2014-06-05 01:28:59 +00:00
|
|
|
of plaintext passwords as the credentials store::
|
|
|
|
|
|
|
|
userpassdict = {'alice' : '4x5istwelve'}
|
|
|
|
get_ha1 = cherrypy.lib.auth_digest.get_ha1_dict_plain(userpassdict)
|
|
|
|
digest_auth = {'tools.auth_digest.on': True,
|
|
|
|
'tools.auth_digest.realm': 'wonderland',
|
|
|
|
'tools.auth_digest.get_ha1': get_ha1,
|
|
|
|
'tools.auth_digest.key': 'a565c27146791cfb',
|
|
|
|
}
|
|
|
|
app_config = { '/' : digest_auth }
|
2014-03-10 05:18:05 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'visteya'
|
|
|
|
__date__ = 'April 2009'
|
|
|
|
|
|
|
|
|
|
|
|
import time
|
2014-06-05 01:28:59 +00:00
|
|
|
from cherrypy._cpcompat import parse_http_list, parse_keqv_list
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
import cherrypy
|
2014-06-05 01:28:59 +00:00
|
|
|
from cherrypy._cpcompat import md5, ntob
|
|
|
|
md5_hex = lambda s: md5(ntob(s)).hexdigest()
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
qop_auth = 'auth'
|
|
|
|
qop_auth_int = 'auth-int'
|
|
|
|
valid_qops = (qop_auth, qop_auth_int)
|
|
|
|
|
|
|
|
valid_algorithms = ('MD5', 'MD5-sess')
|
|
|
|
|
|
|
|
|
|
|
|
def TRACE(msg):
|
|
|
|
cherrypy.log(msg, context='TOOLS.AUTH_DIGEST')
|
|
|
|
|
|
|
|
# Three helper functions for users of the tool, providing three variants
|
|
|
|
# of get_ha1() functions for three different kinds of credential stores.
|
2014-06-05 01:28:59 +00:00
|
|
|
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def get_ha1_dict_plain(user_password_dict):
|
|
|
|
"""Returns a get_ha1 function which obtains a plaintext password from a
|
|
|
|
dictionary of the form: {username : password}.
|
|
|
|
|
|
|
|
If you want a simple dictionary-based authentication scheme, with plaintext
|
|
|
|
passwords, use get_ha1_dict_plain(my_userpass_dict) as the value for the
|
|
|
|
get_ha1 argument to digest_auth().
|
|
|
|
"""
|
|
|
|
def get_ha1(realm, username):
|
|
|
|
password = user_password_dict.get(username)
|
|
|
|
if password:
|
|
|
|
return md5_hex('%s:%s:%s' % (username, realm, password))
|
|
|
|
return None
|
|
|
|
|
|
|
|
return get_ha1
|
|
|
|
|
2014-06-05 01:28:59 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def get_ha1_dict(user_ha1_dict):
|
|
|
|
"""Returns a get_ha1 function which obtains a HA1 password hash from a
|
|
|
|
dictionary of the form: {username : HA1}.
|
|
|
|
|
|
|
|
If you want a dictionary-based authentication scheme, but with
|
|
|
|
pre-computed HA1 hashes instead of plain-text passwords, use
|
|
|
|
get_ha1_dict(my_userha1_dict) as the value for the get_ha1
|
|
|
|
argument to digest_auth().
|
|
|
|
"""
|
|
|
|
def get_ha1(realm, username):
|
2014-06-05 01:28:59 +00:00
|
|
|
return user_ha1_dict.get(username)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
return get_ha1
|
|
|
|
|
2014-06-05 01:28:59 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def get_ha1_file_htdigest(filename):
|
|
|
|
"""Returns a get_ha1 function which obtains a HA1 password hash from a
|
|
|
|
flat file with lines of the same format as that produced by the Apache
|
|
|
|
htdigest utility. For example, for realm 'wonderland', username 'alice',
|
2014-06-05 01:28:59 +00:00
|
|
|
and password '4x5istwelve', the htdigest line would be::
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-06-05 01:28:59 +00:00
|
|
|
alice:wonderland:3238cdfe91a8b2ed8e39646921a02d4c
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
If you want to use an Apache htdigest file as the credentials store,
|
|
|
|
then use get_ha1_file_htdigest(my_htdigest_file) as the value for the
|
|
|
|
get_ha1 argument to digest_auth(). It is recommended that the filename
|
|
|
|
argument be an absolute path, to avoid problems.
|
|
|
|
"""
|
|
|
|
def get_ha1(realm, username):
|
|
|
|
result = None
|
|
|
|
f = open(filename, 'r')
|
|
|
|
for line in f:
|
|
|
|
u, r, ha1 = line.rstrip().split(':')
|
|
|
|
if u == username and r == realm:
|
|
|
|
result = ha1
|
|
|
|
break
|
|
|
|
f.close()
|
|
|
|
return result
|
|
|
|
|
|
|
|
return get_ha1
|
|
|
|
|
|
|
|
|
|
|
|
def synthesize_nonce(s, key, timestamp=None):
|
2014-06-05 01:28:59 +00:00
|
|
|
"""Synthesize a nonce value which resists spoofing and can be checked
|
|
|
|
for staleness. Returns a string suitable as the value for 'nonce' in
|
|
|
|
the www-authenticate header.
|
|
|
|
|
|
|
|
s
|
|
|
|
A string related to the resource, such as the hostname of the server.
|
|
|
|
|
|
|
|
key
|
|
|
|
A secret string known only to the server.
|
|
|
|
|
|
|
|
timestamp
|
|
|
|
An integer seconds-since-the-epoch timestamp
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
if timestamp is None:
|
|
|
|
timestamp = int(time.time())
|
|
|
|
h = md5_hex('%s:%s:%s' % (timestamp, s, key))
|
|
|
|
nonce = '%s:%s' % (timestamp, h)
|
|
|
|
return nonce
|
|
|
|
|
|
|
|
|
|
|
|
def H(s):
|
|
|
|
"""The hash function H"""
|
|
|
|
return md5_hex(s)
|
|
|
|
|
|
|
|
|
|
|
|
class HttpDigestAuthorization (object):
|
2014-06-05 01:28:59 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
"""Class to parse a Digest Authorization header and perform re-calculation
|
|
|
|
of the digest.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def errmsg(self, s):
|
|
|
|
return 'Digest Authorization header: %s' % s
|
|
|
|
|
|
|
|
def __init__(self, auth_header, http_method, debug=False):
|
|
|
|
self.http_method = http_method
|
|
|
|
self.debug = debug
|
|
|
|
scheme, params = auth_header.split(" ", 1)
|
|
|
|
self.scheme = scheme.lower()
|
|
|
|
if self.scheme != 'digest':
|
|
|
|
raise ValueError('Authorization scheme is not "Digest"')
|
|
|
|
|
|
|
|
self.auth_header = auth_header
|
|
|
|
|
|
|
|
# make a dict of the params
|
|
|
|
items = parse_http_list(params)
|
|
|
|
paramsd = parse_keqv_list(items)
|
|
|
|
|
|
|
|
self.realm = paramsd.get('realm')
|
|
|
|
self.username = paramsd.get('username')
|
|
|
|
self.nonce = paramsd.get('nonce')
|
|
|
|
self.uri = paramsd.get('uri')
|
|
|
|
self.method = paramsd.get('method')
|
2014-06-05 01:28:59 +00:00
|
|
|
self.response = paramsd.get('response') # the response digest
|
|
|
|
self.algorithm = paramsd.get('algorithm', 'MD5').upper()
|
2014-03-10 05:18:05 +00:00
|
|
|
self.cnonce = paramsd.get('cnonce')
|
|
|
|
self.opaque = paramsd.get('opaque')
|
2014-06-05 01:28:59 +00:00
|
|
|
self.qop = paramsd.get('qop') # qop
|
|
|
|
self.nc = paramsd.get('nc') # nonce count
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
# perform some correctness checks
|
|
|
|
if self.algorithm not in valid_algorithms:
|
2014-06-05 01:28:59 +00:00
|
|
|
raise ValueError(
|
|
|
|
self.errmsg("Unsupported value for algorithm: '%s'" %
|
|
|
|
self.algorithm))
|
|
|
|
|
|
|
|
has_reqd = (
|
|
|
|
self.username and
|
|
|
|
self.realm and
|
|
|
|
self.nonce and
|
|
|
|
self.uri and
|
|
|
|
self.response
|
|
|
|
)
|
2014-03-10 05:18:05 +00:00
|
|
|
if not has_reqd:
|
2014-06-05 01:28:59 +00:00
|
|
|
raise ValueError(
|
|
|
|
self.errmsg("Not all required parameters are present."))
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
if self.qop:
|
|
|
|
if self.qop not in valid_qops:
|
2014-06-05 01:28:59 +00:00
|
|
|
raise ValueError(
|
|
|
|
self.errmsg("Unsupported value for qop: '%s'" % self.qop))
|
2014-03-10 05:18:05 +00:00
|
|
|
if not (self.cnonce and self.nc):
|
2014-06-05 01:28:59 +00:00
|
|
|
raise ValueError(
|
|
|
|
self.errmsg("If qop is sent then "
|
|
|
|
"cnonce and nc MUST be present"))
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
|
|
|
if self.cnonce or self.nc:
|
2014-06-05 01:28:59 +00:00
|
|
|
raise ValueError(
|
|
|
|
self.errmsg("If qop is not sent, "
|
|
|
|
"neither cnonce nor nc can be present"))
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'authorization : %s' % self.auth_header
|
|
|
|
|
|
|
|
def validate_nonce(self, s, key):
|
|
|
|
"""Validate the nonce.
|
2014-06-05 01:28:59 +00:00
|
|
|
Returns True if nonce was generated by synthesize_nonce() and the
|
|
|
|
timestamp is not spoofed, else returns False.
|
|
|
|
|
|
|
|
s
|
|
|
|
A string related to the resource, such as the hostname of
|
|
|
|
the server.
|
|
|
|
|
|
|
|
key
|
|
|
|
A secret string known only to the server.
|
|
|
|
|
|
|
|
Both s and key must be the same values which were used to synthesize
|
|
|
|
the nonce we are trying to validate.
|
2014-03-10 05:18:05 +00:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
timestamp, hashpart = self.nonce.split(':', 1)
|
2014-06-05 01:28:59 +00:00
|
|
|
s_timestamp, s_hashpart = synthesize_nonce(
|
|
|
|
s, key, timestamp).split(':', 1)
|
2014-03-10 05:18:05 +00:00
|
|
|
is_valid = s_hashpart == hashpart
|
|
|
|
if self.debug:
|
|
|
|
TRACE('validate_nonce: %s' % is_valid)
|
|
|
|
return is_valid
|
2014-06-05 01:28:59 +00:00
|
|
|
except ValueError: # split() error
|
2014-03-10 05:18:05 +00:00
|
|
|
pass
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_nonce_stale(self, max_age_seconds=600):
|
2014-06-05 01:28:59 +00:00
|
|
|
"""Returns True if a validated nonce is stale. The nonce contains a
|
|
|
|
timestamp in plaintext and also a secure hash of the timestamp.
|
|
|
|
You should first validate the nonce to ensure the plaintext
|
|
|
|
timestamp is not spoofed.
|
2014-03-10 05:18:05 +00:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
timestamp, hashpart = self.nonce.split(':', 1)
|
|
|
|
if int(timestamp) + max_age_seconds > int(time.time()):
|
|
|
|
return False
|
2014-06-05 01:28:59 +00:00
|
|
|
except ValueError: # int() error
|
2014-03-10 05:18:05 +00:00
|
|
|
pass
|
|
|
|
if self.debug:
|
|
|
|
TRACE("nonce is stale")
|
|
|
|
return True
|
|
|
|
|
|
|
|
def HA2(self, entity_body=''):
|
2014-06-05 01:28:59 +00:00
|
|
|
"""Returns the H(A2) string. See :rfc:`2617` section 3.2.2.3."""
|
2014-03-10 05:18:05 +00:00
|
|
|
# RFC 2617 3.2.2.3
|
2014-06-05 01:28:59 +00:00
|
|
|
# If the "qop" directive's value is "auth" or is unspecified,
|
|
|
|
# then A2 is:
|
2014-03-10 05:18:05 +00:00
|
|
|
# A2 = method ":" digest-uri-value
|
|
|
|
#
|
|
|
|
# If the "qop" value is "auth-int", then A2 is:
|
|
|
|
# A2 = method ":" digest-uri-value ":" H(entity-body)
|
|
|
|
if self.qop is None or self.qop == "auth":
|
|
|
|
a2 = '%s:%s' % (self.http_method, self.uri)
|
|
|
|
elif self.qop == "auth-int":
|
|
|
|
a2 = "%s:%s:%s" % (self.http_method, self.uri, H(entity_body))
|
|
|
|
else:
|
2014-06-05 01:28:59 +00:00
|
|
|
# in theory, this should never happen, since I validate qop in
|
|
|
|
# __init__()
|
2014-03-10 05:18:05 +00:00
|
|
|
raise ValueError(self.errmsg("Unrecognized value for qop!"))
|
|
|
|
return H(a2)
|
|
|
|
|
|
|
|
def request_digest(self, ha1, entity_body=''):
|
2014-06-05 01:28:59 +00:00
|
|
|
"""Calculates the Request-Digest. See :rfc:`2617` section 3.2.2.1.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-06-05 01:28:59 +00:00
|
|
|
ha1
|
|
|
|
The HA1 string obtained from the credentials store.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-06-05 01:28:59 +00:00
|
|
|
entity_body
|
|
|
|
If 'qop' is set to 'auth-int', then A2 includes a hash
|
2014-03-10 05:18:05 +00:00
|
|
|
of the "entity body". The entity body is the part of the
|
2014-06-05 01:28:59 +00:00
|
|
|
message which follows the HTTP headers. See :rfc:`2617` section
|
|
|
|
4.3. This refers to the entity the user agent sent in the
|
|
|
|
request which has the Authorization header. Typically GET
|
|
|
|
requests don't have an entity, and POST requests do.
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
"""
|
|
|
|
ha2 = self.HA2(entity_body)
|
|
|
|
# Request-Digest -- RFC 2617 3.2.2.1
|
|
|
|
if self.qop:
|
2014-06-05 01:28:59 +00:00
|
|
|
req = "%s:%s:%s:%s:%s" % (
|
|
|
|
self.nonce, self.nc, self.cnonce, self.qop, ha2)
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
|
|
|
req = "%s:%s" % (self.nonce, ha2)
|
|
|
|
|
|
|
|
# RFC 2617 3.2.2.2
|
|
|
|
#
|
2014-06-05 01:28:59 +00:00
|
|
|
# If the "algorithm" directive's value is "MD5" or is unspecified,
|
|
|
|
# then A1 is:
|
|
|
|
# A1 = unq(username-value) ":" unq(realm-value) ":" passwd
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
|
|
|
# If the "algorithm" directive's value is "MD5-sess", then A1 is
|
|
|
|
# calculated only once - on the first request by the client following
|
|
|
|
# receipt of a WWW-Authenticate challenge from the server.
|
|
|
|
# A1 = H( unq(username-value) ":" unq(realm-value) ":" passwd )
|
|
|
|
# ":" unq(nonce-value) ":" unq(cnonce-value)
|
|
|
|
if self.algorithm == 'MD5-sess':
|
|
|
|
ha1 = H('%s:%s:%s' % (ha1, self.nonce, self.cnonce))
|
|
|
|
|
|
|
|
digest = H('%s:%s' % (ha1, req))
|
|
|
|
return digest
|
|
|
|
|
|
|
|
|
2014-06-05 01:28:59 +00:00
|
|
|
def www_authenticate(realm, key, algorithm='MD5', nonce=None, qop=qop_auth,
|
|
|
|
stale=False):
|
2014-03-10 05:18:05 +00:00
|
|
|
"""Constructs a WWW-Authenticate header for Digest authentication."""
|
|
|
|
if qop not in valid_qops:
|
|
|
|
raise ValueError("Unsupported value for qop: '%s'" % qop)
|
|
|
|
if algorithm not in valid_algorithms:
|
|
|
|
raise ValueError("Unsupported value for algorithm: '%s'" % algorithm)
|
|
|
|
|
|
|
|
if nonce is None:
|
|
|
|
nonce = synthesize_nonce(realm, key)
|
|
|
|
s = 'Digest realm="%s", nonce="%s", algorithm="%s", qop="%s"' % (
|
2014-06-05 01:28:59 +00:00
|
|
|
realm, nonce, algorithm, qop)
|
2014-03-10 05:18:05 +00:00
|
|
|
if stale:
|
|
|
|
s += ', stale="true"'
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
def digest_auth(realm, get_ha1, key, debug=False):
|
2014-06-05 01:28:59 +00:00
|
|
|
"""A CherryPy tool which hooks at before_handler to perform
|
|
|
|
HTTP Digest Access Authentication, as specified in :rfc:`2617`.
|
|
|
|
|
|
|
|
If the request has an 'authorization' header with a 'Digest' scheme,
|
|
|
|
this tool authenticates the credentials supplied in that header.
|
|
|
|
If the request has no 'authorization' header, or if it does but the
|
|
|
|
scheme is not "Digest", or if authentication fails, the tool sends
|
|
|
|
a 401 response with a 'WWW-Authenticate' Digest header.
|
|
|
|
|
|
|
|
realm
|
|
|
|
A string containing the authentication realm.
|
|
|
|
|
|
|
|
get_ha1
|
|
|
|
A callable which looks up a username in a credentials store
|
2014-03-10 05:18:05 +00:00
|
|
|
and returns the HA1 string, which is defined in the RFC to be
|
|
|
|
MD5(username : realm : password). The function's signature is:
|
2014-06-05 01:28:59 +00:00
|
|
|
``get_ha1(realm, username)``
|
2014-03-10 05:18:05 +00:00
|
|
|
where username is obtained from the request's 'authorization' header.
|
|
|
|
If username is not found in the credentials store, get_ha1() returns
|
|
|
|
None.
|
2014-06-05 01:28:59 +00:00
|
|
|
|
|
|
|
key
|
|
|
|
A secret string known only to the server, used in the synthesis
|
|
|
|
of nonces.
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
"""
|
|
|
|
request = cherrypy.serving.request
|
2014-06-05 01:28:59 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
auth_header = request.headers.get('authorization')
|
|
|
|
nonce_is_stale = False
|
|
|
|
if auth_header is not None:
|
|
|
|
try:
|
2014-06-05 01:28:59 +00:00
|
|
|
auth = HttpDigestAuthorization(
|
|
|
|
auth_header, request.method, debug=debug)
|
|
|
|
except ValueError:
|
|
|
|
raise cherrypy.HTTPError(
|
|
|
|
400, "The Authorization header could not be parsed.")
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if debug:
|
|
|
|
TRACE(str(auth))
|
2014-06-05 01:28:59 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if auth.validate_nonce(realm, key):
|
|
|
|
ha1 = get_ha1(realm, auth.username)
|
|
|
|
if ha1 is not None:
|
2014-06-05 01:28:59 +00:00
|
|
|
# note that for request.body to be available we need to
|
|
|
|
# hook in at before_handler, not on_start_resource like
|
|
|
|
# 3.1.x digest_auth does.
|
2014-03-10 05:18:05 +00:00
|
|
|
digest = auth.request_digest(ha1, entity_body=request.body)
|
2014-06-05 01:28:59 +00:00
|
|
|
if digest == auth.response: # authenticated
|
2014-03-10 05:18:05 +00:00
|
|
|
if debug:
|
|
|
|
TRACE("digest matches auth.response")
|
|
|
|
# Now check if nonce is stale.
|
2014-06-05 01:28:59 +00:00
|
|
|
# The choice of ten minutes' lifetime for nonce is somewhat
|
|
|
|
# arbitrary
|
2014-03-10 05:18:05 +00:00
|
|
|
nonce_is_stale = auth.is_nonce_stale(max_age_seconds=600)
|
|
|
|
if not nonce_is_stale:
|
|
|
|
request.login = auth.username
|
|
|
|
if debug:
|
2014-06-05 01:28:59 +00:00
|
|
|
TRACE("authentication of %s successful" %
|
|
|
|
auth.username)
|
2014-03-10 05:18:05 +00:00
|
|
|
return
|
2014-06-05 01:28:59 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
# Respond with 401 status and a WWW-Authenticate header
|
|
|
|
header = www_authenticate(realm, key, stale=nonce_is_stale)
|
|
|
|
if debug:
|
|
|
|
TRACE(header)
|
|
|
|
cherrypy.serving.response.headers['WWW-Authenticate'] = header
|
2014-06-05 01:28:59 +00:00
|
|
|
raise cherrypy.HTTPError(
|
|
|
|
401, "You are not authorized to access that resource")
|