diff options
| author | Eli Collins <elic@assurancetechnologies.com> | 2013-12-20 22:11:07 -0500 |
|---|---|---|
| committer | Eli Collins <elic@assurancetechnologies.com> | 2013-12-20 22:11:07 -0500 |
| commit | 4af9748bcaed1085a8c1e64c2370fa00fb244f11 (patch) | |
| tree | d9ab9cb8cd7aef752bccfb2c9461eef4530eb4ef /passlib/ext | |
| parent | 4c08f92f9caa64140e0010eae88179f260a28704 (diff) | |
| download | passlib-4af9748bcaed1085a8c1e64c2370fa00fb244f11.tar.gz | |
django compatibility updates (should fix issue 50)
passlib.ext.django & it's tests have gotten out of sync with django,
leading to a number of UT failures, as reported in issue 50.
tests now pass on django 1.2 through 1.6
passlib.ext.django
------------------
mimic changes in django's hasher logic:
* handle unsalted_sha1 hasher (django 1.4.6+)
* check_password(): empty hashes return False, rather throw error (django 1.5+
* allow empty passwords (django 1.6+)
* generate unusuable password suffixes (django 1.6+)
passlib.hash
------------
* django_des_crypt: added "use_duplicate_salt" class attr,
allowing tests to enable django 1.4+ style hashes which omit 1st salt.
* django_disabled: added support for django 1.6+ random suffixes
passlib.tests
-------------
* test_ext_django: lots of changes to verify django 1.5/1.6 behavior
* test_handlers_django: split django tests out of test_handlers
to make it easiers to run django-related tests.
* added workaround for encoding glitch in salted_md5 / salted_sha1 hashers (django 1.5+)
Diffstat (limited to 'passlib/ext')
| -rw-r--r-- | passlib/ext/django/models.py | 64 | ||||
| -rw-r--r-- | passlib/ext/django/utils.py | 37 |
2 files changed, 80 insertions, 21 deletions
diff --git a/passlib/ext/django/models.py b/passlib/ext/django/models.py index b4a40da..6c4d245 100644 --- a/passlib/ext/django/models.py +++ b/passlib/ext/django/models.py @@ -72,20 +72,39 @@ def _apply_patch(): from django.contrib.auth.models import UNUSABLE_PASSWORD def is_password_usable(encoded): - return (encoded is not None and encoded != UNUSABLE_PASSWORD) + return encoded is not None and encoded != UNUSABLE_PASSWORD def is_valid_secret(secret): return secret is not None - else: + elif VERSION < (1,6): has_hashers = True from django.contrib.auth.hashers import UNUSABLE_PASSWORD, \ is_password_usable + # NOTE: 1.4 - 1.5 - empty passwords no longer valid. def is_valid_secret(secret): - # NOTE: changed in 1.4 - empty passwords no longer valid. return bool(secret) + else: + has_hashers = True + from django.contrib.auth.hashers import is_password_usable + + # 1.6 - empty passwords valid again + def is_valid_secret(secret): + return secret is not None + + if VERSION < (1,6): + def make_unusable_password(): + return UNUSABLE_PASSWORD + else: + from django.contrib.auth.hashers import make_password as _make_password + def make_unusable_password(): + return _make_password(None) + + # django 1.4.6+ uses a separate hasher for "sha1$$digest" hashes + has_unsalted_sha1 = (VERSION >= (1,4,6)) + # # backport ``User.set_unusable_password()`` for Django 0.9 # (simplifies rest of the code) @@ -95,7 +114,7 @@ def _apply_patch(): @_manager.monkeypatch(USER_PATH) def set_unusable_password(user): - user.password = UNUSABLE_PASSWORD + user.password = make_unusable_password() @_manager.monkeypatch(USER_PATH) def has_usable_password(user): @@ -123,6 +142,8 @@ def _apply_patch(): hash = user.password if not is_valid_secret(password) or not is_password_usable(hash): return False + if not hash and VERSION < (1,4): + return False # NOTE: pulls _get_category from module globals cat = _get_category(user) ok, new_hash = password_context.verify_and_update(password, hash, @@ -159,12 +180,18 @@ def _apply_patch(): def make_password(password, salt=None, hasher="default"): "passlib replacement for make_password()" if not is_valid_secret(password): - return UNUSABLE_PASSWORD - kwds = {} - if salt is not None: + return make_unusable_password() + if hasher == "default": + scheme = None + else: + scheme = hasher_to_passlib_name(hasher) + kwds = dict(scheme=scheme) + handler = password_context.handler(scheme) + # NOTE: django make specify an empty string for the salt, + # even if scheme doesn't accept a salt. we omit keyword + # in that case. + if salt is not None and (salt or 'salt' in handler.setting_kwds): kwds['salt'] = salt - if hasher != "default": - kwds['scheme'] = hasher_to_passlib_name(hasher) return password_context.encrypt(password, **kwds) @_manager.monkeypatch(HASHERS_PATH) @@ -175,18 +202,29 @@ def _apply_patch(): scheme = None else: scheme = hasher_to_passlib_name(algorithm) + # NOTE: resolving scheme -> handler instead of + # passing scheme into get_passlib_hasher(), + # in case context contains custom handler + # shadowing name of a builtin handler. handler = password_context.handler(scheme) - return get_passlib_hasher(handler) + return get_passlib_hasher(handler, algorithm=algorithm) - # NOTE: custom helper that doesn't exist in django proper - # (though submitted a patch - https://code.djangoproject.com/ticket/18184) + # identify_hasher() was added in django 1.5, + # patching it anyways for 1.4, so passlib's version is always available. @_manager.monkeypatch(HASHERS_PATH) @_manager.monkeypatch(FORMS_PATH) def identify_hasher(encoded): "passlib helper to identify hasher from encoded password" handler = password_context.identify(encoded, resolve=True, required=True) - return get_passlib_hasher(handler) + algorithm = None + if (has_unsalted_sha1 and handler.name == "django_salted_sha1" and + encoded.startswith("sha1$$")): + # django 1.4.6+ uses a separate hasher for "sha1$$digest" hashes, + # but passlib just reuses the "sha1$salt$digest" handler. + # we want to resolve to correct django hasher. + algorithm = "unsalted_sha1" + return get_passlib_hasher(handler, algorithm=algorithm) _patched = True log.debug("... finished monkeypatching django") diff --git a/passlib/ext/django/utils.py b/passlib/ext/django/utils.py index 3c03637..ab10b6f 100644 --- a/passlib/ext/django/utils.py +++ b/passlib/ext/django/utils.py @@ -119,6 +119,10 @@ def hasher_to_passlib_name(hasher_name): "convert hasher name -> passlib handler name" if hasher_name.startswith(PASSLIB_HASHER_PREFIX): return hasher_name[len(PASSLIB_HASHER_PREFIX):] + if hasher_name == "unsalted_sha1": + # django 1.4.6+ uses a separate hasher for "sha1$$digest" hashes, + # but passlib just reuses the "sha1$salt$digest" handler. + hasher_name = "sha1" for name in list_crypt_handlers(): if name.startswith(DJANGO_PASSLIB_PREFIX) or name in _other_django_hashes: handler = get_crypt_handler(name) @@ -132,13 +136,14 @@ def hasher_to_passlib_name(hasher_name): #============================================================================= # wrapping passlib handlers as django hashers #============================================================================= -_FAKE_SALT = "--fake-salt--" +_GEN_SALT_SIGNAL = "--!!!generate-new-salt!!!--" class _HasherWrapper(object): """helper for wrapping passlib handlers in Hasher-compatible class.""" # filled in by subclass, drives the other methods. passlib_handler = None + iterations = None @classproperty def algorithm(cls): @@ -146,19 +151,22 @@ class _HasherWrapper(object): return PASSLIB_HASHER_PREFIX + cls.passlib_handler.name def salt(self): - # XXX: our encode wrapper generates a new salt each time it's called, - # so just returning a 'no value' flag here. - return _FAKE_SALT + # NOTE: passlib's handler.encrypt() should generate new salt each time, + # so this just returns a special constant which tells + # encode() (below) not to pass a salt keyword along. + return _GEN_SALT_SIGNAL def verify(self, password, encoded): return self.passlib_handler.verify(password, encoded) def encode(self, password, salt=None, iterations=None): kwds = {} - if salt is not None and salt != _FAKE_SALT: + if salt is not None and salt != _GEN_SALT_SIGNAL: kwds['salt'] = salt if iterations is not None: kwds['rounds'] = iterations + elif self.iterations is not None: + kwds['rounds'] = self.iterations return self.passlib_handler.encrypt(password, **kwds) _translate_kwds = dict(checksum="hash", rounds="iterations") @@ -178,10 +186,17 @@ class _HasherWrapper(object): items.append((_(key), value)) return SortedDict(items) + # added in django 1.6 + def must_update(self, encoded): + # TODO: would like to do something useful here, + # but would require access to password context, + # which would mean a serious recoding of this ext. + return False + # cache of hasher wrappers generated by get_passlib_hasher() _hasher_cache = WeakKeyDictionary() -def get_passlib_hasher(handler): +def get_passlib_hasher(handler, algorithm=None): """create *Hasher*-compatible wrapper for specified passlib hash. This takes in the name of a passlib hash (or the handler object itself), @@ -204,8 +219,14 @@ def get_passlib_hasher(handler): handler = get_crypt_handler(handler) if hasattr(handler, "django_name"): # return native hasher instance - # XXX: should cache this too. - return _get_hasher(handler.django_name) + # XXX: should add this to _hasher_cache[] + name = handler.django_name + if name == "sha1" and algorithm == "unsalted_sha1": + # django 1.4.6+ uses a separate hasher for "sha1$$digest" hashes, + # but passlib just reuses the "sha1$salt$digest" handler. + # we want to resolve to correct django hasher. + name = algorithm + return _get_hasher(name) if handler.name == "django_disabled": raise ValueError("can't wrap unusable-password handler") try: |
