-
-
Notifications
You must be signed in to change notification settings - Fork 342
Expand file tree
/
Copy pathcallable_reusable.py
More file actions
38 lines (26 loc) · 947 Bytes
/
callable_reusable.py
File metadata and controls
38 lines (26 loc) · 947 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""`Callable` provider example with configurable parameters"""
import passlib.hash
from dependency_injector import containers, providers
from dependency_injector.wiring import Provide
class Service:
def __init__(self, hasher):
self.hasher = hasher
def hash(self, value):
return self.hasher(value)
class Container(containers.DeclarativeContainer):
password_hasher = providers.Callable(
passlib.hash.sha256_crypt.hash,
salt_size=16,
rounds=10000,
)
password_verifier = providers.Callable(passlib.hash.sha256_crypt.verify)
service = providers.Factory(
Service,
hasher=password_hasher.provider
)
if __name__ == "__main__":
service: Service = Provide["service"]
container = Container()
container.wire(modules=[__name__])
hashed_password = service.hash("super secret")
assert container.password_verifier("super secret", hashed_password)