Skip to content

Commit b78b1fc

Browse files
committed
Better default messages for NoAuthorizationError (refs #16)
1 parent c96bb90 commit b78b1fc

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

docs/changing_default_behavior.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Possible loader functions are:
2727
- Takes one argument - an error string indicating why the token is invalid
2828
* - **unauthorized_loader**
2929
- Function to call when a request with no JWT accesses a protected endpoint
30-
- None
30+
- Takes one argument - an error string indicating why the request in unauthorized
3131
* - **needs_fresh_token_loader**
3232
- Function to call when a non-fresh token accesses a **fresh_jwt_required** endpoint
3333
- None

flask_jwt_extended/jwt_manager.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def __init__(self, app=None):
2525

2626
# Function that will be called when attempting to access a protected
2727
# endpoint without a valid token
28-
self._unauthorized_callback = lambda: (
29-
jsonify({'msg': 'Missing Authorization Header'}), 401
28+
self._unauthorized_callback = lambda err: (
29+
jsonify({'msg': err}), 401
3030
)
3131

3232
# Function that will be called when attempting to access a fresh_jwt_required
@@ -54,7 +54,7 @@ def init_app(self, app):
5454

5555
@app.errorhandler(NoAuthorizationError)
5656
def handle_auth_error(e):
57-
return self._unauthorized_callback()
57+
return self._unauthorized_callback(str(e))
5858

5959
@app.errorhandler(ExpiredSignatureError)
6060
def handle_expired_error(e):

tests/test_jwt_manager.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_default_invalid_token_callback(self):
5656
def test_default_unauthorized_callback(self):
5757
with self.app.test_request_context():
5858
m = JWTManager(self.app)
59-
result = m._unauthorized_callback()
59+
result = m._unauthorized_callback("Missing Authorization Header")
6060
status_code, data = self._parse_callback_result(result)
6161

6262
self.assertEqual(status_code, 401)
@@ -124,10 +124,10 @@ def test_custom_unauthorized_callback(self):
124124
m = JWTManager(self.app)
125125

126126
@m.unauthorized_loader
127-
def custom_unauthorized():
128-
return jsonify({"err": "GOTTA LOGIN FOOL"}), 200
127+
def custom_unauthorized(err_str):
128+
return jsonify({"err": err_str}), 200
129129

130-
result = m._unauthorized_callback()
130+
result = m._unauthorized_callback("GOTTA LOGIN FOOL")
131131
status_code, data = self._parse_callback_result(result)
132132

133133
self.assertEqual(status_code, 200)

0 commit comments

Comments
 (0)