2222
2323# Format error response and append status code.
2424class AuthError (Exception ):
25+ """
26+ An AuthError is raised whenever the authentication failed.
27+ """
2528 def __init__ (self , error , status_code ):
29+ super ().__init__ ()
2630 self .error = error
2731 self .status_code = status_code
2832
2933
3034@APP .errorhandler (AuthError )
3135def handle_auth_error (ex ):
36+ """
37+ serializes the given AuthError as json and sets the response status code accordingly.
38+ :param ex: an auth error
39+ :return: json serialized ex response
40+ """
3241 response = jsonify (ex .error )
3342 response .status_code = ex .status_code
3443 return response
@@ -50,10 +59,10 @@ def get_token_auth_header():
5059 "description" :
5160 "Authorization header must start with"
5261 " Bearer" }, 401 )
53- elif len (parts ) == 1 :
62+ if len (parts ) == 1 :
5463 raise AuthError ({"code" : "invalid_header" ,
5564 "description" : "Token not found" }, 401 )
56- elif len (parts ) > 2 :
65+ if len (parts ) > 2 :
5766 raise AuthError ({"code" : "invalid_header" ,
5867 "description" :
5968 "Authorization header must be"
@@ -78,21 +87,21 @@ def requires_scope(required_scope):
7887 return False
7988
8089
81- def requires_auth (f ):
90+ def requires_auth (func ):
8291 """Determines if the access token is valid
8392 """
84- @wraps (f )
93+ @wraps (func )
8594 def decorated (* args , ** kwargs ):
8695 token = get_token_auth_header ()
8796 jsonurl = urlopen ("https://" + AUTH0_DOMAIN + "/.well-known/jwks.json" )
8897 jwks = json .loads (jsonurl .read ())
8998 try :
9099 unverified_header = jwt .get_unverified_header (token )
91- except jwt .JWTError :
100+ except jwt .JWTError as jwt_error :
92101 raise AuthError ({"code" : "invalid_header" ,
93102 "description" :
94103 "Invalid header. "
95- "Use an RS256 signed JWT Access Token" }, 401 )
104+ "Use an RS256 signed JWT Access Token" }, 401 ) from jwt_error
96105 if unverified_header ["alg" ] == "HS256" :
97106 raise AuthError ({"code" : "invalid_header" ,
98107 "description" :
@@ -117,22 +126,22 @@ def decorated(*args, **kwargs):
117126 audience = API_IDENTIFIER ,
118127 issuer = "https://" + AUTH0_DOMAIN + "/"
119128 )
120- except jwt .ExpiredSignatureError :
129+ except jwt .ExpiredSignatureError as expired_sign_error :
121130 raise AuthError ({"code" : "token_expired" ,
122- "description" : "token is expired" }, 401 )
123- except jwt .JWTClaimsError :
131+ "description" : "token is expired" }, 401 ) from expired_sign_error
132+ except jwt .JWTClaimsError as jwt_claims_error :
124133 raise AuthError ({"code" : "invalid_claims" ,
125134 "description" :
126135 "incorrect claims,"
127- " please check the audience and issuer" }, 401 )
128- except Exception :
136+ " please check the audience and issuer" }, 401 ) from jwt_claims_error
137+ except Exception as exc :
129138 raise AuthError ({"code" : "invalid_header" ,
130139 "description" :
131140 "Unable to parse authentication"
132- " token." }, 401 )
141+ " token." }, 401 ) from exc
133142
134143 _request_ctx_stack .top .current_user = payload
135- return f (* args , ** kwargs )
144+ return func (* args , ** kwargs )
136145 raise AuthError ({"code" : "invalid_header" ,
137146 "description" : "Unable to find appropriate key" }, 401 )
138147 return decorated
0 commit comments