Adding auth decorators to flask restx Adding auth decorators to flask restx flask flask

Adding auth decorators to flask restx


Based on this and this, method_decorators variable should be a list of functions, so you should use it like:

def _perform_auth(method):    is_public_endpoint = getattr(method, 'is_public', False)    # place the validation hereclass Resource(flask_restx.Resource):    method_decorators = [_perform_auth]


Is it possible to reach the decorator from the view_function?

Well... it is possible, but I wouldn't recommend it. Here's an example

Is it possible to know whether the endpoint is a restx resource or aplain rest function?

You probably can inspect the func and figure out if it's from restx, maybe looking at __qualname__, but then again, I`d wouldn't recommend it.

Is there a better way to do what I'm trying to achieve?

I would go one of these solutions:

  • Explicitly decorate view_funcs and resources that do need authentication, instead of the other way around
  • Create a blueprint for public endpoints, a blueprint for protected endpoints with the before_request decorator for authorization