aiohttp的身份验证库
aegis的Python项目详细描述
aegis允许protect端点 验证作用域。
安装
pip install aegis
简单示例
fromaiohttpimportwebfromaegisimportlogin_required,JWTAuthclassJWTAuthenticator(JWTAuth):jwt_secret="<secret>"asyncdefauthenticate(self,request:web.Request)->dict:db=request.app["db"]credentials=awaitrequest.json()id_=credentials["id"]user=db.get(id_)returnuser@login_requiredasyncdefprotected(request):returnweb.json_response({'hello':'user'})defcreate_app():app=web.Application()app["db"]={5:{"name":"test"}}app.router.add_get('/protected',protected)JWTAuthenticator.setup(app)returnappif__name__=="__main__":app=create_app()web.run_app(app)
获取访问令牌
curl -X POST http://0.0.0.0:8080/auth -d '{"id": 5}'{"access_token": "<access_token>"}
获取用户
curl http://0.0.0.0:8080/protected -H 'Authorization: Bearer <access_token>'{'hello': 'user'}
测试
git clone https://github.com/mgurdal/aegis.git
cd aegis
make cov
要求
- Python=3.6
- aiohttp
- Pyjwt
许可证
aegis是在apache 2许可下提供的。