aiohttp middleware for working with JWT
Project description
aiohttp-jwt
The API is in the process of settling, but has not yet had sufficient real-world testing to be considered stable. Backwards-compatibility will be maintained if reasonable.
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
Install
$ pip install aiohttp_jwt
Simple Usage
server.py
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
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
Credits
This module inspired by official auth0/express-jwt middleware and express-jwt-permissions extension.
Related packages
For advanced security facilities check aio-libs/aiohttp_security
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
aiohttp_jwt-0.3.0.tar.gz
(5.3 kB
view hashes)
Built Distribution
Close
Hashes for aiohttp_jwt-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0af96db8314bb70b8361a8c86dde32945180739fdc2846493423f4d6daf55c09 |
|
MD5 | 229ae75a2ece5f4b1009e901317a69b0 |
|
BLAKE2b-256 | a18f3e90d682c9fbf2b0ee14f55d6ecdd556df7b86e33cf817f82ca17dbcca97 |