Refactor password hashing example

This commit is contained in:
Roman Mogylatov 2020-09-07 11:50:32 -04:00
parent e257fd9cb0
commit 5306b27c48
8 changed files with 84 additions and 73 deletions

View File

@ -14,6 +14,6 @@ This sections contains assorted ``Dependency Injector`` examples.
:maxdepth: 2
use_cases_miniapp
password_hashing_miniapp
password-hashing
chained_factories
factory_of_factories

View File

@ -0,0 +1,19 @@
Password hashing example
========================
This example demonstrates an injection of the ``Callable`` provider.
Listing of the ``example.py``:
.. literalinclude:: ../../examples/miniapps/password-hashing/example.py
:language: python
Instructions for running:
.. code-block:: bash
python example.py
You can find the source code on the `Github <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/password-hashing>`_.
.. disqus::

View File

@ -1,18 +0,0 @@
Dependency injection and password hashing in Python
===================================================
Small example that demonstrates using of dependency injection for user
password hashing.
Instructions for running:
.. code-block:: bash
python example.py
Listing of ``example.py``:
.. literalinclude:: ../../examples/miniapps/password_hashing/example.py
:language: python
.. disqus::

View File

@ -7,6 +7,10 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_
Develop
-------
- Refactor "password hashing" example.
3.40.0
------
- Add "Decoupled packages" example.

View File

@ -0,0 +1,10 @@
Password hashing
================
This example demonstrates an injection of the ``Callable`` provider.
Instructions for running:
.. code-block:: bash
python example.py

View File

@ -0,0 +1,50 @@
"""Password hashing example."""
from typing import Callable, Dict
import passlib.hash
from dependency_injector import containers, providers
class UserService:
def __init__(self, password_hasher: Callable[[str], str]) -> None:
self._password_hasher = password_hasher
def create_user(self, name: str, password: str) -> Dict[str, str]:
hashed_password = self._password_hasher(password)
return {
'name': name,
'password': hashed_password,
}
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
password_hasher = providers.Callable(
passlib.hash.sha256_crypt.hash,
salt_size=config.salt_size,
rounds=config.rounds,
)
user_service = providers.Factory(
UserService,
password_hasher=password_hasher.provider,
)
if __name__ == '__main__':
container = Container(
config={
'salt_size': 16,
'rounds': 10000,
},
)
user_service = container.user_service()
user = user_service.create_user(name='Roman', password='secret1')
print(user)

View File

@ -1,12 +0,0 @@
Dependency injection and password hashing in Python
===================================================
Small example that demonstrates using of dependency injection for user
password hashing.
instructions for running:
.. code-block:: bash
python example.py

View File

@ -1,42 +0,0 @@
"""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:
"""Users service."""
def __init__(self, password_hasher):
"""Initialize instance."""
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,
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)