-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivvy_onboarding_ux.py
665 lines (560 loc) · 22.3 KB
/
divvy_onboarding_ux.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
"""
Overengineered web form to facilitate onboarding users to BILL Spend & Expense
"""
from datetime import datetime, timezone
from email.headerregistry import Address
from re import fullmatch
from typing import Any, Dict, Union
from authlib.integrations.flask_client import OAuth # type: ignore
from flask import Flask, Response, redirect, render_template, request, session, url_for
from flask.helpers import get_debug_flag
from google.auth.transport import requests
from google.oauth2 import id_token
from ldap3 import Connection, Server
from requests import delete, get, post
import sentry_sdk
from sentry_sdk import capture_message, set_user
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.pure_eval import PureEvalIntegration
from werkzeug.exceptions import BadRequest, InternalServerError, Unauthorized
def traces_sampler(sampling_context: Dict[str, Dict[str, str]]) -> bool:
"""
Ignore ping events, sample all other events
"""
try:
request_uri = sampling_context["wsgi_environ"]["REQUEST_URI"]
except KeyError:
return False
return request_uri != "/ping"
sentry_sdk.init(
debug=get_debug_flag(),
integrations=[
FlaskIntegration(),
PureEvalIntegration(),
],
traces_sampler=traces_sampler,
attach_stacktrace=True,
max_request_body_size="always",
in_app_include=[
"divvy_onboarding_ux",
],
profiles_sample_rate=1.0,
)
app = Flask(__name__)
app.config.from_prefixed_env()
oauth = OAuth(app)
oauth.register(
name="keycloak",
server_metadata_url=app.config["KEYCLOAK_METADATA_URL"],
client_kwargs={"scope": "openid email profile"},
)
oauth.register(
name="google",
server_metadata_url="https://accounts.google.com/.well-known/openid-configuration",
client_kwargs={"scope": "openid email"},
)
oauth.register(
name="microsoft",
server_metadata_url=app.config["MICROSOFT_METADATA_URL"],
client_kwargs={"scope": "openid email"},
)
@app.get("/")
def index() -> Any:
"""
Generates the main form or messaging if the user shouldn't fill it out
"""
if "user_state" not in session:
return oauth.keycloak.authorize_redirect(url_for("login", _external=True))
set_user(
{
"id": session["user_id"],
"username": session["username"],
"email": session["email_address"],
"ip_address": request.remote_addr,
}
)
if session["user_state"] == "provisioned":
session.clear()
return render_template("provisioned.html")
if session["user_state"] == "ineligible":
session.clear()
return render_template("ineligible.html")
if session["user_state"] == "requested":
return render_template("submitted.html")
apiary_managers_response = get(
url=app.config["APIARY_URL"] + "/api/v1/users/managers",
headers={
"Authorization": "Bearer " + app.config["APIARY_TOKEN"],
"Accept": "application/json",
},
timeout=(5, 5),
)
managers = {}
if apiary_managers_response.status_code == 200:
apiary_managers_json = apiary_managers_response.json()
for manager in apiary_managers_json["users"]:
managers[manager["id"]] = manager["full_name"]
else:
raise InternalServerError("Unable to load managers from Apiary")
return render_template(
"form.html",
elm_model={
"firstName": session["first_name"],
"lastName": session["last_name"],
"emailAddress": session["email_address"],
"emailVerified": session["email_verified"],
"managerId": session["manager_id"],
"managerOptions": managers,
"selfId": session["user_id"],
"addressLineOne": session["address_line_one"],
"addressLineTwo": session["address_line_two"],
"city": session["city"],
"state": session["address_state"],
"zip": session["zip_code"],
"googleMapsApiKey": app.config["GOOGLE_MAPS_FRONTEND_API_KEY"],
"googleClientId": app.config["GOOGLE_CLIENT_ID"],
"googleOneTapLoginUri": url_for("verify_google_onetap", _external=True),
},
)
@app.get("/login")
def login() -> Any: # pylint: disable=too-many-branches,too-many-statements,too-many-locals
"""
Handles the return from Keycloak and collects default values for the form
"""
token = oauth.keycloak.authorize_access_token()
userinfo = token["userinfo"]
username = userinfo["preferred_username"]
session["user_id"] = None
session["username"] = username
session["first_name"] = userinfo["given_name"]
session["last_name"] = userinfo["family_name"]
session["address_line_one"] = ""
session["address_line_two"] = ""
session["city"] = ""
session["address_state"] = None
session["zip_code"] = ""
session["manager_id"] = None
session["sub"] = userinfo["sub"]
if "googleWorkspaceAccount" in userinfo:
session["email_address"] = userinfo["googleWorkspaceAccount"]
session["email_verified"] = False
else:
session["email_address"] = userinfo["email"]
session["email_verified"] = False
set_user(
{
"id": session["user_id"],
"username": session["username"],
"email": session["email_address"],
"ip_address": request.remote_addr,
}
)
if "roles" in userinfo:
if "provisioned" in userinfo["roles"]:
session["user_state"] = "provisioned"
elif "eligible" in userinfo["roles"]:
session["user_state"] = "eligible"
else:
session["user_state"] = "ineligible"
else:
session["user_state"] = "ineligible"
if session["user_state"] == "ineligible" or session["user_state"] == "eligible":
apiary_user_response = get(
url=app.config["APIARY_URL"] + "/api/v1/users/" + username,
headers={
"Authorization": "Bearer " + app.config["APIARY_TOKEN"],
"Accept": "application/json",
},
params={"include": "roles,teams,assignments.travel"},
timeout=(5, 5),
)
if apiary_user_response.status_code == 200:
apiary_user = apiary_user_response.json()["user"]
session["user_id"] = apiary_user["id"]
set_user(
{
"id": session["user_id"],
"username": session["username"],
"email": session["email_address"],
"ip_address": request.remote_addr,
}
)
role_check = False
if "roles" in apiary_user and apiary_user["roles"] is not None:
for role in apiary_user["roles"]:
if role["name"] != "member" and role["name"] != "non-member":
role_check = True
travel_check = False
if "travel" in apiary_user and apiary_user["travel"] is not None:
for travel in apiary_user["travel"]:
if datetime.now(timezone.utc) < datetime.fromisoformat(
travel["travel"]["return_date"]
):
travel_check = True
if (
apiary_user["is_active"] # pylint: disable=too-many-boolean-expressions
and apiary_user["is_access_active"]
and apiary_user["signed_latest_agreement"]
and len(apiary_user["teams"]) > 0
and (role_check or travel_check)
):
session["user_state"] = "eligible"
if "manager" in apiary_user and apiary_user["manager"] is not None:
session["manager_id"] = apiary_user["manager"]["id"]
else:
session["manager_id"] = None
else:
raise InternalServerError("Unable to retrieve user information from Apiary")
if session["user_state"] == "eligible": # pylint: disable=too-many-nested-blocks
with sentry_sdk.start_span(op="ldap.connect"):
ldap = Connection(
Server("whitepages.gatech.edu"),
auto_bind=True,
)
with sentry_sdk.start_span(op="ldap.search"):
result = ldap.search(
search_base="dc=whitepages,dc=gatech,dc=edu",
search_filter="(uid=" + username + ")",
attributes=["postOfficeBox", "homePostalAddress"],
)
georgia_tech_mailbox = None
home_address = None
if result is True:
for entry in ldap.entries:
if (
"postOfficeBox" in entry
and entry["postOfficeBox"] is not None
and entry["postOfficeBox"].value is not None
):
georgia_tech_mailbox = entry["postOfficeBox"].value
if (
"homePostalAddress" in entry
and entry["homePostalAddress"] is not None
and entry["homePostalAddress"].value is not None
and entry["homePostalAddress"].value != "UNPUBLISHED INFO"
):
home_address = entry["homePostalAddress"].value
if georgia_tech_mailbox is not None:
session["address_line_one"] = "351 Ferst Dr NW"
session["address_line_two"] = georgia_tech_mailbox.split(",")[0]
session["city"] = "Atlanta"
session["address_state"] = "GA"
session["zip_code"] = "30332"
elif home_address is not None:
address_validation_response = post(
url="https://addressvalidation.googleapis.com/v1:validateAddress",
params={"key": app.config["GOOGLE_MAPS_BACKEND_API_KEY"]},
json={
"address": {
"regionCode": "US",
"addressLines": [home_address],
},
"enableUspsCass": True,
},
timeout=(5, 5),
)
if address_validation_response.status_code == 200:
address_validation_json = address_validation_response.json()
session["address_line_one"] = ""
session["address_line_two"] = ""
session["city"] = ""
session["address_state"] = None
if (
"result" in address_validation_json
and "address" in address_validation_json["result"]
and "postalAddress" in address_validation_json["result"]["address"]
):
if (
"postalCode"
in address_validation_json["result"]["address"]["postalAddress"]
):
session["zip_code"] = address_validation_json["result"]["address"][
"postalAddress"
]["postalCode"]
if fullmatch(r"^\d{5}-\d{4}$", session["zip_code"]):
session["zip_code"] = session["zip_code"].split("-")[0]
if "locality" in address_validation_json["result"]["address"]["postalAddress"]:
session["city"] = address_validation_json["result"]["address"][
"postalAddress"
]["locality"]
if (
"administrativeArea"
in address_validation_json["result"]["address"]["postalAddress"]
):
session["address_state"] = address_validation_json["result"]["address"][
"postalAddress"
]["administrativeArea"]
if (
"addressLines"
in address_validation_json["result"]["address"]["postalAddress"]
and len(
address_validation_json["result"]["address"]["postalAddress"][
"addressLines"
]
)
> 0
):
session["address_line_one"] = address_validation_json["result"]["address"][
"postalAddress"
]["addressLines"][0]
if (
"addressLines"
in address_validation_json["result"]["address"]["postalAddress"]
and len(
address_validation_json["result"]["address"]["postalAddress"][
"addressLines"
]
)
> 1
):
session["address_line_two"] = address_validation_json["result"]["address"][
"postalAddress"
]["addressLines"][1]
else:
capture_message(
"Failed to validate homePostalAddress from Whitepages: "
+ address_validation_response.text
)
return redirect(url_for("index"))
@app.get("/verify-email")
def verify_email() -> Any:
"""
Redirects user to mailbox provider for email address verification
"""
if "user_state" not in session:
raise Unauthorized("Not logged in")
set_user(
{
"id": session["user_id"],
"username": session["username"],
"email": session["email_address"],
"ip_address": request.remote_addr,
}
)
if session["user_state"] != "eligible":
raise Unauthorized("Not eligible")
email_address_domain = Address(addr_spec=request.args["emailAddress"]).domain.split(".")[-2:]
if email_address_domain == ["robojackets", "org"]:
return oauth.google.authorize_redirect(
url_for("verify_google_complete", _external=True),
login_hint=request.args["emailAddress"],
hd="robojackets.org",
)
if email_address_domain == ["gatech", "edu"]:
return oauth.microsoft.authorize_redirect(
url_for("verify_microsoft_complete", _external=True),
login_hint=request.args["emailAddress"],
hd="gatech.edu",
)
raise BadRequest("Unexpected email address domain")
@app.get("/verify-email/google/complete")
def verify_google_complete() -> Response:
"""
Handles the return from Google and updates session appropriately
"""
if "user_state" not in session:
raise Unauthorized("Not logged in")
set_user(
{
"id": session["user_id"],
"username": session["username"],
"email": session["email_address"],
"ip_address": request.remote_addr,
}
)
token = oauth.google.authorize_access_token()
userinfo = token["userinfo"]
session["email_address"] = userinfo["email"]
session["email_verified"] = True
return redirect(url_for("index")) # type: ignore
@app.post("/verify-email/google/complete")
def verify_google_onetap() -> Response:
"""
Handles a Google One Tap login and updates session appropriately
"""
if "user_state" not in session:
raise Unauthorized("Not logged in")
set_user(
{
"id": session["user_id"],
"username": session["username"],
"email": session["email_address"],
"ip_address": request.remote_addr,
}
)
userinfo = id_token.verify_oauth2_token( # type: ignore
request.form["credential"], requests.Request(), app.config["GOOGLE_CLIENT_ID"] # type: ignore # noqa: E501
)
if userinfo["hd"] != "robojackets.org":
raise Unauthorized("Invalid hd value")
session["email_address"] = userinfo["email"]
session["email_verified"] = userinfo["email_verified"]
return redirect(url_for("index")) # type: ignore
@app.get("/verify-email/microsoft/complete")
def verify_microsoft_complete() -> Response:
"""
Handles the return from Microsoft and updates session appropriately
"""
if "user_state" not in session:
raise Unauthorized("Not logged in")
set_user(
{
"id": session["user_id"],
"username": session["username"],
"email": session["email_address"],
"ip_address": request.remote_addr,
}
)
token = oauth.microsoft.authorize_access_token()
userinfo = token["userinfo"]
session["email_address"] = userinfo["email"]
session["email_verified"] = True
return redirect(url_for("index")) # type: ignore
@app.post("/")
def submit() -> Union[Response, str]: # pylint: disable=too-many-branches
"""
Submits the form for fulfillment
"""
if "user_state" not in session:
raise Unauthorized("Not logged in")
set_user(
{
"id": session["user_id"],
"username": session["username"],
"email": session["email_address"],
"ip_address": request.remote_addr,
}
)
if session["user_state"] == "requested":
return render_template("submitted.html")
if session["user_state"] != "eligible":
raise Unauthorized("Not eligible")
if not session["email_verified"]:
raise BadRequest("Email address must be verified")
manager_response = get(
url=app.config["APIARY_URL"] + "/api/v1/users/" + str(int(request.form["manager"])),
headers={
"Authorization": "Bearer " + app.config["APIARY_TOKEN"],
"Accept": "application/json",
},
timeout=(5, 5),
)
if manager_response.status_code != 200:
raise InternalServerError("Failed to retrieve manager information from Apiary")
manager = manager_response.json()["user"]
keycloak_access_token_response = post(
url=app.config["KEYCLOAK_SERVER"] + "/realms/master/protocol/openid-connect/token",
data={
"client_id": app.config["KEYCLOAK_ADMIN_CLIENT_ID"],
"client_secret": app.config["KEYCLOAK_ADMIN_CLIENT_SECRET"],
"grant_type": "client_credentials",
},
timeout=(5, 5),
)
if keycloak_access_token_response.status_code != 200:
raise InternalServerError("Failed to retrieve access token for Keycloak")
if (
"gmail_address" in manager
and manager["gmail_address"] is not None
and manager["gmail_address"].endswith("@robojackets.org")
):
manager_email_address = manager["gmail_address"]
else:
keycloak_user_response = get(
url=app.config["KEYCLOAK_SERVER"]
+ "/admin/realms/"
+ app.config["KEYCLOAK_REALM"]
+ "/users",
params={
"username": manager["uid"],
"exact": True,
},
headers={
"Authorization": "Bearer " + keycloak_access_token_response.json()["access_token"],
},
timeout=(5, 5),
)
if keycloak_user_response.status_code != 200:
raise InternalServerError("Failed to search for manager in Keycloak")
if len(keycloak_user_response.json()) == 0:
manager_email_address = manager["gt_email"]
elif len(keycloak_user_response.json()) == 1:
keycloak_user = keycloak_user_response.json()[0]
if (
"attributes" in keycloak_user
and "googleWorkspaceAccount" in keycloak_user["attributes"]
):
manager_email_address = keycloak_user["attributes"]["googleWorkspaceAccount"][0]
else:
manager_email_address = manager["gt_email"]
else:
raise InternalServerError("More than one result for manager search in Keycloak")
postmark_response = post(
url="https://api.postmarkapp.com/email",
headers={"X-Postmark-Server-Token": app.config["POSTMARK_TOKEN"]},
json={
"From": app.config["POSTMARK_FROM"],
"To": app.config["POSTMARK_TO_FULFILLMENT"],
"Cc": request.form["first_name"]
+ " "
+ request.form["last_name"]
+ "<"
+ request.form["email_address"]
+ ">, "
+ app.config["POSTMARK_TO_TREASURER"]
+ ", "
+ manager["full_name"]
+ " < "
+ manager_email_address
+ ">",
"Subject": request.form["first_name"]
+ " "
+ request.form["last_name"]
+ " requested a BILL Spend & Expense account",
"TextBody": render_template("email.txt", manager=manager["full_name"]),
"MessageStream": "outbound",
},
timeout=(5, 5),
)
if postmark_response.status_code == 200:
# remove eligible role
delete(
url=app.config["KEYCLOAK_SERVER"]
+ "/admin/realms/"
+ app.config["KEYCLOAK_REALM"]
+ "/users/"
+ session["sub"]
+ "/role-mappings/clients/"
+ app.config["KEYCLOAK_CLIENT_UUID"],
headers={
"Authorization": "Bearer " + keycloak_access_token_response.json()["access_token"],
},
timeout=(5, 5),
json=[{"id": app.config["KEYCLOAK_CLIENT_ROLE_ELIGIBLE"], "name": "eligible"}],
)
# add provisioned role
post(
url=app.config["KEYCLOAK_SERVER"]
+ "/admin/realms/"
+ app.config["KEYCLOAK_REALM"]
+ "/users/"
+ session["sub"]
+ "/role-mappings/clients/"
+ app.config["KEYCLOAK_CLIENT_UUID"],
headers={
"Authorization": "Bearer " + keycloak_access_token_response.json()["access_token"],
},
timeout=(5, 5),
json=[{"id": app.config["KEYCLOAK_CLIENT_ROLE_PROVISIONED"], "name": "provisioned"}],
)
session["user_state"] = "requested"
return render_template("submitted.html")
raise InternalServerError(
"Postmark returned unexpected response code " + str(postmark_response.status_code)
)
@app.get("/ping")
def ping() -> Dict[str, str]:
"""
Returns an arbitrary successful response, for health checks
"""
return {"status": "ok"}