vimalloc / vimalloc/flask-jwt-extended

@jwt_required does not work with the OPTIONS method

Open
#475 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.6k
Forks
253
PR merge metrics
No merged PRs in 30d

Description

Hello,

Thanks for the great work !

With the OPTIONS HTTP method, a confusing error message is raised, stating that @jwt_required() was not called, although it was called:

"You must call @jwt_required() or verify_jwt_in_request() "

Here is a way to reproduce the issue :

from flask import Flask, jsonify
from flask_jwt_extended import get_jwt_identity, jwt_required, JWTManager

app = Flask(__name__)
app.config["JWT_SECRET_KEY"] = "super-secret"
jwt = JWTManager(app)

@app.route("/", methods=["GET", "OPTIONS"])
@jwt_required(optional=True)
def protected():
    current_user = get_jwt_identity()
    return "hello", 200


if __name__ == "__main__":
    app.run()
$ curl -X OPTIONS http://0.0.0.0:5000/ -i 
HTTP/1.0 500 INTERNAL SERVER ERROR
Content-Type: text/html; charset=utf-8
Content-Length: 290
Server: Werkzeug/2.0.2 Python/3.7.11
Date: Wed, 13 Apr 2022 07:55:36 GMT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
2022-04-13 09:55:36,725] ERROR in app: Exception on / [OPTIONS]
Traceback (most recent call last):
  File "/***/venv/lib/python3.7/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/***/venv/lib/python3.7/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/***/venv/lib/python3.7/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/***/venv/lib/python3.7/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/***/venv/lib/python3.7/site-packages/flask_jwt_extended/view_decorators.py", line 127, in decorator
    return current_app.ensure_sync(fn)(*args, **kwargs)
  File "test_app.py", line 11, in protected
    current_user = get_jwt_identity()
  File "/***/venv/lib/python3.7/site-packages/flask_jwt_extended/utils.py", line 58, in get_jwt_identity
    return get_jwt().get(config.identity_claim_key, None)
  File "/***/venv/lib/python3.7/site-packages/flask_jwt_extended/utils.py", line 25, in get_jwt
    "You must call `@jwt_required()` or `verify_jwt_in_request()` "
RuntimeError: You must call `@jwt_required()` or `verify_jwt_in_request()` before using this method
127.0.0.1 - - [13/Apr/2022 09:55:36] "OPTIONS / HTTP/1.1" 500 -

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the OPTIONS request from the issue, then inspect flask_jwt_extended/view_decorators.py and flask_jwt_extended/utils.py at the traceback locations. Use test_app.py as the minimal example and add or run coverage for an OPTIONS request with @jwt_required(optional=True). Done means the request no longer raises the shown RuntimeError and the expected response is preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.