-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
233 lines (181 loc) · 7.7 KB
/
main.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import base64
from cgitb import reset
import hashlib
import requests
import secrets
import os
import logging
from datetime import timedelta
import jwt
from flask import Blueprint, render_template, send_from_directory, redirect, request, session, g, url_for, current_app as app, jsonify
from flask_login import current_user, login_required, login_user, logout_user
from flask_wtf.csrf import validate_csrf
from _env import *
from models.user import User
main = Blueprint('main', __name__)
@main.route("/")
def home():
if current_user.is_authenticated:
return redirect(url_for("main.login"))
return render_template("login.html", user=current_user)
@main.before_request
def before_request():
session.permanent = True
session_timeout = int(APP_SESSION_TIMEOUT)
app.permanent_session_lifetime = timedelta(minutes=session_timeout)
session.modified = True
g.user = current_user
@main.route("/login", methods = ['GET', 'POST'])
def login():
session['app_state'] = secrets.token_urlsafe(64)
session['code_verifier'] = secrets.token_urlsafe(64)
# calculate code challenge
hashed = hashlib.sha256(session['code_verifier'].encode('ascii')).digest()
encoded = base64.urlsafe_b64encode(hashed)
code_challenge = encoded.decode('ascii').strip('=')
query_params = {'client_id': IDP_CLIENT_ID,
'redirect_uri': APP_REDIRECT_URI,
'scope': APP_SCOPES,
'state': session['app_state'],
'code_challenge': code_challenge,
'code_challenge_method': 'S256',
'response_type': 'code',
'response_mode': 'query'}
request_uri = "{base_url}?{query_params}".format(
base_url=IDP_AUTH_URI,
query_params=requests.compat.urlencode(query_params)
)
logging.debug("Redirect to IdP")
return redirect(request_uri)
@main.route("/callback")
def callback():
try:
# For non-prod environments, can turn off SSL cert verification
verify_ssl = True
verify_ssl_env_var = VERIFY_SSL.lower()
if verify_ssl_env_var == "false":
verify_ssl = False
logging.info(f"Verify SSL = {verify_ssl}")
logging.debug("Callback from IdP")
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
code = request.args.get("code")
app_state = request.args.get("state")
if app_state != session['app_state']:
raise Exception("The app state does not match")
if not code:
raise Exception(request.args.get("error_description"))
logging.debug("Verified IdP state and code")
query_params = {'grant_type': 'authorization_code',
'code': code,
'redirect_uri': APP_REDIRECT_URI,
'code_verifier': session['code_verifier'],
}
query_params = requests.compat.urlencode(query_params)
logging.debug("Request token from IdP")
exchange = requests.post(
IDP_TOKEN_URI,
headers=headers,
data=query_params,
auth=(IDP_CLIENT_ID, IDP_CLIENT_SECRET),
verify=verify_ssl
).json()
logging.debug(f"auth data = {exchange}")
# Get tokens and validate
if not exchange.get("token_type"):
raise Exception("Unsupported token type. Should be 'Bearer'")
access_token = exchange["access_token"]
id_token = exchange["id_token"] # Used for logout
logging.debug(f"access token from IdP: {access_token}")
# Decode access token
# TODO: Validate the access token
decoded_access_token = jwt.decode(access_token, options={"verify_signature": False})
logging.debug(f"decoded access token: {decoded_access_token}")
logging.debug("Request user info from IdP")
# Authorization flow successful, get userinfo and login user
userinfo_response = requests.get(
IDP_USERINFO_URI,
headers={'Authorization': f'Bearer {access_token}'},
verify=verify_ssl
).json()
user_sub = userinfo_response["sub"]
user_email = userinfo_response.get("email", "")
if not user_email:
if IDP_DOMAIN == "login.microsoftonline.com":
user_name = decoded_access_token["upn"]
else:
raise Exception("Okta email or username need to be defined")
else:
user_name = userinfo_response["email"]
logging.debug(f"user info from IdP: sub[{user_sub}], user_name[{user_name}], email[{user_email}]")
idp_user = User(
id=user_sub, name=user_name, email=user_email, idp_token=id_token
)
# Check if the user has logged in previously.
app_user = User.get(idp_user.id)
if not app_user:
idp_user.initial_login()
app_user = idp_user
else:
# There may be updates from the IdP so go with that data on the update.
app_user.name = idp_user.name
app_user.email = idp_user.email
app_user.idp_token = idp_user.idp_token
app_user.update_login()
# Testing session across multiple workers.
#logging.info(f"/authorization-code/callback: worker_pid[{os.getpid()}], {app_user}")
# TODO: Add Onboarding API for checking if this id.me user is valid for our apps
# Logs in the user, saves user info to DB, and stores user info in a cookie.
login_user(app_user)
return redirect(url_for("main.profile"))
except Exception as ex:
logging.error(f"Callback exception: {ex}")
error_msg = str(ex)
session["errorMsg"] = error_msg
return redirect(url_for("main.error"))
@main.route("/profile")
@login_required
def profile():
try:
logging.info(f"/profile: worker_pid[{os.getpid()}], {current_user}")
except Exception as e:
logging.error(f"/profile: worker_pid[{os.getpid()}], Exception msg: " + repr(e))
return render_template("profile.html", user=current_user)
@main.route("/error", methods=['GET'])
def error():
error_msg = session["errorMsg"]
session["errorMsg"] = ""
return render_template("error.html", errorMsg=error_msg)
@main.route('/updateprofile')
@login_required
def update_profile():
nav_pane_pos = request.args.get("NavPanePos", None)
high_contrast = request.args.get("HighContrast", None)
current_user.update_pbi_prefs(nav_pane_pos, high_contrast)
return redirect(url_for("main.dashboard"))
@main.route('/dashboard')
@login_required
def dashboard():
return render_template("dashboard.html", user=current_user)
@main.route('/favicon.ico', methods=['GET'])
def getfavicon():
'''Returns path of the favicon to be rendered'''
return send_from_directory(os.path.join(main.root_path, 'static'), 'img/favicon.ico', mimetype='image/vnd.microsoft.icon')
@main.route("/logout")
@login_required
def logout():
# Get OAuth2 id_token and logout user from Identity Provider.
id_token = current_user.idp_token
main_url = url_for("main.home")
if id_token is None:
return redirect(main_url)
query_params = {
'id_token_hint': id_token,
'post_logout_redirect_uri': APP_LOGOUT_URI
}
request_uri = "{base_url}?{query_params}".format(
base_url=IDP_LOGOUT_URI,
query_params=requests.compat.urlencode(query_params)
)
logging.info(f"/logout: worker_pid[{os.getpid()}], {current_user}")
logout_user()
return redirect(request_uri)