vimalloc / flask-jwt-extended

An open source Flask extension that provides JWT support (with batteries included)!
http://flask-jwt-extended.readthedocs.io/en/stable/
MIT License
1.54k stars 240 forks source link

_request_ctx_stack is deprecated as of flask 2.2 #494

Closed tgross35 closed 2 years ago

tgross35 commented 2 years ago

This is in the same vein as #492, just some changes for Flask 2.2

  /usr/local/lib/python3.10/site-packages/flask_jwt_extended/utils.py:6: DeprecationWarning: '_request_ctx_stack' is deprecated and will be removed in Flask 2.3.
    from flask import _request_ctx_stack
../../../usr/local/lib/python3.10/site-packages/flask_jwt_extended/view_decorators.py:11
../../../usr/local/lib/python3.10/site-packages/flask_jwt_extended/view_decorators.py:11
  /usr/local/lib/python3.10/site-packages/flask_jwt_extended/view_decorators.py:11: DeprecationWarning: '_request_ctx_stack' is deprecated and will be removed in Flask 2.3.
    from flask import _request_ctx_stack

Trying to figure out what the correct solution is, but I'm having some trouble there

tgross35 commented 2 years ago

A different error for me recommends

Use 'g' to store data, or 'app_ctx' to access the current context.

tgross35 commented 2 years ago

And the example from the docs here https://flask.palletsprojects.com/en/2.2.x/appcontext/#storing-data


from flask import g

def get_db():
    if 'db' not in g:
        g.db = connect_to_database()

    return g.db

@app.teardown_appcontext
def teardown_db(exception):
    db = g.pop('db', None)

    if db is not None:
        db.close()

It looks like in this package, utils.py and view_decorators.py are the only two files where _request_ctx_stack is used. My super rough diff that would probably change this:

# utils.py

< from flask import _request_ctx_stack
> from flask import g

<    decoded_jwt = getattr(_request_ctx_stack.top, "jwt", None)
>    decoded_jwt = g.get("_flask_jwt_extended_jwt", None)

<    decoded_header = getattr(_request_ctx_stack.top, "jwt_header", None)
>    decoded_header = g.get("_flask_jwt_extended_header", None)

<    return getattr(_request_ctx_stack.top, "jwt_location", None)
>    return g.get("_flask_jwt_extended_location", None)

<    jwt_user_dict = getattr(_request_ctx_stack.top, "jwt_user", None)
>    jwt_user_dict = g.get("_flask_jwt_extended_user", None)
# view_decorators.py

< from flask import _request_ctx_stack
> from flask import g

<    # Should be impossible to hit, this makes mypy checks happy
<    if not _request_ctx_stack.top:  # pragma: no cover
<        raise RuntimeError("No _request_ctx_stack.top present, aborting")

<        _request_ctx_stack.top.jwt = {}
<        _request_ctx_stack.top.jwt_header = {}
<        _request_ctx_stack.top.jwt_user = {"loaded_user": None}
<        _request_ctx_stack.top.jwt_location = None
>        g._flask_jet_extended_jwt = {}
>        g._flask_jet_extended_header = {}
>        g._flask_jet_extended_user = {"loaded_user": None}
>        g._flask_jet_extended_location = None

<    _request_ctx_stack.top.jwt_user = _load_user(jwt_header, jwt_data)
<    _request_ctx_stack.top.jwt_header = jwt_header
<    _request_ctx_stack.top.jwt = jwt_data
<    _request_ctx_stack.top.jwt_location = jwt_location
>    g._flask_jwt_extended_user = _load_user(jwt_header, jwt_data)
>    g._flask_jwt_extended_header = jwt_header
>    g._flask_jwt_extended_jwt = jwt_data
>    g._flask_jwt_extended_location = jwt_location
tgross35 commented 2 years ago

-__- guess I'm behind the curve and this was already sneakily done here https://github.com/vimalloc/flask-jwt-extended/pull/493

Goodbye, issue