Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kwargs for add_route -> Custom attributes for the route #4019

Closed
lennart-k opened this issue Aug 30, 2019 · 1 comment
Closed

kwargs for add_route -> Custom attributes for the route #4019

lennart-k opened this issue Aug 30, 2019 · 1 comment

Comments

@lennart-k
Copy link

Long story short

I'd like to give my API routes a scope attributes so that I can have a middleware check for authorization.

Actual behaviour

My current approach would be either to create a wrapper for the handler with a decorator or to fork web_urldispatcher.py.
I feel like both approaches are pretty horrible.

Steps to reproduce

Imagine something like this:

r = web.RouteTableDef()

@r.get("GET", "/ping", scopes=["no_authentication"])
async def ping(request: web.Request) -> web.Response:
	...

# Middleware
async def middleware(request: web.Request, handler) -> web.Response:
    scopes = request.route.scopes
    # or maybe
	scopes = request.route.data.scopes

Btw I'm aware that this issue is quite similar to #2038 but I think while functools.partial is a fine solution to pass arguments to the handler,
a wrapper is quite a bad solution in my case as this solution does not let me add my middleware to the app's middleware chain.

@asvetlov
Copy link
Member

I have a bad guts-feeling on adding random named fields as proposed here.

The problem can be solved with current aiohttp release though:

r = web.RouteTableDef()

@r.get("GET", "/ping")
async def ping(request: web.Request) -> web.Response:
	...

ping.scopes=["no_authentication"]

# Middleware
async def middleware(request: web.Request, handler) -> web.Response:
    scopes = getattr(request.match_info.handler, [])

@asvetlov asvetlov closed this as completed Sep 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants