33from functools import wraps
44import json
55from os import environ as env
6+ from werkzeug .exceptions import HTTPException
67
78from dotenv import load_dotenv , find_dotenv
89from flask import Flask
910from flask import jsonify
1011from flask import redirect
1112from flask import render_template
12- from flask import request
1313from flask import session
1414from flask import url_for
15- from flask_oauthlib .client import OAuth
15+ from authlib . flask .client import OAuth
1616from six .moves .urllib .parse import urlencode
1717import requests
1818
2626AUTH0_CLIENT_ID = env .get (constants .AUTH0_CLIENT_ID )
2727AUTH0_CLIENT_SECRET = env .get (constants .AUTH0_CLIENT_SECRET )
2828AUTH0_DOMAIN = env .get (constants .AUTH0_DOMAIN )
29+ AUTH0_BASE_URL = 'https://' + AUTH0_DOMAIN
2930AUTH0_AUDIENCE = env .get (constants .AUTH0_AUDIENCE )
3031if AUTH0_AUDIENCE is '' :
31- AUTH0_AUDIENCE = 'https://' + AUTH0_DOMAIN + '/userinfo'
32+ AUTH0_AUDIENCE = AUTH0_BASE_URL + '/userinfo'
3233
33- APP = Flask (__name__ , static_url_path = '/public' , static_folder = './public' )
34- APP .secret_key = constants .SECRET_KEY
35- APP .debug = True
34+ app = Flask (__name__ , static_url_path = '/public' , static_folder = './public' )
35+ app .secret_key = constants .SECRET_KEY
36+ app .debug = True
3637
3738
38- # Format error response and append status code.
39- class AuthError (Exception ):
40- def __init__ (self , error , status_code ):
41- self .error = error
42- self .status_code = status_code
43-
44-
45- @APP .errorhandler (AuthError )
39+ @app .errorhandler (Exception )
4640def handle_auth_error (ex ):
47- response = jsonify (ex . error )
48- response .status_code = ex .status_code
41+ response = jsonify (message = str ( ex ) )
42+ response .status_code = ( ex .code if isinstance ( ex , HTTPException ) else 500 )
4943 return response
5044
5145
52- @APP .errorhandler (Exception )
53- def handle_auth_error (ex ):
54- response = jsonify (message = ex .message )
55- return response
46+ oauth = OAuth (app )
5647
57- oauth = OAuth (APP )
58-
59-
60- auth0 = oauth .remote_app (
48+ auth0 = oauth .register (
6149 'auth0' ,
62- consumer_key = AUTH0_CLIENT_ID ,
63- consumer_secret = AUTH0_CLIENT_SECRET ,
64- request_token_params = {
50+ client_id = AUTH0_CLIENT_ID ,
51+ client_secret = AUTH0_CLIENT_SECRET ,
52+ api_base_url = AUTH0_BASE_URL ,
53+ access_token_url = AUTH0_BASE_URL + '/oauth/token' ,
54+ authorize_url = AUTH0_BASE_URL + '/authorize' ,
55+ client_kwargs = {
6556 'scope' : 'openid profile' ,
66- 'audience' : AUTH0_AUDIENCE
6757 },
68- base_url = 'https://%s' % AUTH0_DOMAIN ,
69- access_token_method = 'POST' ,
70- access_token_url = '/oauth/token' ,
71- authorize_url = '/authorize' ,
7258)
7359
7460
@@ -78,23 +64,21 @@ def decorated(*args, **kwargs):
7864 if constants .PROFILE_KEY not in session :
7965 return redirect ('/login' )
8066 return f (* args , ** kwargs )
67+
8168 return decorated
8269
8370
8471# Controllers API
85- @APP .route ('/' )
72+ @app .route ('/' )
8673def home ():
8774 return render_template ('home.html' )
8875
8976
90- @APP .route ('/callback' )
77+ @app .route ('/callback' )
9178def callback_handling ():
92- resp = auth0 .authorized_response ()
93- if resp is None :
94- raise AuthError ({'code' : request .args ['error' ],
95- 'description' : request .args ['error_description' ]}, 401 )
79+ resp = auth0 .authorize_access_token ()
9680
97- url = 'https://' + AUTH0_DOMAIN + '/userinfo'
81+ url = AUTH0_BASE_URL + '/userinfo'
9882 headers = {'authorization' : 'Bearer ' + resp ['access_token' ]}
9983 resp = requests .get (url , headers = headers )
10084 userinfo = resp .json ()
@@ -110,19 +94,19 @@ def callback_handling():
11094 return redirect ('/dashboard' )
11195
11296
113- @APP .route ('/login' )
97+ @app .route ('/login' )
11498def login ():
115- return auth0 .authorize ( callback = AUTH0_CALLBACK_URL )
99+ return auth0 .authorize_redirect ( redirect_uri = AUTH0_CALLBACK_URL , audience = AUTH0_AUDIENCE )
116100
117101
118- @APP .route ('/logout' )
102+ @app .route ('/logout' )
119103def logout ():
120104 session .clear ()
121105 params = {'returnTo' : url_for ('home' , _external = True ), 'client_id' : AUTH0_CLIENT_ID }
122- return redirect (auth0 .base_url + '/v2/logout?' + urlencode (params ))
106+ return redirect (auth0 .api_base_url + '/v2/logout?' + urlencode (params ))
123107
124108
125- @APP .route ('/dashboard' )
109+ @app .route ('/dashboard' )
126110@requires_auth
127111def dashboard ():
128112 return render_template ('dashboard.html' ,
@@ -131,4 +115,4 @@ def dashboard():
131115
132116
133117if __name__ == "__main__" :
134- APP .run (host = '0.0.0.0' , port = env .get ('PORT' , 3000 ))
118+ app .run (host = '0.0.0.0' , port = env .get ('PORT' , 3000 ))
0 commit comments