Skip to main content

aiohttp middleware for working with JWT

Project description

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Description: ## aiohttp-jwt
[![stability-unstable](https://img.shields.io/badge/stability-unstable-yellow.svg)](https://img.shields.io/badge/stability-unstable-yellow.svg)
[![Updates](https://pyup.io/repos/github/hzlmn/aiohttp-jwt/shield.svg)](https://pyup.io/repos/github/hzlmn/aiohttp-jwt/)
[![Python 3](https://pyup.io/repos/github/hzlmn/aiohttp-jwt/python-3-shield.svg)](https://pyup.io/repos/github/hzlmn/aiohttp-jwt/)
[![Build Status](https://travis-ci.org/hzlmn/aiohttp-jwt.svg?branch=master)](https://travis-ci.org/hzlmn/aiohttp-jwt)
[![codecov](https://codecov.io/gh/hzlmn/aiohttp-jwt/branch/master/graph/badge.svg)](https://codecov.io/gh/hzlmn/aiohttp-jwt)
[![Documentation Status](https://readthedocs.org/projects/aiohttp-jwt/badge/?version=latest)](http://aiohttp-jwt.readthedocs.io/en/latest/?badge=latest)


> 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
- [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)



## Credentials

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

Keywords: asyncio,aiohttp,jwt
Platform: UNKNOWN
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.6

Project details


Release history Release notifications

This version
History Node

0.1.1

History Node

0.1.0

History Node

0.0.2

History Node

0.0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
aiohttp_jwt-0.1.1-py3-none-any.whl (8.5 kB) Copy SHA256 hash SHA256 Wheel py3 Apr 4, 2018
aiohttp_jwt-0.1.1.tar.gz (5.2 kB) Copy SHA256 hash SHA256 Source None Apr 4, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page