|  | 
| 9 | 9 | from flask import request | 
| 10 | 10 | from flask import session | 
| 11 | 11 | from flask import redirect | 
|  | 12 | +from flask import jsonify | 
| 12 | 13 | from flask_oauthlib.client import OAuth | 
| 13 | 14 | from functools import wraps | 
| 14 | 15 | from six.moves.urllib.parse import urlencode | 
|  | 
| 30 | 31 | APP = Flask(__name__, static_url_path='/public', static_folder='./public') | 
| 31 | 32 | APP.secret_key = constants.SECRET_KEY | 
| 32 | 33 | APP.debug = True | 
|  | 34 | + | 
|  | 35 | + | 
|  | 36 | +# Format error response and append status code. | 
|  | 37 | +class AuthError(Exception): | 
|  | 38 | + def __init__(self, error, status_code): | 
|  | 39 | + self.error = error | 
|  | 40 | + self.status_code = status_code | 
|  | 41 | + | 
|  | 42 | + | 
|  | 43 | +@APP.errorhandler(Exception) | 
|  | 44 | +def handle_auth_error(ex): | 
|  | 45 | + response = jsonify(ex.error) | 
|  | 46 | + response.status_code = ex.status_code | 
|  | 47 | + return response | 
|  | 48 | + | 
| 33 | 49 | oauth = OAuth(APP) | 
| 34 | 50 | 
 | 
| 35 | 51 | 
 | 
| @@ -67,10 +83,7 @@ def home(): | 
| 67 | 83 | def callback_handling(): | 
| 68 | 84 |  resp = auth0.authorized_response() | 
| 69 | 85 |  if resp is None: | 
| 70 |  | - raise Exception('Access denied: reason=%s error=%s' % ( | 
| 71 |  | - request.args['error_reason'], | 
| 72 |  | - request.args['error_description'] | 
| 73 |  | - )) | 
|  | 86 | + raise AuthError({'code': request.args['error'], 'description': request.args['error_description']}, 401) | 
| 74 | 87 | 
 | 
| 75 | 88 |  # Obtain JWT and the keys to validate the signature | 
| 76 | 89 |  idToken = resp['id_token'] | 
|  | 
0 commit comments