Skip to content

Instantly share code, notes, and snippets.

@ckkz-it
Last active April 9, 2020 10:26
Show Gist options
  • Save ckkz-it/14ff3b91e0dfdee8f7ebecc8c73d2ddc to your computer and use it in GitHub Desktop.
Save ckkz-it/14ff3b91e0dfdee8f7ebecc8c73d2ddc to your computer and use it in GitHub Desktop.
Aiohttp JWTMiddleware (aiohttp_jwt) with CORS support (for preflight OPTIONS requests)
from aiohttp import web
from app.middlewares import jwt_middleware_with_cors
app = web.Application(
middlewares=[
jwt_middleware_with_cors(
secret_or_pub_key='very secret',
algorithms=['HS256'],
request_property='user',
whitelist=['/auth/*'],
)
]
)
from functools import wraps
from aiohttp import hdrs
from aiohttp.web_request import Request
from aiohttp_jwt import JWTMiddleware
def jwt_middleware_with_cors(*args, **kwargs):
mdlwr = JWTMiddleware(*args, **kwargs)
@wraps(mdlwr)
async def wrap(request: Request, handler):
if request.method == hdrs.METH_OPTIONS:
return await handler(request)
return await mdlwr(request, handler)
return wrap
@ckkz-it
Copy link
Author

ckkz-it commented Apr 9, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment