-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample-03.py
More file actions
54 lines (36 loc) · 1.42 KB
/
example-03.py
File metadata and controls
54 lines (36 loc) · 1.42 KB
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
54
"""
This example illustrates a basic use of the authentication strategy, showing how
authentication handlers can be grouped by authentication schemes.
"""
import asyncio
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 AuthenticationHandlerOne(AuthenticationHandler):
@property
def scheme(self) -> str:
return "one"
def authenticate(self, context: MyAppContext) -> "Identity | None":
return Identity({"sub": "001"}, self.scheme)
class AuthenticationHandlerTwo(AuthenticationHandler):
@property
def scheme(self) -> str:
return "two"
def authenticate(self, context: MyAppContext) -> "Identity | None":
return Identity({"sub": "002"}, self.scheme)
async def main():
authentication = AuthenticationStrategy(
AuthenticationHandlerOne(), AuthenticationHandlerTwo()
)
for scheme in ["one", "two"]:
some_context = MyAppContext()
identity = await authentication.authenticate(some_context, [scheme])
assert identity is not None
assert identity.authentication_mode == scheme
assert some_context.identity is identity
asyncio.run(main())