Ejemplo n.º 1
0
    def test_get_crypt_handler(self):
        """test get_crypt_handler()"""
        class dummy_1(uh.StaticHandler):
            name = "dummy_1"

        # without available handler
        self.assertRaises(KeyError, get_crypt_handler, "dummy_1")
        self.assertIs(get_crypt_handler("dummy_1", None), None)

        # already loaded handler
        register_crypt_handler(dummy_1)
        self.assertIs(get_crypt_handler("dummy_1"), dummy_1)

        with warnings.catch_warnings():
            warnings.filterwarnings(
                "ignore",
                "handler names should be lower-case, and use underscores instead of hyphens:.*",
                UserWarning)

            # already loaded handler, using incorrect name
            self.assertIs(get_crypt_handler("DUMMY-1"), dummy_1)

            # lazy load of unloaded handler, using incorrect name
            register_crypt_handler_path('dummy_0', __name__)
            self.assertIs(get_crypt_handler("DUMMY-0"), dummy_0)

        # check system & private names aren't returned
        from passlib import hash
        hash.__dict__["_fake"] = "dummy"
        for name in ["_fake", "__package__"]:
            self.assertRaises(KeyError, get_crypt_handler, name)
            self.assertIs(get_crypt_handler(name, None), None)
Ejemplo n.º 2
0
    def test_get_crypt_handler(self):
        """test get_crypt_handler()"""

        class dummy_1(uh.StaticHandler):
            name = "dummy_1"

        # without available handler
        self.assertRaises(KeyError, get_crypt_handler, "dummy_1")
        self.assertIs(get_crypt_handler("dummy_1", None), None)

        # already loaded handler
        register_crypt_handler(dummy_1)
        self.assertIs(get_crypt_handler("dummy_1"), dummy_1)

        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", "handler names should be lower-case, and use underscores instead of hyphens:.*", UserWarning)

            # already loaded handler, using incorrect name
            self.assertIs(get_crypt_handler("DUMMY-1"), dummy_1)

            # lazy load of unloaded handler, using incorrect name
            register_crypt_handler_path('dummy_0', __name__)
            self.assertIs(get_crypt_handler("DUMMY-0"), dummy_0)

        # check system & private names aren't returned
        import passlib.hash # ensure module imported, so py3.3 sets __package__
        passlib.hash.__dict__["_fake"] = "dummy" # so behavior seen under py2x also
        for name in ["_fake", "__package__"]:
            self.assertRaises(KeyError, get_crypt_handler, name)
            self.assertIs(get_crypt_handler(name, None), None)
Ejemplo n.º 3
0
    def test_register_crypt_handler_path(self):
        """test register_crypt_handler_path()"""
        # NOTE: this messes w/ internals of registry, shouldn't be used publically.
        paths = registry._locations

        # check namespace is clear
        self.assertTrue("dummy_0" not in paths)
        self.assertFalse(hasattr(hash, "dummy_0"))

        # check invalid names are rejected
        self.assertRaises(ValueError, register_crypt_handler_path, "dummy_0",
                          ".test_registry")
        self.assertRaises(
            ValueError,
            register_crypt_handler_path,
            "dummy_0",
            __name__ + ":dummy_0:xxx",
        )
        self.assertRaises(
            ValueError,
            register_crypt_handler_path,
            "dummy_0",
            __name__ + ":dummy_0.xxx",
        )

        # try lazy load
        register_crypt_handler_path("dummy_0", __name__)
        self.assertTrue("dummy_0" in list_crypt_handlers())
        self.assertTrue("dummy_0" not in list_crypt_handlers(loaded_only=True))
        self.assertIs(hash.dummy_0, dummy_0)
        self.assertTrue("dummy_0" in list_crypt_handlers(loaded_only=True))
        unload_handler_name("dummy_0")

        # try lazy load w/ alt
        register_crypt_handler_path("dummy_0", __name__ + ":alt_dummy_0")
        self.assertIs(hash.dummy_0, alt_dummy_0)
        unload_handler_name("dummy_0")

        # check lazy load w/ wrong type fails
        register_crypt_handler_path("dummy_x", __name__)
        self.assertRaises(TypeError, get_crypt_handler, "dummy_x")

        # check lazy load w/ wrong name fails
        register_crypt_handler_path("alt_dummy_0", __name__)
        self.assertRaises(ValueError, get_crypt_handler, "alt_dummy_0")
        unload_handler_name("alt_dummy_0")

        # TODO: check lazy load which calls register_crypt_handler (warning should be issued)
        sys.modules.pop("passlib.tests._test_bad_register", None)
        register_crypt_handler_path("dummy_bad",
                                    "passlib.tests._test_bad_register")
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", "xxxxxxxxxx", DeprecationWarning)
            h = get_crypt_handler("dummy_bad")
        from passlib.tests import _test_bad_register as tbr

        self.assertIs(h, tbr.alt_dummy_bad)
Ejemplo n.º 4
0
    def test_kwd_constructor(self):
        self.assertFalse(has_crypt_handler("dummy_2"))
        register_crypt_handler_path("dummy_2", "passlib.tests.test_context")

        cc = LazyCryptContext(iter(["dummy_2", "des_crypt"]), deprecated=["des_crypt"])

        self.assertFalse(has_crypt_handler("dummy_2", True))

        self.assertTrue(cc.policy.handler_is_deprecated("des_crypt"))
        self.assertEqual(cc.policy.schemes(), ["dummy_2", "des_crypt"])

        self.assertTrue(has_crypt_handler("dummy_2", True))
Ejemplo n.º 5
0
    def test_callable_constructor(self):
        self.assertFalse(has_crypt_handler("dummy_2"))
        register_crypt_handler_path("dummy_2", "passlib.tests.test_context")

        def create_policy(flag=False):
            self.assertTrue(flag)
            return CryptPolicy(schemes=iter(["dummy_2", "des_crypt"]), deprecated=["des_crypt"])

        cc = LazyCryptContext(create_policy=create_policy, flag=True)

        self.assertFalse(has_crypt_handler("dummy_2", True))

        self.assertTrue(cc.policy.handler_is_deprecated("des_crypt"))
        self.assertEqual(cc.policy.schemes(), ["dummy_2", "des_crypt"])

        self.assertTrue(has_crypt_handler("dummy_2", True))
Ejemplo n.º 6
0
    def test_register_crypt_handler_path(self):
        """test register_crypt_handler_path()"""
        # NOTE: this messes w/ internals of registry, shouldn't be used publically.
        paths = registry._locations

        # check namespace is clear
        self.assertTrue('dummy_0' not in paths)
        self.assertFalse(hasattr(hash, 'dummy_0'))

        # check invalid names are rejected
        self.assertRaises(ValueError, register_crypt_handler_path,
                          "dummy_0", ".test_registry")
        self.assertRaises(ValueError, register_crypt_handler_path,
                          "dummy_0", __name__ + ":dummy_0:xxx")
        self.assertRaises(ValueError, register_crypt_handler_path,
                          "dummy_0", __name__ + ":dummy_0.xxx")

        # try lazy load
        register_crypt_handler_path('dummy_0', __name__)
        self.assertTrue('dummy_0' in list_crypt_handlers())
        self.assertTrue('dummy_0' not in list_crypt_handlers(loaded_only=True))
        self.assertIs(hash.dummy_0, dummy_0)
        self.assertTrue('dummy_0' in list_crypt_handlers(loaded_only=True))
        unload_handler_name('dummy_0')

        # try lazy load w/ alt
        register_crypt_handler_path('dummy_0', __name__ + ':alt_dummy_0')
        self.assertIs(hash.dummy_0, alt_dummy_0)
        unload_handler_name('dummy_0')

        # check lazy load w/ wrong type fails
        register_crypt_handler_path('dummy_x', __name__)
        self.assertRaises(TypeError, get_crypt_handler, 'dummy_x')

        # check lazy load w/ wrong name fails
        register_crypt_handler_path('alt_dummy_0', __name__)
        self.assertRaises(ValueError, get_crypt_handler, "alt_dummy_0")
        unload_handler_name("alt_dummy_0")

        # TODO: check lazy load which calls register_crypt_handler (warning should be issued)
        sys.modules.pop("passlib.tests._test_bad_register", None)
        register_crypt_handler_path("dummy_bad", "passlib.tests._test_bad_register")
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", "xxxxxxxxxx", DeprecationWarning)
            h = get_crypt_handler("dummy_bad")
        from passlib.tests import _test_bad_register as tbr
        self.assertIs(h, tbr.alt_dummy_bad)
Ejemplo n.º 7
0
    def test_get_crypt_handler(self):
        "test get_crypt_handler()"

        class dummy_1(uh.StaticHandler):
            name = "dummy_1"

        # without available handler
        self.assertRaises(KeyError, get_crypt_handler, "dummy_1")
        self.assertIs(get_crypt_handler("dummy_1", None), None)

        # already loaded handler
        register_crypt_handler(dummy_1)
        self.assertIs(get_crypt_handler("dummy_1"), dummy_1)

        with catch_warnings():
            warnings.filterwarnings("ignore", "handler names should be lower-case, and use underscores instead of hyphens:.*", UserWarning)

            # already loaded handler, using incorrect name
            self.assertIs(get_crypt_handler("DUMMY-1"), dummy_1)

            # lazy load of unloaded handler, using incorrect name
            register_crypt_handler_path('dummy_0', __name__)
            self.assertIs(get_crypt_handler("DUMMY-0"), dummy_0)
Ejemplo n.º 8
0
import ConfigParser
from urlparse import urlsplit, urlunsplit

from passlib.context import CryptContext
from passlib.registry import register_crypt_handler_path

# Register our legacy password handler
register_crypt_handler_path("bcrypt_sha1", "legacy_passwords")


class Config:
    ''' Read in the config and set up the vars with the correct type.
    '''
    def __init__(self, configfile):
        c = ConfigParser.ConfigParser()
        c.read(configfile)
        self.database_name = c.get('database', 'name')
        self.database_user = c.get('database', 'user')
        if c.has_option('database', 'driver'):
            self.database_driver = c.get('database', 'driver')
        else:
            self.database_driver = 'psycopg2'
        if c.has_option('database', 'password'):
            self.database_pw = c.get('database', 'password')
        else:
            self.database_pw = None
        if c.has_option('database', 'host'):
            self.database_host = c.get('database', 'host')
        else:
            self.database_host = None
        if c.has_option('database', 'port'):
Ejemplo n.º 9
0
import ConfigParser
from urlparse import urlsplit, urlunsplit

from passlib.context import CryptContext
from passlib.registry import register_crypt_handler_path


# Register our legacy password handler
register_crypt_handler_path("bcrypt_sha1", "legacy_passwords")


class Config:
    ''' Read in the config and set up the vars with the correct type.
    '''
    def __init__(self, configfile):
        c = ConfigParser.ConfigParser()
        c.read(configfile)
        self.database_name = c.get('database', 'name')
        self.database_user = c.get('database', 'user')
        if c.has_option('database', 'driver'):
            self.database_driver = c.get('database', 'driver')
        else:
            self.database_driver = 'psycopg2'
        if c.has_option('database', 'password'):
            self.database_pw = c.get('database', 'password')
        else:
            self.database_pw = None
        if c.has_option('database', 'host'):
            self.database_host = c.get('database', 'host')
        else:
            self.database_host = None
Ejemplo n.º 10
0
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.orm import relationship, backref
from sqlalchemy.orm.collections import attribute_mapped_collection
from sqlalchemy.ext.associationproxy import association_proxy

from passlib.context import CryptContext
from passlib.registry import register_crypt_handler_path

if settings['use_ldap']:
    from yubiauth.core.ldapauth import LDAPAuthenticator
    global ldapauth
    ldapauth = LDAPAuthenticator(settings['ldap_server'],
                                 settings['ldap_bind_dn'])

if settings['use_hsm']:
    register_crypt_handler_path('yhsm_pbkdf2_sha1', 'yubiauth.yhsm')
    register_crypt_handler_path('yhsm_pbkdf2_sha256', 'yubiauth.yhsm')
    register_crypt_handler_path('yhsm_pbkdf2_sha512', 'yubiauth.yhsm')


Base = declarative_base()

pwd_context = CryptContext(**settings['crypt_context'])


user_yubikeys = Table(
    'user_yubikeys',
    Base.metadata,
    Column('user_id', Integer, ForeignKey('users.id', ondelete='cascade')),
    Column('yubikey_id', Integer,
           ForeignKey('yubikeys.id', ondelete='cascade'))
Ejemplo n.º 11
0
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.orm import relationship, backref
from sqlalchemy.orm.collections import attribute_mapped_collection
from sqlalchemy.ext.associationproxy import association_proxy

from passlib.context import CryptContext
from passlib.registry import register_crypt_handler_path

if settings['use_ldap']:
    from yubiauth.core.ldapauth import LDAPAuthenticator
    global ldapauth
    ldapauth = LDAPAuthenticator(settings['ldap_server'],
                                 settings['ldap_bind_dn'])

if settings['use_hsm']:
    register_crypt_handler_path('yhsm_pbkdf2_sha1', 'yubiauth.yhsm')
    register_crypt_handler_path('yhsm_pbkdf2_sha256', 'yubiauth.yhsm')
    register_crypt_handler_path('yhsm_pbkdf2_sha512', 'yubiauth.yhsm')

Base = declarative_base()

pwd_context = CryptContext(**settings['crypt_context'])

user_yubikeys = Table(
    'user_yubikeys', Base.metadata,
    Column('user_id', Integer, ForeignKey('users.id', ondelete='cascade')),
    Column('yubikey_id', Integer, ForeignKey('yubikeys.id',
                                             ondelete='cascade')))


class AttributeAssociation(Base):
Ejemplo n.º 12
0
def register_hash():
    register_crypt_handler_path("aethery1", "aetherguild.deprecated.hash:AetherReallyOldHash")
    register_crypt_handler_path("aethery2", "aetherguild.deprecated.hash:AetherOldHash")