forked from hzlmn/aiohttp-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermissions.py
50 lines (37 loc) · 1.09 KB
/
permissions.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
import jwt
from aiohttp import web
from aiohttp_jwt import JWTMiddleware, check_permissions, match_any
sharable_secret = 'secret'
async def public_handler(request):
return web.json_response({
'username': request['user'].get('username')
if 'user' in request else 'anonymous',
})
@check_permissions([
'app/user:admin',
'username:olehkuchuk',
], comparison=match_any)
async def protected_handler(request):
return web.json_response({
'username': request['user'].get('username'),
})
async def get_token(request):
return jwt.encode({
'username': 'olehkuchuk',
'scopes': ['username:olehkuchuk'],
}, sharable_secret)
app = web.Application(
middlewares=[
JWTMiddleware(
secret_or_pub_key=sharable_secret,
token_getter=get_token,
request_property='user',
credentials_required=False,
whitelist=[r'/public*']
)
]
)
app.router.add_get('/public', public_handler)
app.router.add_get('/protected', protected_handler)
if __name__ == '__main__':
web.run_app(app)