Last active
April 9, 2020 10:26
-
-
Save ckkz-it/14ff3b91e0dfdee8f7ebecc8c73d2ddc to your computer and use it in GitHub Desktop.
Aiohttp JWTMiddleware (aiohttp_jwt) with CORS support (for preflight OPTIONS requests)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/*'], | |
) | |
] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JWTMiddleware from https://github.com/hzlmn/aiohttp-jwt