Skip to content

Commit 005fca5

Browse files
committed
Preserve CSRF errors in all cases (refs #29)
1 parent be6d548 commit 005fca5

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

flask_jwt_extended/exceptions.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ class InvalidHeaderError(JWTExtendedException):
2828

2929
class NoAuthorizationError(JWTExtendedException):
3030
"""
31-
An error getting header information from a request
31+
An error raised when no authorization token was found in a protected endpoint
32+
"""
33+
pass
34+
35+
36+
class CSRFError(JWTExtendedException):
37+
"""
38+
An error with CSRF protection
3239
"""
3340
pass
3441

flask_jwt_extended/jwt_manager.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from flask import jsonify
22

33
from flask_jwt_extended.exceptions import JWTDecodeError, NoAuthorizationError, \
4-
InvalidHeaderError, WrongTokenError, RevokedTokenError, FreshTokenRequired
4+
InvalidHeaderError, WrongTokenError, RevokedTokenError, FreshTokenRequired, \
5+
CSRFError
56
from jwt import ExpiredSignatureError, InvalidTokenError
67

78

@@ -61,6 +62,10 @@ def init_app(self, app):
6162
def handle_auth_error(e):
6263
return self._unauthorized_callback(str(e))
6364

65+
@app.errorhandler(CSRFError)
66+
def handle_auth_error(e):
67+
return self._unauthorized_callback(str(e))
68+
6469
@app.errorhandler(ExpiredSignatureError)
6570
def handle_expired_error(e):
6671
return self._expired_token_callback()

flask_jwt_extended/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
get_csrf_header_name, get_jwt_header_name
2222
from flask_jwt_extended.exceptions import JWTEncodeError, JWTDecodeError, \
2323
InvalidHeaderError, NoAuthorizationError, WrongTokenError, \
24-
FreshTokenRequired
24+
FreshTokenRequired, CSRFError
2525
from flask_jwt_extended.blacklist import check_if_token_revoked, store_token
2626

2727

@@ -206,9 +206,9 @@ def _decode_jwt_from_cookies(type):
206206
if not isinstance(csrf_token_from_cookie, six.string_types):
207207
raise JWTDecodeError("Invalid claim: 'csrf' (must be a string)")
208208
if csrf_token_from_header is None:
209-
raise NoAuthorizationError("Missing CSRF token in headers")
209+
raise CSRFError("Missing CSRF token in headers")
210210
if not safe_str_cmp(csrf_token_from_header, csrf_token_from_cookie):
211-
raise NoAuthorizationError("CSRF double submit tokens do not match")
211+
raise CSRFError("CSRF double submit tokens do not match")
212212
return token
213213

214214

0 commit comments

Comments
 (0)