-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
1723 lines (1317 loc) · 59.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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import config
import re
import test_init_config
import json
import base64
from functools import wraps
from flask import Flask, request, send_from_directory, redirect, make_response, render_template, abort
from utils.rest import OktaUtil
from utils.db import PartnerDB
"""
GLOBAL VARIABLES ########################################################################################################
"""
app = Flask(__name__)
app.secret_key = "6w_#w*~AVts3!*yd&C]jP0(x_1ssd]MVgzfAw8%fF+c@|ih0s1H&yZQC&-u~O[--" # For the session
EMAIL_REGEX = re.compile(r"[^@\s]+@[^@\s]+\.[a-zA-Z0-9]+$")
"""
UTILS ###################################################################################################################
"""
def decode_base64(data):
print "decode_base64()"
"""Decode base64, padding being optional.
:param data: Base64 data as an ASCII byte string
:returns: The decoded byte string.
"""
missing_padding = len(data) % 4
if missing_padding != 0:
data += b'='* (4 - missing_padding)
return json.loads(base64.b64decode(data))
def get_user_info_from_token(token):
print "get_token_data()"
print "token: {0}".format(token)
broke_token = token.split(".")
print "broke_token: {0}".format(broke_token[1])
user_info = decode_base64(broke_token[1])
return user_info
def authorize_partner(f):
@wraps(f)
def decorated_function(*args, **kws):
print "authorize_partner check()"
is_admin = False
if not "token" in request.cookies:
return make_response(redirect("{0}/show_login".format(config.okta["app_host"])))
user_token = None
try:
user_token = get_current_user_token()
if user_token["active"]:
token = request.cookies.get("token")
user_info = get_user_info_from_token(token)
print user_info
# Bacsic admin check. YOu can add more robust checks here
if "groups" in user_info:
for group in user_info["groups"]:
print "group: {0}".format(group)
if (
(
group.find(config.okta["partner_group_filter_prefix"]) != -1 and
group.find(config.okta["partner_group_filter_suffix"]) != -1
) or (
group == config.okta["admin_okta_group_name"] != -1
)
):
is_admin = True
break
if "admin_group" in user_info:
for group in user_info["admin_group"]:
if group == config.okta["admin_okta_group_name"] != -1:
is_admin = True
break
else:
return make_response(redirect("{0}/show_login".format(config.okta["app_host"])))
except:
abort(401)
if not is_admin:
abort(401)
return f(*args, **kws)
return decorated_function
def authorize_admin(f):
@wraps(f)
def decorated_function(*args, **kws):
print "authorize_admin check()"
is_admin = False
if not "token" in request.cookies:
return make_response(redirect("{0}/show_login".format(config.okta["app_host"])))
user_token = None
try:
user_token = get_current_user_token()
if user_token["active"]:
token = request.cookies.get("token")
user_info = get_user_info_from_token(token)
print user_info
# Bacsic admin check. YOu can add more robust checks here
if "admin_group" in user_info:
for group in user_info["admin_group"]:
print "group: {0}".format(group)
if group == config.okta["admin_okta_group_name"] != -1:
is_admin = True
else:
abort(401)
else:
return make_response(redirect("{0}/show_login".format(config.okta["app_host"])))
except:
abort(401)
if not is_admin:
abort(401)
return f(*args, **kws)
return decorated_function
def authenticated(f):
@wraps(f)
def decorated_function(*args, **kws):
print "authenticated()"
user = get_current_user()
if not user:
return make_response(redirect("{0}/show_login".format(config.okta["app_host"])))
return f(*args, **kws)
return decorated_function
def get_oauth_token(oauth_code):
print "get_oauth_token()"
okta_util = OktaUtil(request.headers, config.okta)
oauth_token_response_json = okta_util.get_oauth_token(oauth_code, config.okta["redirect_uri"])
print "oauth_token_response_json: {0}".format(oauth_token_response_json)
return oauth_token_response_json["access_token"]
def get_session_token(username, password):
print "get_session_token()"
okta_util = OktaUtil(request.headers, config.okta)
session_token = None
authn_reponse_json = okta_util.get_session_token(username, password)
if "sessionToken" in authn_reponse_json:
session_token = authn_reponse_json["sessionToken"]
return session_token
def handle_login(form_data):
print "handle_login()"
okta_util = OktaUtil(request.headers, config.okta)
# Authenticate via Okta API to get Session Token
session_token = None
try:
if "auth_token" in form_data:
session_token = okta_util.authenticate_via_activation_token(form_data["auth_token"])
else:
session_token = get_session_token(username=form_data["username"], password=form_data["password"])
except ValueError as err:
print(err.args)
print "session_token: {0}".format(session_token)
# Use Session Token to generatet OIDC Auth Code URL
if(session_token):
oidc_auth_code_url = okta_util.create_oidc_auth_code_url(
session_token,
config.okta["oidc_client_id"],
config.okta["redirect_uri"])
print "url: {0}".format(oidc_auth_code_url)
# redirect to User Auth Code URL to Get OIDC Code
return redirect(oidc_auth_code_url)
else:
error_list = {
"messages": [{"message": "Bad user name and/or password"}]
}
response = make_response(
render_template(
"login.html",
user={},
error_list=error_list,
form_data={},
okta_config=config.okta,
is_admin=False
)
)
return response
def handle_logout():
print "handle_logout()"
redirect_url = "{host}/login/signout?fromURI={redirect_path}".format(
host=config.okta["org_host"],
redirect_path=config.okta["app_host"]
)
print "redirect_url: {0}".format(redirect_url)
response = make_response(redirect(redirect_url))
response.set_cookie('token', "")
return response
# Checks if form entries are valid
# Creates the user in Okta
# Puts a record in the DB Approval Queue
# Emails the respective group/partner admin for approval
# Displays the success screen to the end user
def handle_register(form_data):
print "handle_register()"
print form_data
is_registration_completed = False
okta_util = OktaUtil(request.headers, config.okta)
partner_groups = okta_util.search_groups(config.okta["partner_group_filter_prefix"], config.okta["group_query_limit"])
partner_db = PartnerDB(config.okta["db_file_name"])
roles = partner_db.get_roles()
# Checks if form entries are valid
error_list, is_registration_completed = validate_registration(form_data)
if is_registration_completed: # No errors
is_registration_completed = True
# Creates the user in Okta
created_user = create_user(form_data)
email_activation_link_to_user(form_data, created_user["id"])
login_data = {
"username": form_data["email"],
"password": form_data["password"]
}
# Add user to webiste group for access
okta_util.assign_user_to_group(created_user["id"], config.okta["app_okta_group_id"])
# Puts a record in the DB Approval Queue
# Emails the respective group/partner admin for approval
partner_admin_group_name = None
partner_admin_group_id = None
for group in partner_groups:
if group["id"] == form_data["partner"]:
partner_admin_group_name = "{0}{1}".format(group["profile"]["name"], config.okta["partner_group_filter_suffix"])
break
print "partner_admin_group_name: {0}".format(partner_admin_group_name)
for group in partner_groups:
if group["profile"]["name"] == partner_admin_group_name:
partner_admin_group_id = group["id"]
print "partner_admin_group_id: {0}".format(partner_admin_group_id)
json = {
"user_id": created_user["id"],
"group_id": form_data["partner"],
"partner_admin_group_id": partner_admin_group_id
}
if "ADMIN" == form_data["role"]:
handle_partner_admin_request(json)
else:
handle_partner_user_request(json)
# Prepare response
response = make_response(render_template(
"register.html",
is_registration_completed=is_registration_completed,
partner_groups=partner_groups,
error_list=error_list,
form_data=form_data,
roles_list=roles,
okta_config=config.okta
))
return response
def handle_partner_admin_request(post_json):
print "handle_partner_admin_request()"
print post_json
okta_util = OktaUtil(request.headers, config.okta)
partner_db = PartnerDB(config.okta["db_file_name"])
user = okta_util.get_user(post_json["user_id"])
group = okta_util.get_group(post_json["group_id"])
admin_group = okta_util.get_group(config.okta["admin_okta_group_id"])
dealer_admin_group = None
dealer_admin_search_results = okta_util.search_groups("{0}{1}".format(group["profile"]["name"], config.okta["partner_group_filter_suffix"]), 1)
if(len(dealer_admin_search_results) != 0):
dealer_admin_group = dealer_admin_search_results[0]
if(dealer_admin_group):
partner_db.create_partner_admin_approval_queue(post_json["user_id"], dealer_admin_group["id"])
else:
partner_db.create_partner_admin_approval_queue(post_json["user_id"], group["id"])
subject = "The user {first_name} {last_name} is requesting ADMIN access to {group_name}".format(
first_name=user["profile"]["firstName"],
last_name=user["profile"]["lastName"],
group_name=group["profile"]["name"]
)
message = (
"The user {first_name} {last_name} is requesting ADMIN access to {group_name}. <br />"
"Click <a href='{app_host}/admin/admin_approval_queue/'>here</a> to manage the Admin approval queue."
).format(
first_name=user["profile"]["firstName"],
last_name=user["profile"]["lastName"],
app_host=config.okta["app_host"],
group_name=post_json["group_id"]
)
admin_user_list = okta_util.get_group_users(admin_group["id"])
admin_email_list = []
for admin_user in admin_user_list:
admin_email_list.append({"address": admin_user["profile"]["email"]})
okta_util.send_mail(subject, message, admin_email_list)
return "SUCCESS"
def handle_partner_user_request(post_json):
print "handle_partner_user_request()"
print post_json
okta_util = OktaUtil(request.headers, config.okta)
partner_db = PartnerDB(config.okta["db_file_name"])
user = okta_util.get_user(post_json["user_id"])
group = okta_util.get_group(post_json["group_id"])
admin_group = okta_util.get_group(post_json["partner_admin_group_id"])
partner_db.create_partner_approval_queue(post_json["user_id"], post_json["group_id"])
subject = "The user {first_name} {last_name} is requesting USER access to {group_name}".format(
first_name=user["profile"]["firstName"],
last_name=user["profile"]["lastName"],
group_name=group["profile"]["name"]
)
message = (
"The user {first_name} {last_name} is requesting USER access to {group_name}. <br />"
"Click <a href='{app_host}/admin/partner_approval_queue/{group_id}'>here</a> to manage the Admin approval queue."
).format(
first_name=user["profile"]["firstName"],
last_name=user["profile"]["lastName"],
app_host=config.okta["app_host"],
group_name=group["profile"]["name"],
group_id=post_json["group_id"]
)
admin_user_list = okta_util.get_group_users(admin_group["id"])
admin_email_list = []
for admin_user in admin_user_list:
admin_email_list.append({"address": admin_user["profile"]["email"]})
okta_util.send_mail(subject, message, admin_email_list)
return "SUCCESS"
def handle_admin():
print "handle_admin"
okta_util = OktaUtil(request.headers, config.okta)
is_admin = False
is_partner_admin = False
user = get_current_user(add_groups=True)
group = {}
#is_admin(user)
#is_current_partner_admin(user, user["profile"]["current_dlr"])
print "user: {0}".format(user["profile"])
if "current_dlr" in user["profile"]:
group = okta_util.get_group(user["profile"]["current_dlr"])
is_partner_admin = True
if group["profile"]["name"] == config.okta["admin_okta_group_name"] != -1:
is_admin = True
else:
groups = okta_util.get_user_groups(user["id"])
for group in groups:
if group["profile"]["name"] == config.okta["admin_okta_group_name"] != -1:
is_admin = True
break
response = make_response(render_template(
"admin.html",
okta_config=config.okta,
user=user,
group=group,
is_admin=is_admin,
is_partner_admin=is_partner_admin
))
return response
def handle_admin_application_request(post_json):
print "handle_admin_application_request()"
okta_util = OktaUtil(request.headers, config.okta)
partner_db = PartnerDB(config.okta["db_file_name"])
user = okta_util.get_user(post_json["user_id"])
group = okta_util.get_group(post_json["group_id"])
app_list = okta_util.find_all_apps_by_id(post_json["app_ids"])
for app_id in post_json["app_ids"]:
partner_db.create_application_approval_queue(post_json["user_id"], post_json["group_id"], app_id)
subject = "The user {first_name} {last_name} is requesting access to some apps".format(
first_name=user["profile"]["firstName"],
last_name=user["profile"]["lastName"]
)
message = (
"The user {first_name} {last_name} is requesting access to some apps. <br />"
"Click <a href='{app_host}/admin/application_approval_queue/{group_id}'>here</a> to manage the Application approval queue."
).format(
first_name=user["profile"]["firstName"],
last_name=user["profile"]["lastName"],
app_host=config.okta["app_host"],
group_id=post_json["group_id"]
)
admin_group_name = "{0}{1}".format(group["profile"]["name"], config.okta["partner_group_filter_suffix"])
admin_group_list = okta_util.search_groups(admin_group_name, 1)
admin_user_list = okta_util.get_group_users(admin_group_list[0]["id"])
admin_email_list = []
for admin_user in admin_user_list:
admin_email_list.append({"address": admin_user["profile"]["email"]})
okta_util.send_mail(subject, message, admin_email_list)
return "SUCCESS"
def handle_admin_approval_queue():
print "handle_admin_approval_queue()"
okta_util = OktaUtil(request.headers, config.okta)
partner_db = PartnerDB(config.okta["db_file_name"])
is_admin = False
is_partner_admin = False
user = get_current_user(add_groups=True)
users_approval_list = partner_db.get_partner_admin_approval_queue()
user_group = {
"list": []
}
for user_approval in users_approval_list:
user = okta_util.get_user(user_approval["okta_user_id"])
print "Get Group by Id: {0}".format(user_approval["okta_group_id"])
group = okta_util.get_group(user_approval["okta_group_id"])
user_group["list"].append({ "user": user, "group": group})
# print "user_group_list: {0}".format(user_group_list)
# print "user_group[list].group: {0}".format(json.dumps(user_group["list"][0]["group"], indent=4, sort_keys=True))
response = make_response(render_template(
"admin_approval_queue.html",
okta_config=config.okta,
user_group=user_group,
user=user,
is_admin=is_admin,
is_partner_admin=is_partner_admin
))
return response
def handle_admin_application_approval_queue(group_id):
print "handle_admin_application_approval_queue()"
okta_util = OktaUtil(request.headers, config.okta)
partner_db = PartnerDB(config.okta["db_file_name"])
current_user = get_current_user(add_groups=True)
group = okta_util.get_group(group_id)
approval_queue_rows = partner_db.get_application_approval_queue_by_group(group_id)
# print "user_ids: {0}".format(user_ids)
app_list = []
for row in approval_queue_rows:
user = okta_util.get_user(row["okta_user_id"])
app = okta_util.get_application(row["okta_app_id"])
app_response = {
"id": app["id"],
"label": app["label"],
"user": {
"id": user["id"],
"profile": {
"firstName": user["profile"]["firstName"],
"lastName": user["profile"]["lastName"],
"email": user["profile"]["email"]
}
}
}
print "app_response: {0}".format(app_response)
app_list.append(app_response)
# print "current_user: {0}".format(json.dumps(current_user, indent=4, sort_keys=True))
response = make_response(
render_template(
"admin_application_approval_queue.html",
user=current_user,
group=group,
app_list=app_list,
okta_config=config.okta
)
)
return response
def handle_admin_partner_approval_queue(group_id):
print "admin_partner_approval_queue()"
okta_util = OktaUtil(request.headers, config.okta)
partner_db = PartnerDB(config.okta["db_file_name"])
is_admin = False
is_partner_admin = False
user = get_current_user(add_groups=True)
group = okta_util.get_group(group_id)
approval_queue_rows = partner_db.get_partner_approval_queue_by_group(group_id)
# print "user_ids: {0}".format(user_ids)
user_id_list = []
for row in approval_queue_rows:
user_id_list.append(row["okta_user_id"])
user_list = okta_util.find_all_users_by_id(user_id_list)
response = make_response(
render_template(
"admin_partner_approval_queue.html",
group=group,
user=user,
user_list=user_list,
okta_config=config.okta,
is_admin=is_admin,
is_partner_admin=is_partner_admin
)
)
return response
def handle_admin_approve_partner_access(group_id, user_id):
print "handle_admin_approve_partner_access()"
okta_util = OktaUtil(request.headers, config.okta)
user = okta_util.get_user(user_id)
group = okta_util.get_group(group_id)
# Check Primary Partner If non, add this to primary
if "primary_dlr" not in user["profile"]:
user["profile"]["primary_dlr"] = group["id"]
user["profile"]["current_dlr"] = group["id"]
okta_util.update_user(user)
# Add to Partner List
# Grant Group Acess
okta_util.assign_user_to_group(user["id"], group["id"])
# Update user profile to add user to the group
if config.okta["user_group_access_list"] not in user["profile"]:
user["profile"][config.okta["user_group_access_list"]] = []
user["profile"][config.okta["user_group_access_list"]].append(group["profile"]["description"].replace(config.okta["user_current_group_desc_mapping_prefix"], ""))
okta_util.update_user(user)
# Remove request record from db
partner_db = PartnerDB(config.okta["db_file_name"])
partner_db.delete_partner_approval_queue(user["id"], group["id"])
# Email acceptance
subject = "Your access to {group_name} has been approved!".format(group_name=group["profile"]["name"])
message = "Your access to {group_name} has been approved!".format(group_name=group["profile"]["name"])
recipients = [{"address": user["profile"]["email"]}]
okta_util.send_mail(subject, message, recipients)
return make_response(redirect("{app_host}/admin/partner_approval_queue/{group_id}".format(app_host=config.okta["app_host"], group_id=group["id"])))
def handle_admin_approve_partner_admin_access(group_id, user_id):
print "handle_admin_approve_partner_admin_access()"
okta_util = OktaUtil(request.headers, config.okta)
user = okta_util.get_user(user_id)
admin_group = okta_util.get_group(group_id)
dealer_group = okta_util.search_groups(admin_group["profile"]["name"].replace(config.okta["partner_group_filter_suffix"],""), 1)[0]
# Check Primary Partner If non, add this to primary
if "primary_dlr" not in user["profile"]:
user["profile"]["primary_dlr"] = dealer_group["id"]
user["profile"]["current_dlr"] = dealer_group["id"]
okta_util.update_user(user)
# Add to Partner List
# Grant Group Acess
okta_util.assign_user_to_group(user["id"], admin_group["id"])
okta_util.assign_user_to_group(user["id"], dealer_group["id"])
# Remove request record from db
partner_db = PartnerDB(config.okta["db_file_name"])
partner_db.delete_partner_admin_approval_queue(user["id"], admin_group["id"])
# Email acceptance
subject = "Your ADMIN access to {group_name} has been approved!".format(group_name=admin_group["profile"]["name"])
message = "Your ADMIN access to {group_name} has been approved!".format(group_name=admin_group["profile"]["name"])
recipients = [{"address": user["profile"]["email"]}]
okta_util.send_mail(subject, message, recipients)
return make_response(redirect("{app_host}/admin/admin_approval_queue".format(app_host=config.okta["app_host"])))
def handle_admin_approve_application_access(group_id, user_id, app_id):
print "handle_admin_approve_application_access()"
okta_util = OktaUtil(request.headers, config.okta)
user = okta_util.get_user(user_id)
group = okta_util.get_group(group_id)
app = okta_util.get_application(app_id)
# Add to Partner List
# Grant App Acess
# HACK need to fix before comitting into github
if app["id"] == "0oaxtkqtmiEzP1qNR2p5":
okta_util.assign_user_to_group(user["id"], "00g1a6tb6UIKbxm1Q2p6") #SalesForceChatter group in okta
else:
okta_util.assign_user_to_app(user["id"], app["id"])
# Remove request record from db
partner_db = PartnerDB(config.okta["db_file_name"])
partner_db.delete_application_approval_queue(user["id"], group["id"], app["id"])
# Email acceptance
subject = "Your access to {app_name} has been approved!".format(app_name=app["label"])
message = "Your access to {app_name} for {group_name} has been approved!".format(
app_name=app["label"],
group_name=group["profile"]["name"]
)
recipients = [{"address": user["profile"]["email"]}]
okta_util.send_mail(subject, message, recipients)
return make_response(redirect("{app_host}/admin/application_approval_queue/{group_id}".format(app_host=config.okta["app_host"], group_id=group_id)))
def handle_admin_deny_partner_access(group_id, user_id):
print "handle_admin_deny_partner_access()"
okta_util = OktaUtil(request.headers, config.okta)
user = okta_util.get_user(user_id)
group = okta_util.get_group(group_id)
# Email Denial
subject = "Your request to access {group_name} has been rejected".format(group_name=group["profile"]["name"])
message = "We are sorry to inform you that your request for access to {group_name} was rejected.".format(group_name=group["profile"]["name"])
recipients = [{"address": user["profile"]["email"]}]
okta_util.send_mail(subject, message, recipients)
# Remove request record from db
partner_db = PartnerDB(config.okta["db_file_name"])
partner_db.delete_partner_approval_queue(user["id"], group["id"])
return make_response(redirect("{app_host}/admin/partner_approval_queue/{group_id}".format(app_host=config.okta["app_host"], group_id=group["id"])))
def handle_admin_deny_partner_admin_access(group_id, user_id):
print "handle_admin_deny_partner_admin_access()"
okta_util = OktaUtil(request.headers, config.okta)
user = okta_util.get_user(user_id)
group = okta_util.get_group(group_id)
# Email Denial
subject = "Your request to access {group_name} has been rejected".format(group_name=group["profile"]["name"])
message = "We are sorry to inform you that your request for access to {group_name} was rejected.".format(group_name=group["profile"]["name"])
recipients = [{"address": user["profile"]["email"]}]
okta_util.send_mail(subject, message, recipients)
# Remove request record from db
partner_db = PartnerDB(config.okta["db_file_name"])
partner_db.delete_partner_admin_approval_queue(user["id"], group["id"])
return make_response(redirect("{app_host}/admin/admin_approval_queue".format(app_host=config.okta["app_host"])))
def handle_admin_deny_application_access(group_id, user_id, app_id):
print "handle_admin_deny_application_access()"
okta_util = OktaUtil(request.headers, config.okta)
user = okta_util.get_user(user_id)
group = okta_util.get_group(group_id)
app = okta_util.get_application(app_id)
# Email Denial
subject = "Your request to access {app_name} has been rejected".format(app_name=app["label"])
message = "We are sorry to inform you that your request for access {app_name} for {group_name} was rejected.".format(
app_name=app["label"],
group_name=group["profile"]["name"]
)
recipients = [{"address": user["profile"]["email"]}]
okta_util.send_mail(subject, message, recipients)
# Remove request record from db
partner_db = PartnerDB(config.okta["db_file_name"])
partner_db.delete_application_approval_queue(user["id"], group["id"], app["id"])
return make_response(redirect("{app_host}/admin/application_approval_queue/{group_id}".format(app_host=config.okta["app_host"], group_id=group["id"])))
def handle_activate(user_id):
print "handle_activate()"
okta_util = OktaUtil(request.headers, config.okta)
activate_response = okta_util.activate_user(user_id)
print "activate_response: {0}".format(activate_response)
if "activationToken" in activate_response:
activation_token = activate_response["activationToken"]
print "activation_token: {0}".format(activation_token)
# Took this out because the MFA Enroll status was breaking the auto login
# handle_login({ "auth_token": activation_token })
#return make_response(redirect(config.okta["app_host"]))
return make_response(redirect("{0}/show_login".format(config.okta["app_host"])))
def handle_activate_all():
print "handle_activate_all()"
okta_util = OktaUtil(request.headers, config.okta)
staged_users = okta_util.find_users_by_status("STAGED")
print "Staged User Count: {0}".format(len(staged_users))
for user in staged_users:
email_activation_link_to_user({"email": user["profile"]["email"]}, user["id"])
# handle_activate(user["id"])
return make_response(redirect("{0}/show_login".format(config.okta["app_host"])))
def handle_init_test():
print "handle_init_test()"
okta_util = OktaUtil(request.headers, config.okta)
partner_db = PartnerDB(config.okta["db_file_name"])
# Create Partner Portal Groups
print "Creating Partner Portal Groups"
for group in test_init_config.partner_portal["groups"]:
group_name = group["profile"]["name"]
found_group = okta_util.search_groups(group_name, 1)
if len(found_group) == 0:
created_group = okta_util.create_group(group)
print "Created group: {0}".format(created_group["profile"]["name"])
else:
print "Group {0} already exsists".format(group_name)
found_group_db_data = partner_db.get_partner_profile_by_group(group_name)
if found_group_db_data == None:
if "zip" in group["profile"]:
print "Group Profile: {0} not in DB"
result = partner_db.create_partner_profile(group_name, group["profile"]["zip"])
# Create Partner Portal Admin Users
print "Creating Partner Portal Admin Users"
for user in test_init_config.partner_portal["admin_users"]:
login = user["profile"]["login"]
found_user = okta_util.find_users_by_login(login)
if "id" not in found_user:
okta_util.create_user(user, True)
print "Created User {0}".format(login)
else:
print "User {0} already exsists".format(login)
# Assign admin roles in DB
print "Partner Portal Admin Assignments"
for assignment in test_init_config.partner_portal["admin_group_assignments"]:
login = assignment["login"]
group_name = assignment["group"]
found_user = okta_util.find_users_by_login(login)[0]
found_group = okta_util.search_groups(group_name, 1)[0]
user_id = found_user["id"]
group_id = found_group["id"]
partner_db.delete_user_partner_role(user_id, group_id, "ADMIN")
result = partner_db.create_user_partner_role(user_id, group_id, "ADMIN")
okta_util.assign_user_to_group(user_id, group_id)
print "assignment: {0}".format(result)
return "done!"
def email_activation_link_to_user(form_data, user_id):
print "email_activation_link_to_user()"
okta_util = OktaUtil(request.headers, config.okta)
subject = "Welcome to the {app_title}".format(app_title=config.okta["app_title"])
message = (
"Welcome to the {app_title}! Click this link to activate your account <br />"
"<a href='{activation_link}'>{activation_link}</a>").format(
app_title=config.okta["app_title"],
activation_link="{app_host}/activate/{user_id}".format(app_host=config.okta["app_host"], user_id=user_id)
)
return okta_util.send_mail(subject, message, [{"address" : form_data["email"]}])
def email_partner_admins_with_new_registration(form_data):
print "email_partner_admins_with_new_registration()"
okta_util = OktaUtil(request.headers, config.okta)
partner_db = PartnerDB(config.okta["db_file_name"])
subject = "A User is requesting {role} access to {group_name}".format(role=form_data["role"], group_name=form_data["partnerName"])
message = (
"A person by the name of {first_name} {last_name} is requesting {role} access to "
"the Partner Portal {group_name} <br />"
"Click <a href=\"{app_host}/admin/partner_approval_queue/{group_id}\">Here</a> to see the approval queue for this Partner"
).format(
first_name=form_data["firstName"],
last_name=form_data["lastName"],
group_name=form_data["partnerName"],
app_host=config.okta["app_host"],
group_id=form_data["partner"],
role=form_data["role"]
)
group_name = "{0}{1}".format(form_data["partnerName"], config.okta["partner_group_filter_suffix"])
group_list = okta_util.search_groups(group_name, 1)
user_list = okta_util.get_group_users(group_list[0]["id"])
admin_email_list = []
for user in user_list:
admin_email_list.append({"address": user["profile"]["email"]})
return okta_util.send_mail(subject, message, admin_email_list)
def email_org_admins_with_admin_request(form_data):
print "email_org_admins_with_admin_request()"
okta_util = OktaUtil(request.headers, config.okta)
partner_db = PartnerDB(config.okta["db_file_name"])
subject = "A User is requesting {role} access to {group_name}".format(role=form_data["role"], group_name=form_data["partnerName"])
message = (
"A person by the name of {first_name} {last_name} is requesting {role} access to "
"the Partner Portal {group_name} <br />"
"Click <a href=\"{app_host}/admin/partner_admin_approval_queue/{group_id}\">Here</a> to see the admin approval queue for this Partner"
).format(
first_name=form_data["firstName"],
last_name=form_data["lastName"],
group_name=form_data["partnerName"],
app_host=config.okta["app_host"],
group_id=form_data["partner"],
role=form_data["role"]
)
portal_admins = okta_util.find_users_by_attribute(config.okta["partner_admin_ud_indicator_attribute"], "true")
admin_email_list = []
for admin_user in portal_admins:
print admin_user
admin_email_list.append({"address": admin_user["profile"]["email"]})
return okta_util.send_mail(subject, message, admin_email_list)
def create_user(form_data):
print "create_user()"
okta_util = OktaUtil(request.headers, config.okta)
user = {
"profile": {
"lastName": form_data["lastName"],
"firstName": form_data["firstName"],
"email": form_data["email"],
"login": form_data["email"]
},
"credentials": {
"password": {"value": form_data["password"]}
}
}
created_user = okta_util.create_user(user, activate_user=False)
print "created_user: {0}".format(created_user)
return created_user
def validate_registration(form_data):
print "validate_registration()"
is_registration_completed = False
okta_util = OktaUtil(request.headers, config.okta)
email = form_data["email"]
password = form_data["password"]
confirm_password = form_data["confirmPassword"]
user = okta_util.find_users_by_login(email)
error_list = {
"messages": []
}
if confirm_password != password:
error_list["messages"].append(
{"message": "Confirm Password does not match password"}
)
if not EMAIL_REGEX.match(email):
error_list["messages"].append(
{"message": "Email is invalid"}
)
if len(user) != 0:
error_list["messages"].append(
{"message": "User already exsists"}
)
if len(error_list["messages"]) == 0:
is_registration_completed = True
return error_list, is_registration_completed
def get_current_user_token():
print "get_current_user_token()"
user_results_json = None
okta_util = OktaUtil(request.headers, config.okta)
if("token" in request.cookies):
introspection_results_json = okta_util.introspect_oauth_token(request.cookies.get("token"))
if("active" in introspection_results_json):
if(introspection_results_json["active"]):
print "Has active token"
user_results_json = {
"active": introspection_results_json["active"],
"username": introspection_results_json["username"],
"uid": introspection_results_json["uid"]
}
else:
print "Has inactive token"
check_okta_session_url = okta_util.create_oidc_auth_code_url(None, config.okta["oidc_client_id"], config.okta["redirect_uri"])
user_results_json = {
"active": False,
"redirect_url": check_okta_session_url
}
else:
print "has inactive token error"
check_okta_session_url = okta_util.create_oidc_auth_code_url(None, config.okta["oidc_client_id"], config.okta["redirect_uri"])
user_results_json = {
"active": False,
"redirect_url": check_okta_session_url
}
else:
print "has no token"
check_okta_session_url = okta_util.create_oidc_auth_code_url(None, config.okta["oidc_client_id"], config.okta["redirect_uri"])
user_results_json = {
"active": False,
"redirect_url": check_okta_session_url
}
if(not user_results_json):
print "has no token default"
user_results_json = {
"active": False
}
return user_results_json
def get_current_user_with_token(user_token_data):
print "get_current_user_with_token()"
current_user = None
if "uid" in user_token_data:
user_id = user_token_data["uid"]
print "Looking up user by id: {0}".format(user_id)
okta_util = OktaUtil(request.headers, config.okta)
current_user = okta_util.get_user(user_id)
return current_user