Skip to content

Commit 1e2129e

Browse files
committed
Allow customizing the JSON error response's message key
1 parent 182abbf commit 1e2129e

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

docs/options.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ General Options:
4141
Defaults to ``'user_claims'``.
4242
``JWT_CLAIMS_IN_REFRESH_TOKEN`` If user claims should be included in refresh tokens.
4343
Defaults to ``False``.
44+
``JWT_ERROR_MESSAGE_KEY`` The key of the error message in a JSON error response when using
45+
the default error handlers.
46+
Defaults to ``'msg'``.
4447
================================= =========================================
4548

4649

flask_jwt_extended/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ def user_claims_in_refresh_token(self):
255255
def exempt_methods(self):
256256
return {"OPTIONS"}
257257

258+
@property
259+
def error_msg_key(self):
260+
return current_app.config['JWT_ERROR_MESSAGE_KEY']
261+
258262
@property
259263
def json_encoder(self):
260264
return current_app.json_encoder

flask_jwt_extended/default_callbacks.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"""
99
from flask import jsonify
1010

11+
from flask_jwt_extended.config import config
12+
1113

1214
def default_user_claims_callback(userdata):
1315
"""
@@ -37,7 +39,7 @@ def default_expired_token_callback():
3739
By default, if an expired token attempts to access a protected endpoint,
3840
we return a generic error message with a 401 status
3941
"""
40-
return jsonify({'msg': 'Token has expired'}), 401
42+
return jsonify({config.error_msg_key: 'Token has expired'}), 401
4143

4244

4345
def default_invalid_token_callback(error_string):
@@ -47,7 +49,7 @@ def default_invalid_token_callback(error_string):
4749
4850
:param error_string: String indicating why the token is invalid
4951
"""
50-
return jsonify({'msg': error_string}), 422
52+
return jsonify({config.error_msg_key: error_string}), 422
5153

5254

5355
def default_unauthorized_callback(error_string):
@@ -57,23 +59,23 @@ def default_unauthorized_callback(error_string):
5759
5860
:param error_string: String indicating why this request is unauthorized
5961
"""
60-
return jsonify({'msg': error_string}), 401
62+
return jsonify({config.error_msg_key: error_string}), 401
6163

6264

6365
def default_needs_fresh_token_callback():
6466
"""
6567
By default, if a non-fresh jwt is used to access a ```fresh_jwt_required```
6668
endpoint, we return a general error message with a 401 status code
6769
"""
68-
return jsonify({'msg': 'Fresh token required'}), 401
70+
return jsonify({config.error_msg_key: 'Fresh token required'}), 401
6971

7072

7173
def default_revoked_token_callback():
7274
"""
7375
By default, if a revoked token is used to access a protected endpoint, we
7476
return a general error message with a 401 status code
7577
"""
76-
return jsonify({'msg': 'Token has been revoked'}), 401
78+
return jsonify({config.error_msg_key: 'Token has been revoked'}), 401
7779

7880

7981
def default_user_loader_error_callback(identity):
@@ -82,7 +84,7 @@ def default_user_loader_error_callback(identity):
8284
function returns None, we return a general error message with a 401
8385
status code
8486
"""
85-
return jsonify({'msg': "Error loading the user {}".format(identity)}), 401
87+
return jsonify({config.error_msg_key: "Error loading the user {}".format(identity)}), 401
8688

8789

8890
def default_claims_verification_callback(user_claims):
@@ -97,4 +99,4 @@ def default_claims_verification_failed_callback():
9799
By default, if the user claims verification failed, we return a generic
98100
error message with a 400 status code
99101
"""
100-
return jsonify({'msg': 'User claims verification failed'}), 400
102+
return jsonify({config.error_msg_key: 'User claims verification failed'}), 400

flask_jwt_extended/jwt_manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ def _set_default_configuration_options(app):
189189

190190
app.config.setdefault('JWT_CLAIMS_IN_REFRESH_TOKEN', False)
191191

192+
app.config.setdefault('JWT_ERROR_MESSAGE_KEY', 'msg')
193+
192194
def user_claims_loader(self, callback):
193195
"""
194196
This decorator sets the callback function for adding custom claims to an

tests/test_config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def test_default_configs(app):
6565

6666
assert config.json_encoder is app.json_encoder
6767

68+
assert config.error_msg_key == 'msg'
69+
6870

6971
def test_override_configs(app):
7072
app.config['JWT_TOKEN_LOCATION'] = ['cookies', 'query_string']
@@ -104,6 +106,8 @@ def test_override_configs(app):
104106

105107
app.config['JWT_CLAIMS_IN_REFRESH_TOKEN'] = True
106108

109+
app.config['JWT_ERROR_MESSAGE_KEY'] = 'message'
110+
107111
class CustomJSONEncoder(JSONEncoder):
108112
pass
109113

@@ -156,6 +160,8 @@ class CustomJSONEncoder(JSONEncoder):
156160

157161
assert config.json_encoder is CustomJSONEncoder
158162

163+
assert config.error_msg_key == 'message'
164+
159165

160166
def test_tokens_never_expire(app):
161167
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = False

tests/test_headers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,9 @@ def custom_response(err_str):
9090
response = test_client.get('/protected', headers=None)
9191
assert response.status_code == 201
9292
assert response.get_json() == {'foo': "bar"}
93+
94+
95+
def test_custom_error_msg_key(app):
96+
app.config['JWT_ERROR_MESSAGE_KEY'] = 'message'
97+
response = app.test_client().get('/protected', headers=None)
98+
assert response.get_json() == {'message': 'Missing Authorization Header'}

0 commit comments

Comments
 (0)