From 5306b27c488e9e682328941bd5a279805d50bb0d Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Mon, 7 Sep 2020 11:50:32 -0400 Subject: [PATCH] Refactor password hashing example --- docs/examples-other/index.rst | 2 +- docs/examples-other/password-hashing.rst | 19 +++++++ .../password_hashing_miniapp.rst | 18 ------- docs/main/changelog.rst | 4 ++ examples/miniapps/password-hashing/README.rst | 10 ++++ examples/miniapps/password-hashing/example.py | 50 +++++++++++++++++++ examples/miniapps/password_hashing/README.rst | 12 ----- examples/miniapps/password_hashing/example.py | 42 ---------------- 8 files changed, 84 insertions(+), 73 deletions(-) create mode 100644 docs/examples-other/password-hashing.rst delete mode 100644 docs/examples-other/password_hashing_miniapp.rst create mode 100644 examples/miniapps/password-hashing/README.rst create mode 100644 examples/miniapps/password-hashing/example.py delete mode 100644 examples/miniapps/password_hashing/README.rst delete mode 100644 examples/miniapps/password_hashing/example.py diff --git a/docs/examples-other/index.rst b/docs/examples-other/index.rst index 8b082fa8..457ff5b3 100644 --- a/docs/examples-other/index.rst +++ b/docs/examples-other/index.rst @@ -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 diff --git a/docs/examples-other/password-hashing.rst b/docs/examples-other/password-hashing.rst new file mode 100644 index 00000000..6ad0173c --- /dev/null +++ b/docs/examples-other/password-hashing.rst @@ -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 `_. + +.. disqus:: diff --git a/docs/examples-other/password_hashing_miniapp.rst b/docs/examples-other/password_hashing_miniapp.rst deleted file mode 100644 index 21177545..00000000 --- a/docs/examples-other/password_hashing_miniapp.rst +++ /dev/null @@ -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:: diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 152f925f..9a67cd9f 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -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. diff --git a/examples/miniapps/password-hashing/README.rst b/examples/miniapps/password-hashing/README.rst new file mode 100644 index 00000000..a979b1e6 --- /dev/null +++ b/examples/miniapps/password-hashing/README.rst @@ -0,0 +1,10 @@ +Password hashing +================ + +This example demonstrates an injection of the ``Callable`` provider. + +Instructions for running: + +.. code-block:: bash + + python example.py diff --git a/examples/miniapps/password-hashing/example.py b/examples/miniapps/password-hashing/example.py new file mode 100644 index 00000000..e9d042b9 --- /dev/null +++ b/examples/miniapps/password-hashing/example.py @@ -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) diff --git a/examples/miniapps/password_hashing/README.rst b/examples/miniapps/password_hashing/README.rst deleted file mode 100644 index 9c306ab5..00000000 --- a/examples/miniapps/password_hashing/README.rst +++ /dev/null @@ -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 diff --git a/examples/miniapps/password_hashing/example.py b/examples/miniapps/password_hashing/example.py deleted file mode 100644 index 1273330e..00000000 --- a/examples/miniapps/password_hashing/example.py +++ /dev/null @@ -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)