forked from hzlmn/aiohttp-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.py
37 lines (24 loc) · 810 Bytes
/
basic.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
import jwt
from aiohttp import web
from aiohttp_jwt import JWTMiddleware
sharable_secret = "secret"
async def public_handler(request):
return web.json_response({"username": "anonymous"})
async def protected_handler(request):
return web.json_response({"username": request["user"].get("username", "anonymous")})
async def get_token(request):
return jwt.encode({"username": "johndoe"}, sharable_secret)
app = web.Application(
middlewares=[
JWTMiddleware(
secret_or_pub_key=sharable_secret,
token_getter=get_token,
request_property="user",
whitelist=[r"/public*"],
)
]
)
app.router.add_get("/public", public_handler)
app.router.add_get("/protected", protected_handler)
if __name__ == "__main__":
web.run_app(app)