summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2012-01-18 17:55:04 -0500
committerEli Collins <elic@assurancetechnologies.com>2012-01-18 17:55:04 -0500
commitc1927edb87df4f22c5d5471e88f42b085a1a946a (patch)
tree480a60dcad9e1dad485306c250b3e6bf292fcdf8
parent666aa14bf15ed898472463d2ec890de0b8c60923 (diff)
downloadpasslib-c1927edb87df4f22c5d5471e88f42b085a1a946a.tar.gz
StaticHandler._norm_hash() should return native str type
-rw-r--r--passlib/handlers/digests.py4
-rw-r--r--passlib/handlers/ldap_digests.py12
-rw-r--r--passlib/handlers/misc.py19
-rw-r--r--passlib/handlers/mysql.py8
-rw-r--r--passlib/handlers/oracle.py4
-rw-r--r--passlib/utils/handlers.py14
6 files changed, 23 insertions, 38 deletions
diff --git a/passlib/handlers/digests.py b/passlib/handlers/digests.py
index 34e1dd3..45c978f 100644
--- a/passlib/handlers/digests.py
+++ b/passlib/handlers/digests.py
@@ -57,9 +57,7 @@ class HexDigestHash(uh.StaticHandler):
@classmethod
def _norm_hash(cls, hash):
- if isinstance(hash, bytes):
- hash = hash.decode("ascii")
- return hash.lower()
+ return to_native_str(hash, "ascii", errname="hash").lower()
def create_hex_hash(hash, digest_name):
#NOTE: could set digest_name=hash.name for cpython, but not for some other platforms.
diff --git a/passlib/handlers/ldap_digests.py b/passlib/handlers/ldap_digests.py
index 8393be5..c25afd5 100644
--- a/passlib/handlers/ldap_digests.py
+++ b/passlib/handlers/ldap_digests.py
@@ -195,19 +195,11 @@ class ldap_plaintext(uh.StaticHandler):
def genhash(cls, secret, hash):
if hash is not None and not cls.identify(hash):
raise ValueError("not a valid ldap_plaintext hash")
- if secret is None:
- raise TypeError("secret must be string")
- return to_native_str(secret, "utf-8")
+ return to_native_str(secret, "utf-8", errname="secret")
@classmethod
def _norm_hash(cls, hash):
- if isinstance(hash, bytes):
- #XXX: current code uses utf-8
- # if existing hashes use something else,
- # probably have to modify this code to allow hash_encoding
- # to be specified as an option.
- hash = hash.decode("utf-8")
- return hash
+ return to_native_str(hash, "utf-8", errname="hash")
#=========================================================
#{CRYPT} wrappers
diff --git a/passlib/handlers/misc.py b/passlib/handlers/misc.py
index 7233768..06d9400 100644
--- a/passlib/handlers/misc.py
+++ b/passlib/handlers/misc.py
@@ -65,6 +65,8 @@ class plaintext(uh.StaticHandler):
"""This class stores passwords in plaintext, and follows the :ref:`password-hash-api`.
Unicode passwords will be encoded using utf-8.
+
+ Under Python 3, existing 'hashes' must decode as utf-8.
"""
name = "plaintext"
@@ -72,21 +74,18 @@ class plaintext(uh.StaticHandler):
def identify(cls, hash):
return hash is not None
+ # NOTE: this tries to avoid decoding bytes under py2,
+ # for applications that are using latin-1 or some other encoding.
+ # they'll just have to stop using plaintext under py3 :)
+ # (or re-encode as utf-8)
+
@classmethod
def genhash(cls, secret, hash):
- if secret is None:
- raise TypeError("secret must be string")
- return to_native_str(secret, "utf-8")
+ return to_native_str(secret, "utf-8", errname="secret")
@classmethod
def _norm_hash(cls, hash):
- if isinstance(hash, bytes):
- #XXX: current code uses utf-8
- # if existing hashes use something else,
- # probably have to modify this code to allow hash_encoding
- # to be specified as an option.
- hash = hash.decode("utf-8")
- return hash
+ return to_native_str(hash, "utf-8", errname="hash")
#=========================================================
#eof
diff --git a/passlib/handlers/mysql.py b/passlib/handlers/mysql.py
index f8d9cf1..3b6d951 100644
--- a/passlib/handlers/mysql.py
+++ b/passlib/handlers/mysql.py
@@ -91,9 +91,7 @@ class mysql323(uh.StaticHandler):
@classmethod
def _norm_hash(cls, hash):
- if isinstance(hash, bytes):
- hash = hash.decode("ascii")
- return hash.lower()
+ return to_native_str(hash, "ascii", errname="hash").lower()
#=========================================================
#eoc
@@ -135,9 +133,7 @@ class mysql41(uh.StaticHandler):
@classmethod
def _norm_hash(cls, hash):
- if isinstance(hash, bytes):
- hash = hash.decode("ascii")
- return hash.upper()
+ return to_native_str(hash, "ascii", errname="hash").upper()
#=========================================================
#eoc
diff --git a/passlib/handlers/oracle.py b/passlib/handlers/oracle.py
index e0c6ff2..ac33bc3 100644
--- a/passlib/handlers/oracle.py
+++ b/passlib/handlers/oracle.py
@@ -119,9 +119,7 @@ class oracle10(uh.StaticHandler):
@classmethod
def _norm_hash(cls, hash):
- if isinstance(hash, bytes):
- hash = hash.decode("ascii")
- return hash.upper()
+ return to_native_str(hash, "ascii", errname="hash").upper()
#=========================================================
#eoc
diff --git a/passlib/utils/handlers.py b/passlib/utils/handlers.py
index 7643c01..8b664b4 100644
--- a/passlib/utils/handlers.py
+++ b/passlib/utils/handlers.py
@@ -198,6 +198,8 @@ class StaticHandler(object):
setting_kwds = ()
context_kwds = ()
+ # reserved value to be returned by default genconfig()
+ # may be ``None`` if no such value; otherwise should be native ascii str.
_stub_config = None
#=====================================================
@@ -238,15 +240,15 @@ class StaticHandler(object):
raise ValueError("expected %s hash, got %s config string instead" %
(cls.name, cls.name))
result = cls.genhash(secret, hash, *cargs, **context)
- return consteq(cls._norm_hash(result), hash)
+ return consteq(result, hash)
@classmethod
def _norm_hash(cls, hash):
- """[helper for verify] normalize hash for comparsion purposes"""
- #NOTE: this is mainly provided for case-insenstive subclasses to override.
- if isinstance(hash, bytes):
- hash = hash.decode("ascii")
- return hash
+ """[helper for verify] normalize hash for comparsion purposes.
+
+ should return a native :class:`str` instance or raise a TypeError.
+ """
+ return to_native_str(hash, "ascii", errname="hash")
#=====================================================
#eoc