Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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 | ||
:linenos: | ||
|
||
.. disqus:: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""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(object): | ||
"""Users service.""" | ||
|
||
def __init__(self, password_hasher): | ||
"""Initializer.""" | ||
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.encrypt, | ||
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) |