-
Notifications
You must be signed in to change notification settings - Fork 4
/
example-04.py
53 lines (34 loc) · 1.22 KB
/
example-04.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
This example illustrates how dependency injection can be used for authentication
handlers.
"""
import asyncio
from rodi import Container
from guardpost import AuthenticationHandler, AuthenticationStrategy, Identity
class MyAppContext:
"""
This represents a context for an application - it can be anything depending on
use cases and the user's notion of application context.
"""
def __init__(self) -> None:
self.identity: Identity | None = None
class Foo:
"""Example to illustrate dependency injection."""
class MyAuthenticationHandler(AuthenticationHandler):
def __init__(self, foo: Foo) -> None:
# foo will be injected
self.foo = foo
def authenticate(self, context: MyAppContext) -> "Identity | None":
assert isinstance(self.foo, Foo)
return Identity({"sub": "001"}, self.scheme)
async def main():
container = Container()
container.register(Foo)
container.register(MyAuthenticationHandler)
authentication = AuthenticationStrategy(
MyAuthenticationHandler, container=container
)
some_context = MyAppContext()
identity = await authentication.authenticate(some_context)
assert identity is not None
asyncio.run(main())