Skip to content

Commit 21f5c7f

Browse files
committed
Better errors when using cookie methods without configured to use cookies
refs #31
1 parent 8337bc1 commit 21f5c7f

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

flask_jwt_extended/utils.py

+12
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ def set_access_cookies(response, encoded_access_token):
388388
Takes a flask response object, and configures it to set the encoded access
389389
token in a cookie (as well as a csrf access cookie if enabled)
390390
"""
391+
if 'cookies' not in get_token_location():
392+
raise RuntimeWarning("set_access_cookies() called without "
393+
"'JWT_TOKEN_LOCATION' configured to use cookies")
394+
391395
# Set the access JWT in the cookie
392396
response.set_cookie(get_access_cookie_name(),
393397
value=encoded_access_token,
@@ -409,6 +413,10 @@ def set_refresh_cookies(response, encoded_refresh_token):
409413
Takes a flask response object, and configures it to set the encoded refresh
410414
token in a cookie (as well as a csrf refresh cookie if enabled)
411415
"""
416+
if 'cookies' not in get_token_location():
417+
raise RuntimeWarning("set_refresh_cookies() called without "
418+
"'JWT_TOKEN_LOCATION' configured to use cookies")
419+
412420
# Set the refresh JWT in the cookie
413421
response.set_cookie(get_refresh_cookie_name(),
414422
value=encoded_refresh_token,
@@ -431,6 +439,10 @@ def unset_jwt_cookies(response):
431439
cookies. Basically, this is a logout helper method if using cookies to store
432440
the JWT
433441
"""
442+
if 'cookies' not in get_token_location():
443+
raise RuntimeWarning("unset_refresh_cookies() called without "
444+
"'JWT_TOKEN_LOCATION' configured to use cookies")
445+
434446
response.set_cookie(get_refresh_cookie_name(),
435447
value='',
436448
expires=0,

tests/test_protected_endpoints.py

+35
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,41 @@ def test_different_headers(self):
349349
self.assertIn('msg', data)
350350
self.assertEqual(status, 401)
351351

352+
def test_cookie_methods_fail_with_headers_configured(self):
353+
app = Flask(__name__)
354+
app.config['JWT_TOKEN_LOCATION'] = ['headers']
355+
app.secret_key = 'super=secret'
356+
app.testing = True
357+
JWTManager(app)
358+
client = app.test_client()
359+
360+
@app.route('/login-bad', methods=['POST'])
361+
def bad_login():
362+
access_token = create_access_token('test')
363+
resp = jsonify({'login': True})
364+
set_access_cookies(resp, access_token)
365+
return resp, 200
366+
367+
@app.route('/refresh-bad', methods=['POST'])
368+
def bad_refresh():
369+
refresh_token = create_refresh_token('test')
370+
resp = jsonify({'login': True})
371+
set_refresh_cookies(resp, refresh_token)
372+
return resp, 200
373+
374+
@app.route('/logout-bad', methods=['POST'])
375+
def bad_logout():
376+
resp = jsonify({'logout': True})
377+
unset_jwt_cookies(resp)
378+
return resp, 200
379+
380+
with self.assertRaises(RuntimeWarning):
381+
client.post('/login-bad')
382+
with self.assertRaises(RuntimeWarning):
383+
client.post('/refresh-bad')
384+
with self.assertRaises(RuntimeWarning):
385+
client.post('/logout-bad')
386+
352387

353388
class TestEndpointsWithCookies(unittest.TestCase):
354389

0 commit comments

Comments
 (0)