@@ -47,8 +47,7 @@ def verify_jwt_in_request(optional=False, fresh=False, refresh=False, locations=
47
47
Defaults to ``False``.
48
48
49
49
:param refresh:
50
- If ``True``, require a refresh JWT to be verified. If ``False`` require an access
51
- JWT to be verified. Defaults to ``False``.
50
+ If ``True``, require a refresh JWT to be verified.
52
51
53
52
:param locations:
54
53
A list of locations to look for the JWT in this request, for example:
@@ -61,9 +60,11 @@ def verify_jwt_in_request(optional=False, fresh=False, refresh=False, locations=
61
60
62
61
try :
63
62
if refresh :
64
- jwt_data , jwt_header = _decode_jwt_from_request ("refresh" , locations , fresh )
63
+ jwt_data , jwt_header = _decode_jwt_from_request (
64
+ locations , fresh , refresh = True
65
+ )
65
66
else :
66
- jwt_data , jwt_header = _decode_jwt_from_request ("access" , locations , fresh )
67
+ jwt_data , jwt_header = _decode_jwt_from_request (locations , fresh )
67
68
except (NoAuthorizationError , InvalidHeaderError ):
68
69
if not optional :
69
70
raise
@@ -170,15 +171,15 @@ def _decode_jwt_from_headers():
170
171
return encoded_token , None
171
172
172
173
173
- def _decode_jwt_from_cookies (token_type ):
174
- if token_type == "access" :
175
- cookie_key = config .access_cookie_name
176
- csrf_header_key = config .access_csrf_header_name
177
- csrf_field_key = config .access_csrf_field_name
178
- else :
174
+ def _decode_jwt_from_cookies (refresh ):
175
+ if refresh :
179
176
cookie_key = config .refresh_cookie_name
180
177
csrf_header_key = config .refresh_csrf_header_name
181
178
csrf_field_key = config .refresh_csrf_field_name
179
+ else :
180
+ cookie_key = config .access_cookie_name
181
+ csrf_header_key = config .access_csrf_header_name
182
+ csrf_field_key = config .access_csrf_field_name
182
183
183
184
encoded_token = request .cookies .get (cookie_key )
184
185
if not encoded_token :
@@ -205,15 +206,15 @@ def _decode_jwt_from_query_string():
205
206
return encoded_token , None
206
207
207
208
208
- def _decode_jwt_from_json (token_type ):
209
+ def _decode_jwt_from_json (refresh ):
209
210
content_type = request .content_type or ""
210
211
if not content_type .startswith ("application/json" ):
211
212
raise NoAuthorizationError ("Invalid content-type. Must be application/json." )
212
213
213
- if token_type == "access" :
214
- token_key = config .json_key
215
- else :
214
+ if refresh :
216
215
token_key = config .refresh_json_key
216
+ else :
217
+ token_key = config .json_key
217
218
218
219
try :
219
220
encoded_token = request .json .get (token_key , None )
@@ -225,7 +226,7 @@ def _decode_jwt_from_json(token_type):
225
226
return encoded_token , None
226
227
227
228
228
- def _decode_jwt_from_request (token_type , locations , fresh ):
229
+ def _decode_jwt_from_request (locations , fresh , refresh = False ):
229
230
# All the places we can get a JWT from in this request
230
231
get_encoded_token_functions = []
231
232
@@ -238,16 +239,14 @@ def _decode_jwt_from_request(token_type, locations, fresh):
238
239
for location in locations :
239
240
if location == "cookies" :
240
241
get_encoded_token_functions .append (
241
- lambda : _decode_jwt_from_cookies (token_type )
242
+ lambda : _decode_jwt_from_cookies (refresh )
242
243
)
243
244
if location == "query_string" :
244
245
get_encoded_token_functions .append (_decode_jwt_from_query_string )
245
246
if location == "headers" :
246
247
get_encoded_token_functions .append (_decode_jwt_from_headers )
247
248
if location == "json" :
248
- get_encoded_token_functions .append (
249
- lambda : _decode_jwt_from_json (token_type )
250
- )
249
+ get_encoded_token_functions .append (lambda : _decode_jwt_from_json (refresh ))
251
250
252
251
# Try to find the token from one of these locations. It only needs to exist
253
252
# in one place to be valid (not every location).
@@ -277,10 +276,10 @@ def _decode_jwt_from_request(token_type, locations, fresh):
277
276
raise NoAuthorizationError (errors [0 ])
278
277
279
278
# Additional verifications provided by this extension
280
- verify_token_type (decoded_token , expected_type = token_type )
279
+ verify_token_type (decoded_token , refresh )
281
280
if fresh :
282
281
_verify_token_is_fresh (jwt_header , decoded_token )
283
- verify_token_not_blocklisted (jwt_header , decoded_token , token_type )
282
+ verify_token_not_blocklisted (jwt_header , decoded_token )
284
283
custom_verification_for_token (jwt_header , decoded_token )
285
284
286
285
return decoded_token , jwt_header
0 commit comments