python-dependency-injector/examples/miniapps/password_hashing/example.py

43 lines
1.2 KiB
Python
Raw Normal View History

2018-06-27 19:29:55 +03:00
"""Example of dependency injection and password hashing in Python."""
import passlib.hash
import dependency_injector.containers as containers
import dependency_injector.providers as providers
class UsersService:
2018-06-27 19:29:55 +03:00
"""Users service."""
def __init__(self, password_hasher):
"""Initialize instance."""
2018-06-27 19:29:55 +03:00
self._password_hasher = password_hasher
def create_user(self, name, password):
"""Create user with hashed password."""
hashed_password = self._password_hasher(password)
return dict(name=name, password=hashed_password)
class Container(containers.DeclarativeContainer):
"""Inversion of control container."""
password_hasher = providers.Callable(
passlib.hash.sha256_crypt.hash,
2018-06-27 19:29:55 +03:00
salt_size=16,
rounds=10000)
users_service = providers.Factory(
UsersService,
password_hasher=password_hasher.provider)
if __name__ == '__main__':
container = Container()
users_service = container.users_service()
user1 = users_service.create_user(name='Roman', password='secret1')
user2 = users_service.create_user(name='Vitaly', password='secret2')
print(user1, user2)