vimalloc/flask-jwt-extended

Direct call to decorator (jwt_required)

0coolcard0 opened this issue · 2 comments

jwt_required(func(arg))
Previously, in version 3.x, it was called and used as above.

A TypeError occurred in version 4.4.4.
->
Traceback (most recent call last):
File "/Users/user/PycharmProjects/flaskTestProject/venv/lib/python3.8/site-packages/flask/app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "/Users/user/PycharmProjects/flaskTestProject/venv/lib/python3.8/site-packages/flask/app.py", line 1823, in full_dispatch_request
return self.finalize_request(rv)
File "/Users/user/PycharmProjects/flaskTestProject/venv/lib/python3.8/site-packages/flask/app.py", line 1842, in finalize_request
response = self.make_response(rv)
File "/Users/user/PycharmProjects/flaskTestProject/venv/lib/python3.8/site-packages/flask/app.py", line 2162, in make_response
raise TypeError(
File "/Users/user/PycharmProjects/flaskTestProject/venv/lib/python3.8/site-packages/flask/app.py", line 2158, in make_response
rv = self.response_class.force_type(
File "/Users/user/PycharmProjects/flaskTestProject/venv/lib/python3.8/site-packages/werkzeug/wrappers/response.py", line 268, in force_type
response = Response(*run_wsgi_app(response, environ))
File "/Users/user/PycharmProjects/flaskTestProject/venv/lib/python3.8/site-packages/werkzeug/test.py", line 1242, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: wrapper() takes 1 positional argument but 2 were given

How should I call it in version 4.4.4?

def jwt_required(
optional: bool = False,
fresh: bool = False,
refresh: bool = False,
locations: LocationType = None,
verify_type: bool = True,
) -> Any:
"""
A decorator to protect a Flask endpoint with JSON Web Tokens.
Any route decorated with this will require a valid JWT to be present in the
request (unless optional=True, in which case no JWT is also valid) before the
endpoint can be called.
:param optional:
If ``True``, allow the decorated endpoint to be accessed if no JWT is present in
the request. Defaults to ``False``.
:param fresh:
If ``True``, require a JWT marked with ``fresh`` to be able to access this
endpoint. Defaults to ``False``.
:param refresh:
If ``True``, requires a refresh JWT to access this endpoint. If ``False``,
requires an access JWT to access this endpoint. Defaults to ``False``.
:param locations:
A location or list of locations to look for the JWT in this request, for
example ``'headers'`` or ``['headers', 'cookies']``. Defaults to ``None``
which indicates that JWTs will be looked for in the locations defined by the
``JWT_TOKEN_LOCATION`` configuration option.
:param verify_type:
If ``True``, the token type (access or refresh) will be checked according
to the ``refresh`` argument. If ``False``, type will not be checked and both
access and refresh tokens will be accepted.
"""
def wrapper(fn):
@wraps(fn)
def decorator(*args, **kwargs):
verify_jwt_in_request(optional, fresh, refresh, locations, verify_type)
return current_app.ensure_sync(fn)(*args, **kwargs)
return decorator
return wrapper

You can do it via jwt_required()(protected). See: https://stackoverflow.com/questions/21441100/manually-calling-a-decorator-that-takes-arguments

I do wonder why you are doing this though? It feels like using either the decorator (@jwt_required()) or calling verify_jwt_in_request() directly in your function would be easier.

I wanted to make the authentication method different depending on the version of the currently running app, so I wanted to call jwt_required directly.
However, when calling directly, typeerror occurred due to arguments problem.
I don't think it's good to call and use the decorator directly.
I will use verify_jwt_in_request as per your guide.
Thanks for the reply.