11from flask import jsonify
22
3+ from flask_jwt_extended .exceptions import JWTDecodeError , NoAuthorizationError , \
4+ InvalidHeaderError , WrongTokenError , RevokedTokenError , FreshTokenRequired
5+ from jwt import ExpiredSignatureError , InvalidTokenError
6+
37
48class JWTManager :
59 def __init__ (self , app = None ):
610 # Function that will be called to add custom user claims to a JWT.
711 self .user_claims_callback = lambda _ : {}
812
913 # Function that will be called when an expired token is received
10- self .expired_token_callback = lambda : (
14+ self ._expired_token_callback = lambda : (
1115 jsonify ({'msg' : 'Token has expired' }), 401
1216 )
1317
1418 # Function that will be called when an invalid token is received
15- self .invalid_token_callback = lambda err : (
19+ self ._invalid_token_callback = lambda err : (
1620 jsonify ({'msg' : err }), 422
1721 )
1822
1923 # Function that will be called when attempting to access a protected
2024 # endpoint without a valid token
21- self .unauthorized_callback = lambda : (
25+ self ._unauthorized_callback = lambda : (
2226 jsonify ({'msg' : 'Missing Authorization Header' }), 401
2327 )
2428
2529 # Function that will be called when attempting to access a fresh_jwt_required
2630 # endpoint with a valid token that is not fresh
27- self .needs_fresh_token_callback = lambda : (
31+ self ._needs_fresh_token_callback = lambda : (
2832 jsonify ({'msg' : 'Fresh token required' }), 401
2933 )
3034
3135 # Function that will be called when a revoked token attempts to access
3236 # a protected endpoint
33- self .revoked_token_callback = lambda : (
37+ self ._revoked_token_callback = lambda : (
3438 jsonify ({'msg' : 'Token has been revoked' }), 401
3539 )
3640
@@ -45,6 +49,38 @@ def init_app(self, app):
4549 """
4650 app .jwt_manager = self
4751
52+ @app .errorhandler (NoAuthorizationError )
53+ def handle_auth_error (e ):
54+ return self ._unauthorized_callback ()
55+
56+ @app .errorhandler (ExpiredSignatureError )
57+ def handle_expired_error (e ):
58+ return self ._expired_token_callback ()
59+
60+ @app .errorhandler (InvalidHeaderError )
61+ def handle_invalid_header_error (e ):
62+ return self ._invalid_token_callback (str (e ))
63+
64+ @app .errorhandler (InvalidTokenError )
65+ def handle_invalid_token_error (e ):
66+ return self ._invalid_token_callback (str (e ))
67+
68+ @app .errorhandler (JWTDecodeError )
69+ def handle_jwt_decode_error (e ):
70+ return self ._invalid_token_callback (str (e ))
71+
72+ @app .errorhandler (WrongTokenError )
73+ def handle_wrong_token_error (e ):
74+ return self ._invalid_token_callback (str (e ))
75+
76+ @app .errorhandler (RevokedTokenError )
77+ def hanlde_revoked_token_error (e ):
78+ return self ._revoked_token_callback ()
79+
80+ @app .errorhandler (FreshTokenRequired )
81+ def handle_fresh_token_required (e ):
82+ return self ._needs_fresh_token_callback ()
83+
4884 def user_claims_loader (self , callback ):
4985 """
5086 This sets the callback method for adding custom user claims to a JWT.
@@ -66,7 +102,7 @@ def expired_token_loader(self, callback):
66102
67103 Callback must be a function that takes zero arguments.
68104 """
69- self .expired_token_callback = callback
105+ self ._expired_token_callback = callback
70106 return callback
71107
72108 def invalid_token_loader (self , callback ):
@@ -79,7 +115,7 @@ def invalid_token_loader(self, callback):
79115 Callback must be a function that takes only one argument, which is the
80116 error message of why the token is invalid.
81117 """
82- self .invalid_token_callback = callback
118+ self ._invalid_token_callback = callback
83119 return callback
84120
85121 def unauthorized_loader (self , callback ):
@@ -92,7 +128,7 @@ def unauthorized_loader(self, callback):
92128 Callback must be a function that takes only one argument, which is the
93129 error message of why the token is invalid.
94130 """
95- self .unauthorized_callback = callback
131+ self ._unauthorized_callback = callback
96132 return callback
97133
98134 def needs_fresh_token_loader (self , callback ):
@@ -105,7 +141,7 @@ def needs_fresh_token_loader(self, callback):
105141
106142 Callback must be a function that takes no arguments.
107143 """
108- self .needs_fresh_token_callback = callback
144+ self ._needs_fresh_token_callback = callback
109145 return callback
110146
111147 def revoked_token_loader (self , callback ):
@@ -118,5 +154,5 @@ def revoked_token_loader(self, callback):
118154
119155 Callback must be a function that takes no arguments.
120156 """
121- self .revoked_token_callback = callback
157+ self ._revoked_token_callback = callback
122158 return callback
0 commit comments