https://github.com/hzlmn/aiohttp-jwt
aiohttp middleware and helper utils for working with JSON web token.
https://github.com/hzlmn/aiohttp-jwt
aiohttp asyncio jwt python
Last synced: 10 months ago
JSON representation
aiohttp middleware and helper utils for working with JSON web token.
- Host: GitHub
- URL: https://github.com/hzlmn/aiohttp-jwt
- Owner: hzlmn
- License: mit
- Created: 2017-09-11T19:07:10.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-10-14T16:29:21.000Z (over 1 year ago)
- Last Synced: 2024-10-20T07:46:36.184Z (over 1 year ago)
- Topics: aiohttp, asyncio, jwt, python
- Language: Python
- Homepage:
- Size: 205 KB
- Stars: 76
- Watchers: 6
- Forks: 15
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
## aiohttp-jwt
[](https://pepy.tech/project/aiohttp-jwt/month)
[](https://travis-ci.org/hzlmn/aiohttp-jwt)
[](https://codecov.io/gh/hzlmn/aiohttp-jwt)
The library provides `aiohttp` middleware and helper utils for working with JSON web tokens.
* Works on Python3.5+
* MIT License
* Latest docs [TBD]()
* Contributions are highly welcome!
## Requirements
- [Aiohttp >= 2.3.5](https://github.com/aio-libs/aiohttp)
- [PyJWT](https://github.com/jpadilla/pyjwt)
## Install
```bash
$ pip install aiohttp_jwt
```
## Simple Usage
`server.py`
```python
import jwt
from aiohttp import web
from aiohttp_jwt import JWTMiddleware
sharable_secret = 'secret'
async def protected_handler(request):
return web.json_response({'user': request['payload']})
app = web.Application(
middlewares=[
JWTMiddleware(sharable_secret),
]
)
app.router.add_get('/protected', protected_handler)
if __name__ == '__main__':
web.run_app(app)
```
`client.py`
```python
import asyncio
import aiohttp
import async_timeout
async def fetch(session, url, headers=None):
async with async_timeout.timeout(10):
async with session.get(url, headers=headers) as response:
return await response.json()
async def main():
async with aiohttp.ClientSession() as session:
response = await fetch(
session,
'http://localhost:8080/protected',
headers={'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRlc3QifQ.pyNsXX_vNsUvdt6xu13F1Gs1zGELT4Va8a38eG5svBA'})
print(response)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```
## Examples
- [Basic Example](/example/basic.py)
- [Permissions control](/example/permissions.py)
## Credits
This module inspired by official [auth0/express-jwt](https://github.com/auth0/express-jwt) middleware and
[express-jwt-permissions](https://github.com/MichielDeMey/express-jwt-permissions) extension.
## Related packages
For advanced security facilities check [aio-libs/aiohttp_security](https://github.com/aio-libs/aiohttp-security)
### License
MIT License