@@ -47,8 +47,7 @@ def verify_jwt_in_request(optional=False, fresh=False, refresh=False, locations=
4747 Defaults to ``False``.
4848
4949 :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.
5251
5352 :param locations:
5453 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=
6160
6261 try :
6362 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+ )
6566 else :
66- jwt_data , jwt_header = _decode_jwt_from_request ("access" , locations , fresh )
67+ jwt_data , jwt_header = _decode_jwt_from_request (locations , fresh )
6768 except (NoAuthorizationError , InvalidHeaderError ):
6869 if not optional :
6970 raise
@@ -170,15 +171,15 @@ def _decode_jwt_from_headers():
170171 return encoded_token , None
171172
172173
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 :
179176 cookie_key = config .refresh_cookie_name
180177 csrf_header_key = config .refresh_csrf_header_name
181178 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
182183
183184 encoded_token = request .cookies .get (cookie_key )
184185 if not encoded_token :
@@ -205,15 +206,15 @@ def _decode_jwt_from_query_string():
205206 return encoded_token , None
206207
207208
208- def _decode_jwt_from_json (token_type ):
209+ def _decode_jwt_from_json (refresh ):
209210 content_type = request .content_type or ""
210211 if not content_type .startswith ("application/json" ):
211212 raise NoAuthorizationError ("Invalid content-type. Must be application/json." )
212213
213- if token_type == "access" :
214- token_key = config .json_key
215- else :
214+ if refresh :
216215 token_key = config .refresh_json_key
216+ else :
217+ token_key = config .json_key
217218
218219 try :
219220 encoded_token = request .json .get (token_key , None )
@@ -225,7 +226,7 @@ def _decode_jwt_from_json(token_type):
225226 return encoded_token , None
226227
227228
228- def _decode_jwt_from_request (token_type , locations , fresh ):
229+ def _decode_jwt_from_request (locations , fresh , refresh = False ):
229230 # All the places we can get a JWT from in this request
230231 get_encoded_token_functions = []
231232
@@ -238,16 +239,14 @@ def _decode_jwt_from_request(token_type, locations, fresh):
238239 for location in locations :
239240 if location == "cookies" :
240241 get_encoded_token_functions .append (
241- lambda : _decode_jwt_from_cookies (token_type )
242+ lambda : _decode_jwt_from_cookies (refresh )
242243 )
243244 if location == "query_string" :
244245 get_encoded_token_functions .append (_decode_jwt_from_query_string )
245246 if location == "headers" :
246247 get_encoded_token_functions .append (_decode_jwt_from_headers )
247248 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 ))
251250
252251 # Try to find the token from one of these locations. It only needs to exist
253252 # in one place to be valid (not every location).
@@ -277,10 +276,10 @@ def _decode_jwt_from_request(token_type, locations, fresh):
277276 raise NoAuthorizationError (errors [0 ])
278277
279278 # Additional verifications provided by this extension
280- verify_token_type (decoded_token , expected_type = token_type )
279+ verify_token_type (decoded_token , refresh )
281280 if fresh :
282281 _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 )
284283 custom_verification_for_token (jwt_header , decoded_token )
285284
286285 return decoded_token , jwt_header
0 commit comments