Esempio n. 1
0
    def test_02_from_string(self):
        "test CryptPolicy.from_string() constructor"
        # test "\n" linesep
        policy = CryptPolicy.from_string(self.sample_config_1s)
        self.assertEqual(policy.to_dict(), self.sample_config_1pd)

        # test "\r\n" linesep
        policy = CryptPolicy.from_string(
            self.sample_config_1s.replace("\n", "\r\n"))
        self.assertEqual(policy.to_dict(), self.sample_config_1pd)

        # test with unicode
        data = to_unicode(self.sample_config_1s)
        policy = CryptPolicy.from_string(data)
        self.assertEqual(policy.to_dict(), self.sample_config_1pd)

        # test with non-ascii-compatible encoding
        uc2 = to_bytes(self.sample_config_1s,
                       "utf-16",
                       source_encoding="utf-8")
        policy = CryptPolicy.from_string(uc2, encoding="utf-16")
        self.assertEqual(policy.to_dict(), self.sample_config_1pd)

        # test category specific options
        policy = CryptPolicy.from_string(self.sample_config_4s)
        self.assertEqual(policy.to_dict(), self.sample_config_4pd)
Esempio n. 2
0
    def test_13_get_options(self):
        "test get_options() method"

        p12 = CryptPolicy(**self.sample_config_12pd)

        self.assertEqual(p12.get_options("bsdi_crypt"),dict(
            vary_rounds = "10%",
            min_rounds = 29000,
            max_rounds = 35000,
            default_rounds = 31000,
        ))

        self.assertEqual(p12.get_options("sha512_crypt"),dict(
            vary_rounds = "10%",
            min_rounds = 45000,
            max_rounds = 50000,
        ))

        p4 = CryptPolicy.from_string(self.sample_config_4s)
        self.assertEqual(p4.get_options("sha512_crypt"), dict(
            vary_rounds="10%",
            max_rounds=20000,
        ))

        self.assertEqual(p4.get_options("sha512_crypt", "user"), dict(
            vary_rounds="10%",
            max_rounds=20000,
        ))

        self.assertEqual(p4.get_options("sha512_crypt", "admin"), dict(
            vary_rounds="5%",
            max_rounds=40000,
        ))
    def test_22_to_string(self):
        "test to_string() method"
        pa = CryptPolicy(**self.sample_config_5pd)
        s = pa.to_string() #NOTE: can't compare string directly, ordering etc may not match
        pb = CryptPolicy.from_string(s)
        self.assertEqual(pb.to_dict(), self.sample_config_5pd)

        s = pa.to_string(encoding="latin-1")
        self.assertIsInstance(s, bytes)
Esempio n. 4
0
    def test_22_to_string(self):
        """test to_string() method"""
        pa = CryptPolicy(**self.sample_config_5pd)
        s = pa.to_string() # NOTE: can't compare string directly, ordering etc may not match
        pb = CryptPolicy.from_string(s)
        self.assertEqual(pb.to_dict(), self.sample_config_5pd)

        s = pa.to_string(encoding="latin-1")
        self.assertIsInstance(s, bytes)
    def test_13_get_options(self):
        """test get_options() method"""

        p12 = CryptPolicy(**self.sample_config_12pd)

        self.assertEqual(
            p12.get_options("bsdi_crypt"),
            dict(
                # NOTE: not maintaining backwards compat for rendering to "10%"
                vary_rounds=0.1,
                min_rounds=29000,
                max_rounds=35000,
                default_rounds=31000,
            ),
        )

        self.assertEqual(
            p12.get_options("sha512_crypt"),
            dict(
                # NOTE: not maintaining backwards compat for rendering to "10%"
                vary_rounds=0.1,
                min_rounds=45000,
                max_rounds=50000,
            ),
        )

        p4 = CryptPolicy.from_string(self.sample_config_4s)
        self.assertEqual(
            p4.get_options("sha512_crypt"),
            dict(
                # NOTE: not maintaining backwards compat for rendering to "10%"
                vary_rounds=0.1,
                max_rounds=20000,
            ),
        )

        self.assertEqual(
            p4.get_options("sha512_crypt", "user"),
            dict(
                # NOTE: not maintaining backwards compat for rendering to "10%"
                vary_rounds=0.1,
                max_rounds=20000,
            ),
        )

        self.assertEqual(
            p4.get_options("sha512_crypt", "admin"),
            dict(
                # NOTE: not maintaining backwards compat for rendering to "5%"
                vary_rounds=0.05,
                max_rounds=40000,
            ),
        )
    def test_02_from_string(self):
        "test CryptPolicy.from_string() constructor"
        #test "\n" linesep
        policy = CryptPolicy.from_string(self.sample_config_1s)
        self.assertEqual(policy.to_dict(), self.sample_config_1pd)

        #test "\r\n" linesep
        policy = CryptPolicy.from_string(
            self.sample_config_1s.replace("\n","\r\n"))
        self.assertEqual(policy.to_dict(), self.sample_config_1pd)

        #test with unicode
        data = to_unicode(self.sample_config_1s)
        policy = CryptPolicy.from_string(data)
        self.assertEqual(policy.to_dict(), self.sample_config_1pd)

        #test with non-ascii-compatible encoding
        uc2 = to_bytes(self.sample_config_1s, "utf-16", source_encoding="utf-8")
        policy = CryptPolicy.from_string(uc2, encoding="utf-16")
        self.assertEqual(policy.to_dict(), self.sample_config_1pd)

        #test category specific options
        policy = CryptPolicy.from_string(self.sample_config_4s)
        self.assertEqual(policy.to_dict(), self.sample_config_4pd)
def patch():
    #get config
    ctx = getattr(settings, "PASSLIB_CONTEXT", "passlib-default")
    catfunc = getattr(settings, "PASSLIB_GET_CATEGORY", get_category)

    #parse & validate input value
    if not ctx:
        # remove any patching that was already set, just in case.
        set_django_password_context(None)
        return
    if ctx == "passlib-default":
        ctx = DEFAULT_CTX
    if isinstance(ctx, (unicode, bytes)):
        ctx = CryptPolicy.from_string(ctx)
    if isinstance(ctx, CryptPolicy):
        ctx = CryptContext(policy=ctx)
    if not is_crypt_context(ctx):
        raise TypeError("django settings.PASSLIB_CONTEXT must be CryptContext instance or config string: %r" % (ctx,))

    #monkeypatch django.contrib.auth.models:User
    set_django_password_context(ctx, get_category=catfunc)
    def test_13_get_options(self):
        "test get_options() method"

        p12 = CryptPolicy(**self.sample_config_12pd)

        self.assertEqual(p12.get_options("bsdi_crypt"),dict(
            # NOTE: not maintaining backwards compat for rendering to "10%"
            vary_rounds = 0.1,
            min_rounds = 29000,
            max_rounds = 35000,
            default_rounds = 31000,
        ))

        self.assertEqual(p12.get_options("sha512_crypt"),dict(
            # NOTE: not maintaining backwards compat for rendering to "10%"
            vary_rounds = 0.1,
            min_rounds = 45000,
            max_rounds = 50000,
        ))

        p4 = CryptPolicy.from_string(self.sample_config_4s)
        self.assertEqual(p4.get_options("sha512_crypt"), dict(
            # NOTE: not maintaining backwards compat for rendering to "10%"
            vary_rounds=0.1,
            max_rounds=20000,
        ))

        self.assertEqual(p4.get_options("sha512_crypt", "user"), dict(
            # NOTE: not maintaining backwards compat for rendering to "10%"
            vary_rounds=0.1,
            max_rounds=20000,
        ))

        self.assertEqual(p4.get_options("sha512_crypt", "admin"), dict(
            # NOTE: not maintaining backwards compat for rendering to "5%"
            vary_rounds=0.05,
            max_rounds=40000,
        ))
Esempio n. 9
0
def patch():
    #get config
    ctx = getattr(settings, "PASSLIB_CONTEXT", "passlib-default")
    catfunc = getattr(settings, "PASSLIB_GET_CATEGORY", get_category)

    #parse & validate input value
    if not ctx:
        # remove any patching that was already set, just in case.
        set_django_password_context(None)
        return
    if ctx == "passlib-default":
        ctx = DEFAULT_CTX
    if isinstance(ctx, (unicode, bytes)):
        ctx = CryptPolicy.from_string(ctx)
    if isinstance(ctx, CryptPolicy):
        ctx = CryptContext(policy=ctx)
    if not is_crypt_context(ctx):
        raise TypeError(
            "django settings.PASSLIB_CONTEXT must be CryptContext instance or config string: %r"
            % (ctx, ))

    #monkeypatch django.contrib.auth.models:User
    set_django_password_context(ctx, get_category=catfunc)
Esempio n. 10
0
 def test_22_to_string(self):
     "test to_string() method"
     pa = CryptPolicy(**self.sample_config_5pd)
     s = pa.to_string() #NOTE: can't compare string directly, ordering etc may not match
     pb = CryptPolicy.from_string(s)
     self.assertEqual(pb.to_dict(), self.sample_config_5pd)