Coder Social home page Coder Social logo

tenproduct / aiohttp-jwt Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hzlmn/aiohttp-jwt

0.0 2.0 0.0 127 KB

aiohttp middleware and helper utils for working with JSON web token.

License: MIT License

Python 80.67% Dockerfile 2.04% Makefile 16.41% Shell 0.88%

aiohttp-jwt's Introduction

aiohttp-jwt

Downloads TeamCity build status codecov

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!

Getting started (docker-based)

make build

Getting started (locally)

# Create virtualenv
python -m venv .venv

# Activate virtualenv
source .venv/bin/activate

# Install requirements
pip install -r requirements.txt

Run tests (docker-based)

make test

Compile requirements (docker-based)

# Run this if you have an ed25519 ssh key pair
make pip-compile-ed-25519

# Run this if you have an rsa ssh key pair
make pip-compile-rsa

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

aiohttp-jwt's People

Contributors

hzlmn avatar pyup-bot avatar vikitikitavi avatar idegree avatar ckkz-it avatar gbarbaten avatar kavithamadhavaraj avatar ranc58 avatar alexandrueneten avatar rockwelln avatar

Watchers

James Cloos avatar Liam Kenneth avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.