Пример #1
0
    def test_02_handler_wrapper(self):
        """test Hasher-compatible handler wrappers"""
        from passlib.ext.django.utils import get_passlib_hasher
        from django.contrib.auth import hashers

        # should return native django hasher if available
        hasher = get_passlib_hasher("hex_md5")
        self.assertIsInstance(hasher, hashers.UnsaltedMD5PasswordHasher)

        hasher = get_passlib_hasher("django_bcrypt")
        self.assertIsInstance(hasher, hashers.BCryptPasswordHasher)

        # otherwise should return wrapper
        from passlib.hash import sha256_crypt
        hasher = get_passlib_hasher("sha256_crypt")
        self.assertEqual(hasher.algorithm, "passlib_sha256_crypt")

        # and wrapper should return correct hash
        encoded = hasher.encode("stub")
        self.assertTrue(sha256_crypt.verify("stub", encoded))
        self.assertTrue(hasher.verify("stub", encoded))
        self.assertFalse(hasher.verify("xxxx", encoded))

        # test wrapper accepts options
        encoded = hasher.encode("stub", "abcd"*4, iterations=1234)
        self.assertEqual(encoded, "$5$rounds=1234$abcdabcdabcdabcd$"
                                  "v2RWkZQzctPdejyRqmmTDQpZN6wTh7.RUy9zF2LftT6")
        self.assertEqual(hasher.safe_summary(encoded),
            {'algorithm': 'sha256_crypt',
             'salt': u('abcdab**********'),
             'iterations': 1234,
             'hash': u('v2RWkZ*************************************'),
             })
Пример #2
0
 def get_hasher(algorithm="default"):
     "passlib replacement for get_hasher()"
     if algorithm == "default":
         scheme = None
     else:
         scheme = hasher_to_passlib_name(algorithm)
     handler = password_context.handler(scheme)
     return get_passlib_hasher(handler)
Пример #3
0
 def get_hasher(algorithm="default"):
     "passlib replacement for get_hasher()"
     if algorithm == "default":
         scheme = None
     else:
         scheme = hasher_to_passlib_name(algorithm)
     handler = password_context.handler(scheme)
     return get_passlib_hasher(handler)
Пример #4
0
 def identify_hasher(encoded):
     """passlib helper to identify hasher from encoded password"""
     handler = password_context.identify(encoded, resolve=True, required=True)
     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)
Пример #5
0
 def get_hasher(algorithm="default"):
     """passlib replacement for get_hasher()"""
     if algorithm == "default":
         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, algorithm=algorithm)
Пример #6
0
 def get_hasher(algorithm="default"):
     """passlib replacement for get_hasher()"""
     if algorithm == "default":
         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, algorithm=algorithm)
Пример #7
0
 def identify_hasher(encoded):
     """passlib helper to identify hasher from encoded password"""
     handler = password_context.identify(encoded,
                                         resolve=True,
                                         required=True)
     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)
Пример #8
0
    def test_02_handler_wrapper(self):
        "test Hasher-compatible handler wrappers"
        if not has_django14:
            raise self.skipTest("Django >= 1.4 not installed")
        from passlib.ext.django.utils import get_passlib_hasher
        from django.contrib.auth import hashers

        # should return native django hasher if available
        hasher = get_passlib_hasher("hex_md5")
        self.assertIsInstance(hasher, hashers.UnsaltedMD5PasswordHasher)

        hasher = get_passlib_hasher("django_bcrypt")
        self.assertIsInstance(hasher, hashers.BCryptPasswordHasher)

        # otherwise should return wrapper
        from passlib.hash import sha256_crypt

        hasher = get_passlib_hasher("sha256_crypt")
        self.assertEqual(hasher.algorithm, "passlib_sha256_crypt")

        # and wrapper should return correct hash
        encoded = hasher.encode("stub")
        self.assertTrue(sha256_crypt.verify("stub", encoded))
        self.assertTrue(hasher.verify("stub", encoded))
        self.assertFalse(hasher.verify("xxxx", encoded))

        # test wrapper accepts options
        encoded = hasher.encode("stub", "abcd" * 4, iterations=1234)
        self.assertEqual(encoded, "$5$rounds=1234$abcdabcdabcdabcd$" "v2RWkZQzctPdejyRqmmTDQpZN6wTh7.RUy9zF2LftT6")
        self.assertEqual(
            hasher.safe_summary(encoded),
            {
                "algorithm": "sha256_crypt",
                "salt": u("abcdab**********"),
                "iterations": 1234,
                "hash": u("v2RWkZ*************************************"),
            },
        )
Пример #9
0
 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)
Пример #10
0
 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)