-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
138 lines (102 loc) · 3.49 KB
/
server.py
File metadata and controls
138 lines (102 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""Python Flask WebApp Auth0 integration example
"""
from functools import wraps
import json
from os import environ as env
from werkzeug.exceptions import HTTPException
from dotenv import load_dotenv, find_dotenv
from flask import Flask
from flask import jsonify
from flask import redirect
from flask import render_template
from flask import session
from flask import url_for
from authlib.integrations.flask_client import OAuth
from six.moves.urllib.parse import urlencode
app = Flask(__name__, static_url_path='/public', static_folder='./public')
import constants
ENV_FILE = find_dotenv()
if ENV_FILE:
load_dotenv(ENV_FILE)
AUTH0_CALLBACK_URL = env.get('AUTH0_CALLBACK_URL')
AUTH0_CLIENT_ID = env.get('AUTH0_CLIENT_ID')
AUTH0_CLIENT_SECRET = env.get('AUTH0_CLIENT_SECRET')
AUTH0_DOMAIN = env.get('AUTH0_DOMAIN')
AUTH0_BASE_URL = 'https://{}'.format(AUTH0_DOMAIN)
AUTH0_AUDIENCE = env.get('AUTH0_AUDIENCE')
SECRET_KEY = env.get('SECRET_KEY')
PROFILE_KEY = env.get('PROFILE_KEY')
JWT_PAYLOAD = env.get('JWT_PAYLOAD')
#app = Flask(__name__, static_url_path='/public', static_folder='./public')
#app.secret_key = constants.SECRET_KEY
app.secret_key = SECRET_KEY
app.debug = True
auth0_data = None
@app.errorhandler(Exception)
def handle_auth_error(ex):
response = jsonify(message=str(ex))
response.status_code = (ex.code if isinstance(ex, HTTPException) else 500)
return response
oauth = OAuth(app)
auth0 = oauth.register(
'auth0',
client_id=AUTH0_CLIENT_ID,
client_secret=AUTH0_CLIENT_SECRET,
api_base_url=AUTH0_BASE_URL,
access_token_url='{}/oauth/token'.format(AUTH0_BASE_URL),
authorize_url='{}/authorize'.format(AUTH0_BASE_URL),
client_kwargs={
'scope': 'openid profile email',
},
)
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
if PROFILE_KEY not in session:
return redirect('/login')
return f(*args, **kwargs)
return decorated
# Controllers API
@app.route('/')
def home():
return render_template('home.html')
@app.route('/callback')
def callback_handling():
token = auth0.authorize_access_token()
resp = auth0.get('userinfo')
userinfo = resp.json()
session[JWT_PAYLOAD] = userinfo
session[PROFILE_KEY] = {
'user_id': userinfo['sub'],
'name': userinfo['name'],
'picture': userinfo['picture']
}
return redirect('/dashboard')
@app.route('/login')
def login():
return auth0.authorize_redirect(redirect_uri=AUTH0_CALLBACK_URL, audience=AUTH0_AUDIENCE)
@app.route('/logout')
def logout():
session.clear()
params = {'returnTo': url_for('home', _external=True), 'client_id': AUTH0_CLIENT_ID}
return redirect('{}/v2/logout?{}'.format(auth0.api_base_url, urlencode(params)))
@app.route('/dashboard')
@requires_auth
def dashboard():
auth0_data = auth0.__dict__
if 'email_verified' in session[JWT_PAYLOAD]:
email_verified = session[JWT_PAYLOAD]['email_verified']
else:
email_verified = False
try:
return render_template( 'dashboard.html',
email_verified=email_verfiied,
userinfo=session[PROFILE_KEY],
userinfo_pretty=json.dumps(session[JWT_PAYLOAD], indent=4))
except Exception as e:
##
## this can happen when reciving profile data from SAML connecitons
##
return render_template('error.html', message=e)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=env.get('PORT', 3000))