PYTHON-795 - Fix password handling for None and the empty string.

This commit is contained in:
Bernie Hackett 2014-12-02 08:45:37 -08:00
parent 807c6797e1
commit 86e85ce715
4 changed files with 12 additions and 9 deletions

View File

@ -52,6 +52,7 @@ MECHANISMS = frozenset(
def _build_credentials_tuple(mech, source, user, passwd, extra):
"""Build and return a mechanism specific credentials tuple.
"""
user = unicode(user)
if mech == 'GSSAPI':
gsn = 'mongodb'
if "gssapiservicename" in extra:
@ -68,7 +69,10 @@ def _build_credentials_tuple(mech, source, user, passwd, extra):
return (mech, '$external', user, gsn)
elif mech == 'MONGODB-X509':
return (mech, '$external', user)
return (mech, source, user, passwd)
else:
if passwd is None:
raise ConfigurationError("A password is required.")
return (mech, source, user, unicode(passwd))
if PY3:
@ -292,7 +296,7 @@ def _auth_key(nonce, username, password):
"""
digest = _password_digest(username, password)
md5hash = _MD5()
data = "%s%s%s" % (nonce, unicode(username), digest)
data = "%s%s%s" % (nonce, username, digest)
md5hash.update(data.encode('utf-8'))
return unicode(md5hash.hexdigest())

View File

@ -902,9 +902,8 @@ class Database(common.BaseObject):
validated_options[normalized] = val
credentials = auth._build_credentials_tuple(mechanism,
source or self.name, unicode(name),
password and unicode(password) or None,
validated_options)
source or self.name, name,
password, validated_options)
self.connection._cache_credentials(self.name, credentials)
return True

View File

@ -385,8 +385,8 @@ class MongoClient(common.BaseObject):
credentials = auth._build_credentials_tuple(mechanism,
source,
unicode(username),
unicode(password),
username,
password,
options)
try:
self._cache_credentials(source, credentials, _connect)

View File

@ -706,8 +706,8 @@ class MongoReplicaSetClient(common.BaseObject):
credentials = auth._build_credentials_tuple(mechanism,
source,
unicode(username),
unicode(password),
username,
password,
options)
try:
self._cache_credentials(source, credentials, _connect)