Skip to content

Commit eb738d9

Browse files
committed
Switched from _request_ctx_stack.top to flask.g
1 parent 37634ed commit eb738d9

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

flask_jwt_extended/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Optional
44

55
import jwt
6-
from flask import _request_ctx_stack
6+
from flask import g
77
from flask import Response
88
from werkzeug.local import LocalProxy
99

@@ -23,7 +23,7 @@ def get_jwt() -> dict:
2323
:return:
2424
The payload (claims) of the JWT in the current request
2525
"""
26-
decoded_jwt = getattr(_request_ctx_stack.top, "jwt", None)
26+
decoded_jwt = getattr(g, "_jwt_extended_jwt", None)
2727
if decoded_jwt is None:
2828
raise RuntimeError(
2929
"You must call `@jwt_required()` or `verify_jwt_in_request()` "
@@ -41,7 +41,7 @@ def get_jwt_header() -> dict:
4141
:return:
4242
The headers of the JWT in the current request
4343
"""
44-
decoded_header = getattr(_request_ctx_stack.top, "jwt_header", None)
44+
decoded_header = getattr(g, "_jwt_extended_jwt_header", None)
4545
if decoded_header is None:
4646
raise RuntimeError(
4747
"You must call `@jwt_required()` or `verify_jwt_in_request()` "
@@ -73,7 +73,7 @@ def get_jwt_request_location() -> Optional[str]:
7373
The location of the JWT in the current request; e.g., "cookies",
7474
"query-string", "headers", or "json"
7575
"""
76-
return getattr(_request_ctx_stack.top, "jwt_location", None)
76+
return getattr(g, "_jwt_extended_jwt_location", None)
7777

7878

7979
def get_current_user() -> Any:
@@ -91,7 +91,7 @@ def get_current_user() -> Any:
9191
The current user object for the JWT in the current request
9292
"""
9393
get_jwt() # Raise an error if not in a decorated context
94-
jwt_user_dict = getattr(_request_ctx_stack.top, "jwt_user", None)
94+
jwt_user_dict = getattr(g, "_jwt_extended_jwt_user", None)
9595
if jwt_user_dict is None:
9696
raise RuntimeError(
9797
"You must provide a `@jwt.user_lookup_loader` callback to use "

flask_jwt_extended/view_decorators.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from typing import Tuple
99
from typing import Union
1010

11-
from flask import _request_ctx_stack
1211
from flask import current_app
12+
from flask import g
1313
from flask import request
1414
from werkzeug.exceptions import BadRequest
1515

@@ -86,8 +86,9 @@ def verify_jwt_in_request(
8686
return None
8787

8888
# Should be impossible to hit, this makes mypy checks happy
89-
if not _request_ctx_stack.top: # pragma: no cover
90-
raise RuntimeError("No _request_ctx_stack.top present, aborting")
89+
# TODO: Convert this check to the new `flask.g` object
90+
# if not _request_ctx_stack.top: # pragma: no cover
91+
# raise RuntimeError("No _request_ctx_stack.top present, aborting")
9192

9293
try:
9394
jwt_data, jwt_header, jwt_location = _decode_jwt_from_request(
@@ -97,18 +98,18 @@ def verify_jwt_in_request(
9798
except NoAuthorizationError:
9899
if not optional:
99100
raise
100-
_request_ctx_stack.top.jwt = {}
101-
_request_ctx_stack.top.jwt_header = {}
102-
_request_ctx_stack.top.jwt_user = {"loaded_user": None}
103-
_request_ctx_stack.top.jwt_location = None
101+
g._jwt_extended_jwt = {}
102+
g._jwt_extended_jwt_header = {}
103+
g._jwt_extended_jwt_user = {"loaded_user": None}
104+
g._jwt_extended_jwt_location = None
104105
return None
105106

106107
# Save these at the very end so that they are only saved in the requet
107108
# context if the token is valid and all callbacks succeed
108-
_request_ctx_stack.top.jwt_user = _load_user(jwt_header, jwt_data)
109-
_request_ctx_stack.top.jwt_header = jwt_header
110-
_request_ctx_stack.top.jwt = jwt_data
111-
_request_ctx_stack.top.jwt_location = jwt_location
109+
g._jwt_extended_jwt_user = _load_user(jwt_header, jwt_data)
110+
g._jwt_extended_jwt_header = jwt_header
111+
g._jwt_extended_jwt = jwt_data
112+
g._jwt_extended_jwt_location = jwt_location
112113

113114
return jwt_header, jwt_data
114115

0 commit comments

Comments
 (0)