@@ -50,10 +50,10 @@ def verify_jwt_in_request(optional=False, fresh=False, refresh=False, locations=
50
50
If ``True``, require a refresh JWT to be verified.
51
51
52
52
:param locations:
53
- A list of locations to look for the JWT in this request, for example:
54
- `` ['headers', 'cookies']``. Defaluts to ``None`` which indicates that JWTs
55
- will be looked for in the locations defined by the ``JWT_TOKEN_LOCATION``
56
- configuration option.
53
+ A location or list of locations to look for the JWT in this request, for
54
+ example ``'headers'`` or `` ['headers', 'cookies']``. Defaluts to ``None``
55
+ which indicates that JWTs will be looked for in the locations defined by the
56
+ ``JWT_TOKEN_LOCATION`` configuration option.
57
57
"""
58
58
if request .method in config .exempt_methods :
59
59
return
@@ -103,10 +103,10 @@ def jwt_required(optional=False, fresh=False, refresh=False, locations=None):
103
103
requires an access JWT to access this endpoint. Defaults to ``False``.
104
104
105
105
:param locations:
106
- A list of locations to look for the JWT in this request, for example:
107
- `` ['headers', 'cookies']``. Defaluts to ``None`` which indicates that JWTs
108
- will be looked for in the locations defined by the ``JWT_TOKEN_LOCATION``
109
- configuration option.
106
+ A location or list of locations to look for the JWT in this request, for
107
+ example ``'headers'`` or `` ['headers', 'cookies']``. Defaluts to ``None``
108
+ which indicates that JWTs will be looked for in the locations defined by the
109
+ ``JWT_TOKEN_LOCATION`` configuration option.
110
110
"""
111
111
112
112
def wrapper (fn ):
@@ -227,26 +227,28 @@ def _decode_jwt_from_json(refresh):
227
227
228
228
229
229
def _decode_jwt_from_request (locations , fresh , refresh = False ):
230
- # All the places we can get a JWT from in this request
231
- get_encoded_token_functions = []
230
+ # Figure out what locations to look for the JWT in this request
231
+ if isinstance (locations , str ):
232
+ locations = [locations ]
232
233
233
- # Get locations in the order specified by the decorator or JWT_TOKEN_LOCATION
234
- # configuration.
235
234
if not locations :
236
235
locations = config .token_location
237
236
238
- # Add the functions in the order specified by locations.
237
+ # Get the decode functions in the order specified by locations.
238
+ get_encoded_token_functions = []
239
239
for location in locations :
240
240
if location == "cookies" :
241
241
get_encoded_token_functions .append (
242
242
lambda : _decode_jwt_from_cookies (refresh )
243
243
)
244
- if location == "query_string" :
244
+ elif location == "query_string" :
245
245
get_encoded_token_functions .append (_decode_jwt_from_query_string )
246
- if location == "headers" :
246
+ elif location == "headers" :
247
247
get_encoded_token_functions .append (_decode_jwt_from_headers )
248
- if location == "json" :
248
+ elif location == "json" :
249
249
get_encoded_token_functions .append (lambda : _decode_jwt_from_json (refresh ))
250
+ else :
251
+ raise RuntimeError (f"'{ location } ' is not a valid location" )
250
252
251
253
# Try to find the token from one of these locations. It only needs to exist
252
254
# in one place to be valid (not every location).
0 commit comments