-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathomniportal.py
1661 lines (1469 loc) · 70.8 KB
/
omniportal.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
from flask_bootstrap import Bootstrap5
from flask import Flask, flash, g, redirect, render_template, request, session, url_for
from flask import Markup
from flask_babel import Babel, _
from flask_babel import lazy_gettext as _l
from flask_wtf import FlaskForm
from wtforms.validators import DataRequired, Length, Regexp
from wtforms.fields import *
from wtforms.widgets import PasswordInput
import json
import requests
import functools
import time
import datetime
import random
import string
import os
import sys
from passlib.hash import pbkdf2_sha256
import secrets
app = Flask(__name__)
# Paths
op_secret_key = "conf/omniportal_secret_key.json"
op_userfile = "conf/omniportal_users.json"
op_settingsfile = "conf/omniportal_settings.json"
op_employeefile = "conf/omniportal_employees.json"
# Try to load a secret token, if it doesn't exist create it.
# This secures cookies and makes them unique to this installation.
try:
with open(op_secret_key, "r") as secret_key_fh:
token = json.loads(secret_key_fh.read())
except json.decoder.JSONDecodeError:
with open(op_secret_key, "w") as secret_key_fh:
import secrets
token = {"token": secrets.token_hex()}
secret_key_fh.write(json.dumps(token))
os.chmod(op_secret_key, 0o600)
except FileNotFoundError:
with open(op_secret_key, "w") as secret_key_fh:
import secrets
token = {"token": secrets.token_hex()}
secret_key_fh.write(json.dumps(token))
os.chmod(op_secret_key, 0o600)
except PermissionError:
sys.exit(f"Permission denied to read/write {op_secret_key}")
secret_key_fh.close()
app.secret_key = token["token"]
# i18n - Translation to other languages
babel = Babel(app)
# Beautify the app with Bootstrap5
bootstrap = Bootstrap5(app)
app.config['BOOTSTRAP_SERVE_LOCAL'] = True
class LoginForm(FlaskForm):
username = StringField(_l('Username'), validators=[DataRequired(message=_l('The username doesn\'t meet the requirements')), Length(1, 64)])
password = PasswordField(_l('Password'), validators=[DataRequired(message=_l('The password doesn\'t meet the requirements')), Length(8, 150)])
submit = SubmitField(_l('Log In'))
class CreateUserForm(FlaskForm):
username = StringField(_l('Username'), validators=[DataRequired(message=_l('The username doesn\'t meet the requirements')), Length(1, 64)])
password = PasswordField(_l('Password'), validators=[DataRequired(message=_l('The password doesn\'t meet the requirements')), Length(8, 150)])
entitlement = SelectMultipleField(label=_l('Entitlement'), choices=[('admin', _l('Administrator')), ('guest', _l('Guests')), ('employee', _l('Employees'))], default='guest', validators=[DataRequired(message=_l('A selection is required!'))])
submit = SubmitField(_l('Create User'))
class ChangePasswordForm(FlaskForm):
current_password = PasswordField(_l('Current Password'), validators=[DataRequired(message=_l('The password doesn\'t meet the requirements')), Length(8, 150)])
new_password = PasswordField(_l('New Password'), validators=[DataRequired(message=_l('The password doesn\'t meet the requirements')), Length(8, 150)])
submit = SubmitField(_l('Change Password'))
class RemoveUserForm(FlaskForm):
username = SelectField(_l('Username'))
submit = SubmitField(_l('Remove User'))
@classmethod
def new(cls):
form = cls()
form.username.choices = get_usernames()
return form
class ChangeSettings(FlaskForm):
guest_operator_url = URLField(_l('OmniVista 2500 NMS / OmniVista Cirrus UPAM URL'), default="https://ov2500-upam-cportal.al-enterprise.com")
guest_operator_username = StringField(_l('Guest Operator Username'), validators=[DataRequired(message=_l('The username doesn\'t meet the requirements')), Length(1, 32)])
guest_operator_password = StringField(_l('Guest Operator Password'), validators=[DataRequired(message=_l('The password doesn\'t meet the requirements')), Length(6, 16)], widget=PasswordInput(hide_value=False))
guest_prefix = StringField(_l('Guest Prefix'), validators=[DataRequired(message=_l('The guest prefix doesn\'t meet the requirements')), Length(1, 10)], default=_l('guest'))
wifi_network = StringField(_l('Guest Wi-Fi / SSID Name'), validators=[DataRequired(message=_l('The Wi-Fi network / SSID name doesn\'t meet the requirements')), Length(1, 31)])
ale_rainbow_webhook = StringField(_l('ALE Rainbow Webhook'), widget=PasswordInput(hide_value=False))
ringcentral_webhook = StringField(_l('RingCentral / Rainbow Office Webhook'), widget=PasswordInput(hide_value=False))
ms_teams_webhook = StringField(_l('Microsoft Teams Webhook'), widget=PasswordInput(hide_value=False))
employee_prefix = StringField(_l('Employee Prefix'), default=_l('employee_'))
employee_wifi = StringField(_l('Employee Wi-Fi / SSID Name'))
ove_ovc_url = URLField(_l('OmniVista 2500 NMS / OmniVista Cirrus URL'), default='https://tenant-name.ov.ovcirrus.com')
validate_ove_ovc_cert = RadioField(_l('Validate OmniVista 2500 NMS HTTPS certificate'), choices=[('yes', _l('Yes')), ('no', _l('No'))], default='no')
# Access via API key is currently not offered
#ove_ovc_api_key = StringField(_l('OmniVista 2500 NMS / OmniVista Cirrus API Key'), widget=PasswordInput(hide_value=False))
ove_ovc_username = StringField(_l('OmniVista 2500 NMS / OmniVista Cirrus Username'))
ove_ovc_password = StringField(_l('OmniVista 2500 NMS / OmniVista Cirrus Password'), widget=PasswordInput(hide_value=False))
smtp_server = StringField(_l('SMTP Server'))
smtp_auth = RadioField(_l('SMTP Authentication'), choices=[('yes', _l('Yes')), ('no', _l('No'))], default='yes')
smtp_port = IntegerField(_l('SMTP Port'), default=587)
smtp_user = StringField(_l('SMTP User'))
smtp_password = StringField(_l('SMTP Password'), widget=PasswordInput(hide_value=False))
email_from_address = StringField(_l('From: Email address'))
submit = SubmitField(_l('Save Settings'))
@classmethod
def new(cls):
form = cls()
settings = read_settings()
try:
form.guest_operator_url.data = settings["guest_operator_url"]
form.guest_operator_username.data = settings["guest_operator_username"]
form.guest_operator_password.data = settings["guest_operator_password"]
form.guest_prefix.data = settings["guest_prefix"]
form.ale_rainbow_webhook.data = settings["ale_rainbow_webhook"]
form.ringcentral_webhook.data = settings["ringcentral_webhook"]
form.ms_teams_webhook.data = settings["ms_teams_webhook"]
form.employee_prefix.data = settings["employee_prefix"]
form.wifi_network.data = settings["wifi_network"]
form.employee_wifi.data = settings["employee_wifi"]
form.ove_ovc_url.data = settings["ove_ovc_url"]
form.validate_ove_ovc_cert.data = settings["validate_ove_ovc_cert"]
#form.ove_ovc_api_key.data = settings["ove_ovc_api_key"]
form.ove_ovc_username.data = settings["ove_ovc_username"]
form.ove_ovc_password.data = settings["ove_ovc_password"]
form.smtp_server.data = settings["smtp_server"]
form.smtp_auth.data = settings["smtp_auth"]
form.smtp_port.data = settings["smtp_port"]
form.smtp_user.data = settings["smtp_user"]
form.smtp_password.data = settings["smtp_password"]
form.email_from_address.data = settings["email_from_address"]
except KeyError:
pass
return form
class AddGuestForm(FlaskForm):
#username = StringField(_l('Username'), validators=[DataRequired(message=_l('The username doesn\'t meet the requirements')), Length(1, 128)])
username = StringField(_l('Username'), validators=[Regexp(r'^[0-9a-zA-Z/\.\-:_@\S]+$', message=_l('The field can only contain 0-9 a-z A-Z / . - : _ @')), Length(1, 128)])
# I don't think that anybody would want to specify the password manually
#password = PasswordField(_l('Password'), validators=[DataRequired(message=_l('The password doesn\'t meet the requirements')), Length(6, 16)])
valid_until = DateField(label=_l('Valid Until'), description=_l('Select the day until when the account should be valid!'))
submit = SubmitField(label=_l('Create Guest'))
class AddEmployeeForm(FlaskForm):
#username = StringField(_l('Username'), validators=[Regexp(r'^[0-9a-zA-Z/\.\-:_@\S]+$', message=_l('The field can only contain 0-9 a-z A-Z / . - : _ @')), Length(1, 32)])
#email = StringField(_l('Email'), validators=[DataRequired(message=_l('The field can only contain 0-9 a-z A-Z / . - : _ @')), Length(4, 64)])
email = StringField(_l('Email'), validators=[Regexp(r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)', message=_l('The field requires a valid email-address.')), Length(4, 64)])
#telephone = StringField(_l('Mobile'), validators=[DataRequired(message=_l('The field can only contain 0-9 +')), Length(4, 18)])
submit = SubmitField(label=_l('Create Employee'))
class TestGuestForm(FlaskForm):
username = StringField(_l('Username'), validators=[Regexp(r'^[0-9a-zA-Z/\.\-:_@\S]+$', message=_l('The field can only contain 0-9 a-z A-Z / . - : _ @')), Length(1, 128)])
submit = SubmitField(label=_l('Create Guest'))
class QuickGuestForm(FlaskForm):
one_day = SubmitField(_l('1 Day'))
three_days = SubmitField(_l('3 Days'))
five_days = SubmitField(_l('5 Days'))
seven_days = SubmitField(_l('7 Days'))
fourteen_days = SubmitField(_l('14 Days'))
thirty_days = SubmitField(_l('30 Days'))
class ChangeEmployeePasswordWithToken(FlaskForm):
email = StringField(_l('Email'), validators=[Regexp(r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)', message=_l('The field requires a valid email-address.')), Length(4, 64)])
#email = EmailField(_l('Email'), validators=[DataRequired(message=_l('The field can only contain 0-9 a-z A-Z / . - : _ @')), Length(4, 64)])
password = PasswordField(_l('New Password'), validators=[Regexp(r'^[ -~]+$', message=_l('The field can only contain ASCII characters 0x20-0x7E. Length 6-16 characters')), Length(6, 16)])
change_token = HiddenField()
submit = SubmitField(label=_l('Change Password'))
class RequestEmployeePasswordChange(FlaskForm):
email = StringField(_l('Email'), validators=[Regexp(r'(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)', message=_l('The field requires a valid email-address.')), Length(4, 64)])
#email = EmailField(_l('Email'), validators=[DataRequired(message=_l('The field can only contain 0-9 a-z A-Z / . - : _ @')), Length(4, 64)])
submit = SubmitField(label=_l('Request Password Change Link'))
@babel.localeselector
def get_locale():
available_translations = ["de", "en"]
# "en" is the default
locale = "en"
for lang in request.accept_languages.values():
if lang[:2] in available_translations:
locale = lang[:2]
break
g.locale = locale
return locale
def admin_required(view):
@functools.wraps(view)
def wrapped_view(**kwargs):
if g.user is None or (g.entitlement is None or "admin" not in g.entitlement):
return redirect(url_for('login'))
return view(**kwargs)
return wrapped_view
def guest_required(view):
@functools.wraps(view)
def wrapped_view(**kwargs):
if g.user is None:
return redirect(url_for('login'))
elif g.entitlement is None:
return redirect(url_for('login'))
elif ("guest" not in g.entitlement) and ("admin" not in g.entitlement):
return redirect(url_for('login'))
return view(**kwargs)
return wrapped_view
def employee_required(view):
@functools.wraps(view)
def wrapped_view(**kwargs):
if g.user is None:
return redirect(url_for('login'))
elif g.entitlement is None:
return redirect(url_for('login'))
elif ("employee" not in g.entitlement) and ("admin" not in g.entitlement):
return redirect(url_for('login'))
return view(**kwargs)
return wrapped_view
def login_required(view):
@functools.wraps(view)
def wrapped_view(**kwargs):
if g.user is None:
return redirect(url_for('login'))
return view(**kwargs)
return wrapped_view
def get_usernames():
try:
with open(op_userfile, "r") as op_users:
users = json.loads(op_users.read())
choices = []
for user in users.keys():
choices.append((user, user))
return choices
except json.decoder.JSONDecodeError:
pass
def check_for_undesireable_words(input):
# You may need to adapt this to your local language
undesireable_words = ["cunt", "pussy", "nigger", "penis", "fotze", "hitler", "fuck"]
for uw in undesireable_words:
if uw in input.lower():
return True
return False
def generate_admin_password():
# Doesn't contain "l", "I", "O", "0" and "1" on purpose to avoid mistyping, thx Michael
letters = "ABCDEFGHJKMNPQRSTUVWXYZ23456789abcdefghijkmnopqrstuvwxyz"
try:
r = random.SystemRandom()
except NotImplementedError as nie:
print(nie)
flash(_("Your system doesn't provide a secure random generator!"))
return False
# Generate a reasonable secure random password for Admin Accounts
random_password = "".join(r.choice(letters) for _ in range(16))
while check_for_undesireable_words(random_password):
print("Found an undesireable word in password string, generating new one!")
random_password = "".join(r.choice(letters) for _ in range(16))
return random_password
def generate_employee_username(length=6):
letters = "23456789abcdefghijkmnopqrstuvwxyz"
try:
r = random.SystemRandom()
except NotImplementedError as nie:
print(nie)
flash(_("Your system doesn't provide a secure random generator!"))
return False
# Generate a reasonable secure random password for Admin Accounts
random_employee_username = "".join(r.choice(letters) for _ in range(length))
while check_for_undesireable_words(random_employee_username):
print("Found an undesireable word in password string, generating new one!")
random_employee_username = "".join(r.choice(letters) for _ in range(length))
return random_employee_username
def create_default_op_users():
with open(op_userfile, "w") as op_users:
default_user = {}
random_password = generate_admin_password()
default_user["admin"] = {"password":pbkdf2_sha256.hash(random_password), "entitlement":["admin"]}
op_users.write(json.dumps(default_user))
os.chmod(op_userfile, 0o600)
return random_password
def create_op_user(new_user, password, entitlements):
with open(op_userfile, "r") as op_users:
users = json.loads(op_users.read())
if new_user in users.keys():
return False
with open(op_userfile, "w") as op_users:
final_entitlements = []
for entitlement in entitlements:
final_entitlements.append(entitlement)
users[new_user] = {"password":pbkdf2_sha256.hash(password), "entitlement":final_entitlements}
op_users.write(json.dumps(users))
return True
def remove_op_user(username):
with open(op_userfile, "r") as op_users:
users = json.loads(op_users.read())
if username == g.user:
return False
elif username not in users.keys():
return False
with open(op_userfile, "w") as op_users:
users.pop(username)
op_users.write(json.dumps(users))
return True
def change_op_password(username, password, new_password):
with open(op_userfile, "r") as op_users:
users = json.loads(op_users.read())
if not pbkdf2_sha256.verify(password, users[username]["password"]):
return False
with open(op_userfile, "w") as op_users:
users[username]["password"] = pbkdf2_sha256.hash(new_password)
op_users.write(json.dumps(users))
return True
def create_default_op_settings():
try:
with open(op_settingsfile, "w") as settings_fh:
default_settings = {
"guest_operator_url": "https://ov2500-upam-cportal.al-enterprise.com",
"guest_operator_username": "",
"guest_operator_password": "",
"guest_prefix": "guest",
"wifi_network": "",
"ale_rainbow_webhook": "",
"ringcentral_webhook": "",
"ms_teams_webhook": "",
"employee_prefix": "",
"employee_wifi": "",
"ove_ovc_url": "",
"validate_ove_ovc_cert": "no",
"ove_ovc_username": "",
"ove_ovc_password": "",
"smtp_server": "",
"smtp_auth": "yes",
"smtp_port": "",
"smtp_user": "",
"smtp_password": "",
"email_from_address": ""
}
settings_fh.write(json.dumps(default_settings))
os.chmod(op_settingsfile, 0o600)
except PermissionError:
sys.exit(f"Permission denied to read/write {op_settingsfile}")
return default_settings
def read_settings():
try:
with open(op_settingsfile, "r") as settings:
setting = json.loads(settings.read())
if setting["validate_ove_ovc_cert"] == "yes":
setting["check_certs"] = True
else:
setting["check_certs"] = False
except json.decoder.JSONDecodeError:
setting = create_default_op_settings()
except FileNotFoundError:
setting = create_default_op_settings()
return setting
def save_settings(guest_operator_url, guest_operator_username, guest_operator_password, guest_prefix, wifi_network,
ale_rainbow_webhook, ringcentral_webhook, ms_teams_webhook, ove_ovc_url, validate_ove_ovc_cert,
employee_wifi, employee_prefix, ove_ovc_username, ove_ovc_password,
smtp_server, smtp_auth, smtp_port, smtp_user, smtp_password, email_from_address):
with open(op_settingsfile, "w") as settings:
setting = {}
setting["guest_operator_url"] = guest_operator_url.rstrip("/")
setting["guest_operator_username"] = guest_operator_username
setting["guest_operator_password"] = guest_operator_password
setting["guest_prefix"] = guest_prefix
setting["wifi_network"] = wifi_network
setting["ale_rainbow_webhook"] = ale_rainbow_webhook
setting["ringcentral_webhook"] = ringcentral_webhook
setting["ms_teams_webhook"] = ms_teams_webhook
setting["ove_ovc_url"] = ove_ovc_url.rstrip("/")
setting["validate_ove_ovc_cert"] = validate_ove_ovc_cert
#setting["ove_ovc_api_key"] = ove_ovc_api_key
setting["employee_wifi"] = employee_wifi
setting["employee_prefix"] = employee_prefix
setting["ove_ovc_username"] = ove_ovc_username
setting["ove_ovc_password"] = ove_ovc_password
setting["smtp_server"] = smtp_server
setting["smtp_auth"] = smtp_auth
setting["smtp_port"] = smtp_port
setting["smtp_user"] = smtp_user
setting["smtp_password"] = smtp_password
setting["email_from_address"] = email_from_address
settings.write(json.dumps(setting))
return True
def valid_login(username, password):
try:
with open(op_userfile, "r") as op_users:
users = json.loads(op_users.read())
for user, values in users.items():
if user == username and pbkdf2_sha256.verify(password, values["password"]):
return True
except FileNotFoundError:
random_password = create_default_op_users()
flash(_('Default credentials created: admin/%(pw)s to login!', pw=random_password), 'success')
return False
def log_the_user_in(username):
try:
with open(op_userfile, "r") as op_users:
users = json.loads(op_users.read())
except FileNotFoundError:
pass
session["user_id"] = username
session["entitlement"] = users[username]["entitlement"]
return redirect(url_for('index'))
@app.before_request
def load_logged_in_user():
user_id = session.get("user_id")
entitlement = session.get("entitlement")
if user_id is None:
g.user = None
g.entitlement = None
else:
g.user = user_id
g.entitlement = entitlement
@app.route("/")
def index():
return render_template("index.html")
@app.route("/admin", methods=["POST", "GET"])
@admin_required
def admin():
# if request.method == "GET" and g.entitlement == "admin":
# return render_template("create_user.html")
#error = None
form = ChangeSettings.new()
if request.method == "POST" and "admin" in g.entitlement:
if save_settings(request.form["guest_operator_url"],
request.form["guest_operator_username"],
request.form["guest_operator_password"],
request.form["guest_prefix"],
request.form["wifi_network"],
request.form["ale_rainbow_webhook"],
request.form["ringcentral_webhook"],
request.form["ms_teams_webhook"],
request.form["ove_ovc_url"],
request.form["validate_ove_ovc_cert"],
#request.form["ove_ovc_api_key"]
request.form["employee_wifi"],
request.form["employee_prefix"],
request.form["ove_ovc_username"],
request.form["ove_ovc_password"],
request.form["smtp_server"],
request.form["smtp_auth"],
request.form["smtp_port"],
request.form["smtp_user"],
request.form["smtp_password"],
request.form["email_from_address"]
):
flash(_('The settings were saved!'), 'success')
return redirect(url_for('index'))
else:
#error = _l("Invalid username/password")
#print(error)
flash(_('Something went wrong!'), 'danger')
# the code below is executed if the request method
# was GET or the credentials were invalid
return render_template("change_settings.html", form=form)
@app.route("/create_user", methods=["POST", "GET"])
@admin_required
def create_user():
# if request.method == "GET" and g.entitlement == "admin":
# return render_template("create_user.html")
#error = None
form = CreateUserForm()
if request.method == "POST" and "admin" in g.entitlement:
if create_op_user(request.form["username"],
request.form["password"],
request.form.getlist("entitlement")):
flash(_('The user was successfully created!'), 'success')
return redirect(url_for('index'))
else:
#error = _l("Invalid username/password")
#print(error)
flash(_('The user you try to create already exists!'), 'danger')
# the code below is executed if the request method
# was GET or the credentials were invalid
return render_template("create_user.html", form=form)
@app.route("/change_password", methods=["POST", "GET"])
@login_required
def change_password():
# if request.method == "GET" and g.entitlement == "admin":
# return render_template("create_user.html")
#error = None
form = ChangePasswordForm()
if request.method == "POST":
if change_op_password(g.user,
request.form["current_password"],
request.form["new_password"]):
flash(_('The password was updated!'), 'success')
return redirect(url_for('index'))
else:
#error = _l("Invalid username/password")
#print(error)
flash(_('The password wasn\'t updated!'), 'danger')
# the code below is executed if the request method
# was GET or the credentials were invalid
return render_template("change_password.html", form=form)
@app.route("/remove_user", methods=["POST", "GET"])
@admin_required
def remove_user():
# if request.method == "GET" and g.entitlement == "admin":
# return render_template("create_user.html")
#error = None
form = RemoveUserForm.new()
if request.method == "POST":
if remove_op_user(request.form["username"]):
flash(_('The user was removed!'), 'success')
return redirect(url_for('index'))
else:
#error = _l("Invalid username/password")
#print(error)
flash(_('You cannot delete yourself!'), 'danger')
# the code below is executed if the request method
# was GET or the credentials were invalid
return render_template("remove_user.html", form=form)
@app.route("/login", methods=["POST", "GET"])
def login():
if request.method == "GET" and g.user:
return redirect(url_for('index'))
#error = None
form = LoginForm()
if request.method == "POST":
if valid_login(request.form["username"],
request.form["password"]):
flash(_('Login successful!'), 'success')
return log_the_user_in(request.form["username"])
else:
#error = _l("Invalid username/password")
#print(error)
flash(_('Invalid username/password!'), 'danger')
# the code below is executed if the request method
# was GET or the credentials were invalid
return render_template("login.html", form=form)
@app.route("/logout")
def logout():
# remove the username from the session if it's there
session.pop("user_id", None)
session.pop("entitlement", None)
session.clear()
return redirect(url_for("index"))
def get_guest_accounts():
settings = read_settings()
req = requests.Session()
login_header = {
"Content-Type":"application/json"
}
if settings['guest_operator_url'] == "" or settings['guest_operator_username'] == "" or settings['guest_operator_password'] == "":
flash(_('Configuration missing for OmniVista URL, Username or Password!'), 'danger')
return False
login_data = {
"username":settings['guest_operator_username'],
"password":settings['guest_operator_password'],
"identity":"Guest"
}
resp_login = req.post(f"{settings['guest_operator_url']}/sponsor/api/ham/guest/manager/judge", headers=login_header, json=login_data)
print("LOGIN: ", resp_login.status_code, resp_login.reason, resp_login.json())
if resp_login.json()['errorCode'] != 0:
flash(_('Login to OmniVista UPAM failed! Wrong username or password!'), 'danger')
return False
account_data = {
"queryBy":"Account",
"querySize":1000,
"start":""
}
resp_accounts = req.post(f"{settings['guest_operator_url']}/sponsor/api/ham/guest/account/getPageAccountList", json=account_data)
print("Accounts: ", resp_accounts.status_code, resp_accounts.reason)
#print(json.dumps(resp_accounts.json(), indent=4))
req.get(f"{settings['guest_operator_url']}/sponsor/api/ham/guest/manager/logout")
return resp_accounts.json()
def get_local_employee_accounts():
data = read_employees_file()
if data:
return data
else:
return False
def get_employee_accounts():
settings = read_settings()
req = requests.Session()
login_header = {
"Content-Type":"application/json"
}
if settings['ove_ovc_url'] == "" or settings['ove_ovc_username'] == "" or settings['ove_ovc_password'] == "":
flash(_('Configuration missing for OmniVista URL, Username or Password!'), 'danger')
return False
login_data = {
"userName":settings['ove_ovc_username'],
"password":settings['ove_ovc_password']
}
ov_login = req.post(f"{settings['ove_ovc_url']}/api/login", headers=login_header, json=login_data, verify=settings['check_certs'])
print(ov_login.status_code, ov_login.reason, "OV Login - Employee Accounts")
#print(ov_login.json())
employee_data = {
"start":"",
"querySize":1000
}
resp_accounts = req.post(f"{settings['ove_ovc_url']}/api/ham/userAccount/getPageAllAccountList", headers=login_header, json=employee_data, verify=settings['check_certs'])
#print("Accounts: ", resp_accounts.status_code, resp_accounts.reason)
print(resp_accounts.status_code, resp_accounts.reason, "OV Query - Employee Accounts - DATA")
ov_logout = req.get(f"{settings['ove_ovc_url']}/api/logout", headers=login_header, verify=settings['check_certs'])
print(ov_logout.status_code, ov_logout.reason, "OV Logout - Employee Accounts")
req.close()
return resp_accounts.json()
def create_employee_file():
with open(op_employeefile, "w") as op_employees:
default_user = {}
op_employees.write(json.dumps(default_user))
os.chmod(op_employeefile, 0o600)
def read_employees_file():
try:
with open(op_employeefile, "r") as op_employees:
users = json.loads(op_employees.read())
except FileNotFoundError:
create_employee_file()
return False
except json.decoder.JSONDecodeError:
flash(_("Can't read omniportal_employees.json file, you'll need your backup!"), "danger")
return False
return users
def update_employee_in_file(employee_mail, update_timestamp="yes"):
with open(op_employeefile, "r") as op_employees:
users = json.loads(op_employees.read())
if employee_mail not in users.keys():
return False
with open(op_employeefile, "w") as op_employees:
change_token = secrets.token_urlsafe()
users[employee_mail]["change_token"] = change_token
if update_timestamp == "yes":
users[employee_mail]["pw_timestamp"] = datetime.datetime.fromtimestamp(time.time()).strftime("%d.%m.%Y, %H:%M:%S")
op_employees.write(json.dumps(users))
return change_token
def write_employee_to_file(new_employee_mail, new_employee_id, new_employee_ov_id):
with open(op_employeefile, "r") as op_employees:
users = json.loads(op_employees.read())
if new_employee_mail in users.keys():
return False
with open(op_employeefile, "w") as op_employees:
change_token = secrets.token_urlsafe()
users[new_employee_mail] = {"change_token":change_token, "user_id":new_employee_id, "pw_timestamp":_("Never"), "ovid":new_employee_ov_id}
op_employees.write(json.dumps(users))
return change_token
def remove_employee_from_file(employee_mail):
with open(op_employeefile, "r") as op_employees:
users = json.loads(op_employees.read())
if employee_mail not in users.keys():
return False
with open(op_employeefile, "w") as op_employees:
users.pop(employee_mail)
op_employees.write(json.dumps(users))
return True
def create_employee_account(email):
# TODO: send email with link to change password
# TODO: Test without configuration!
settings = read_settings()
if settings['ove_ovc_url'] == "" or settings['ove_ovc_username'] == "" or settings['ove_ovc_password'] == "":
flash(_('Configuration missing for OmniVista URL, Username or Password!'), 'danger')
return False
email = email.lower()
existing_users = read_employees_file()
if existing_users:
if email in existing_users.keys():
flash(_('An employee account for this email-address already exists!'))
return False
req = requests.Session()
login_header = {
"Content-Type":"application/json"
}
login_data = {
"userName":settings['ove_ovc_username'],
"password":settings['ove_ovc_password']
}
random_employee_username = settings['employee_prefix'] + generate_employee_username()
unknown_password = generate_admin_password()
ov_login = req.post(f"{settings['ove_ovc_url']}/api/login", headers=login_header, json=login_data, verify=settings['check_certs'])
print(ov_login.status_code, ov_login.reason, "OV - Add Employee - LOGIN")
employee_data = {
"repeat":unknown_password,
"otherAttributesVOs":[],
"username":random_employee_username,
"password":unknown_password,
"telephone":"",
"email":"",
"description":"User managed by OmniPortal"
}
print(employee_data)
resp_create_employee = req.post(f"{settings['ove_ovc_url']}/api/ham/userAccount/addUser", headers=login_header, json=employee_data, verify=settings['check_certs'])
print(resp_create_employee.status_code, resp_create_employee.reason, "OV - Add Employee - Create User")
if resp_create_employee.json()['errorCode'] != 0:
if resp_create_employee.json()['errorMessage'] == "upam.usernameRepeat":
flash(_('An employee account with this username already exists!'))
return False
elif resp_create_employee.json()['errorMessage'] == "upam.parametersIllegal":
flash(_('Illegal parameters given!'))
return False
else:
flash(_("An error occured! Creating employee account failed!"))
return False
print(resp_create_employee.json())
flash(_('The employee account was created!'), 'success')
employee_query_data = {
"start":"",
"querySize":1000
}
resp_accounts = req.post(f"{settings['ove_ovc_url']}/api/ham/userAccount/getPageAllAccountList", headers=login_header, json=employee_query_data, verify=settings['check_certs'])
#print("Accounts: ", resp_accounts.status_code, resp_accounts.reason)
print(resp_accounts.status_code, resp_accounts.reason, "OV - Employee Accounts - Get all users")
#print(resp_accounts.json())
for employee in resp_accounts.json()["data"]:
if employee["username"] == random_employee_username:
employee_ov_id = employee["id"]
break
change_token = write_employee_to_file(email, random_employee_username, employee_ov_id)
# TODO: Send mail and remove the print
print(url_for('employee_pw', user_email=email))
print(url_for('employee_pw', _external=True, user_email=email, change_token=change_token))
ov_logout = req.get(f"{settings['ove_ovc_url']}/api/logout", headers=login_header, verify=settings['check_certs'])
print(ov_logout.status_code, ov_logout.reason, "OV - Add Employee - Logout")
req.close()
def update_employee_account(username, ov_user_id, password):
settings = read_settings()
if settings['ove_ovc_url'] == "" or settings['ove_ovc_username'] == "" or settings['ove_ovc_password'] == "":
flash(_('Configuration missing for OmniVista URL, Username or Password!'), 'danger')
return False
req = requests.Session()
login_header = {
"Content-Type":"application/json"
}
login_data = {
"userName":settings['ove_ovc_username'],
"password":settings['ove_ovc_password']
}
ov_login = req.post(f"{settings['ove_ovc_url']}/api/login", headers=login_header, json=login_data, verify=settings['check_certs'])
print(ov_login.status_code, ov_login.reason, "OV - Update Employee - LOGIN")
employee_data = {
"id": ov_user_id,
"repeat":password,
"username":username,
"password":password,
"description":"User managed by OmniPortal"
}
print(employee_data)
resp_update_employee = req.post(f"{settings['ove_ovc_url']}/api/ham/userAccount/editAccount", headers=login_header, json=employee_data, verify=settings['check_certs'])
print(resp_update_employee.status_code, resp_update_employee.reason, "OV - Update Employee - Change Employee")
if resp_update_employee.json()['errorCode'] != 0:
if resp_update_employee.json()['errorMessage'] == "upam.usernameRepeat":
flash(_('An employee account with this username already exists!'))
return False
elif resp_update_employee.json()['errorMessage'] == "upam.parametersIllegal":
flash(_('Illegal parameters given!'))
return False
else:
flash(_("An error occured! Updating employee account failed!"))
return False
print(resp_update_employee.json())
flash(_('The employee account/password was updated!'), 'success')
ov_logout = req.get(f"{settings['ove_ovc_url']}/api/logout", headers=login_header, verify=settings['check_certs'])
print(ov_logout.status_code, ov_logout.reason, "OV - Update Employee - Logout")
req.close()
return True
def remove_employee_account(ov_user_id):
settings = read_settings()
if settings['ove_ovc_url'] == "" or settings['ove_ovc_username'] == "" or settings['ove_ovc_password'] == "":
flash(_('Configuration missing for OmniVista URL, Username or Password!'), 'danger')
return False
req = requests.Session()
login_header = {
"Content-Type":"application/json"
}
login_data = {
"userName":settings['ove_ovc_username'],
"password":settings['ove_ovc_password']
}
ov_login = req.post(f"{settings['ove_ovc_url']}/api/login", headers=login_header, json=login_data, verify=settings['check_certs'])
print(ov_login.status_code, ov_login.reason, "OV - Remove Employee - LOGIN")
employee_data = []
employee_data.append(ov_user_id)
resp_remove_employee = req.post(f"{settings['ove_ovc_url']}/api/ham/userAccount/deleteAccount", headers=login_header, json=employee_data, verify=settings['check_certs'])
print(resp_remove_employee.status_code, resp_remove_employee.reason, "OV - Remove Employee")
if resp_remove_employee.json()['data'][0]['status'] != True:
return False
print(resp_remove_employee.json())
ov_logout = req.get(f"{settings['ove_ovc_url']}/api/logout", headers=login_header, verify=settings['check_certs'])
print(ov_logout.status_code, ov_logout.reason, "OV - Remove Employee - Logout")
req.close()
return True
@app.route('/employee_pw/', methods=["POST", "GET"])
@app.route('/employee_pw/<user_email>', methods=["POST", "GET"])
def employee_pw(user_email=None):
if (request.method == "GET" and user_email and request.args.get('change_token')):
existing_users = read_employees_file()
if existing_users:
if user_email in existing_users.keys():
if existing_users[user_email]["change_token"] == request.args.get('change_token'):
form = ChangeEmployeePasswordWithToken(email=user_email, change_token=request.args.get('change_token'))
return render_template("change_employee_password.html", title=_("Change Employee Password"), form=form)
else:
flash(_("Invalid email or change_token specified! This incident will be investigated!"), "danger")
return redirect(url_for('index'))
else:
flash(_("Invalid email or change_token specified! This incident will be investigated!"), "danger")
return redirect(url_for('index'))
else:
flash(_("There are no employees registered yet!"))
return redirect(url_for('index'))
elif (request.method == "GET" and not user_email):
form = RequestEmployeePasswordChange()
return render_template("change_employee_password.html", title=_("Request password change for employee"), form=form)
elif (request.method == "POST" and user_email and request.args.get('change_token')):
form=ChangeEmployeePasswordWithToken()
if not form.validate_on_submit():
return render_template("change_employee_password.html", title=_("Change Employee Password"), form=form)
existing_users = read_employees_file()
if existing_users:
if request.form.get('email') in existing_users.keys():
if existing_users[request.form.get('email')]["change_token"] == request.form.get('change_token'):
ov_username = existing_users[request.form.get('email')]['user_id']
ov_user_id = existing_users[request.form.get('email')]['ovid']
new_password = request.form.get('password')
result = update_employee_account(ov_username, ov_user_id, new_password)
if result:
update_employee_in_file(request.form.get('email'))
return redirect(url_for('index'))
else:
return render_template("change_employee_password.html", title=_("Change Employee Password"), form=form)
else:
# TODO: ALERT
flash(_("Invalid email or change_token specified! This incident will be investigated!"), "danger")
return redirect(url_for('index'))
else:
# TODO: ALERT
flash(_("Invalid email or change_token specified! This incident will be investigated!"), "danger")
return redirect(url_for('index'))
else:
flash(_("There are no employees registered yet!"))
return redirect(url_for('index'))
elif (request.method == "POST" and not user_email):
form = RequestEmployeePasswordChange()
if not form.validate_on_submit():
return render_template("change_employee_password.html", title=_("Request password change for employee"), form=form)
# TODO: Send email to user
#users = read_employees_file()
# Note that the function checks if the user exists and otherwise returns False
result = update_employee_in_file(request.form.get("email"), update_timestamp="no")
if result:
# TODO: Send mail
# flash message
send_mail(request.form.get("email"), action="forgot_password")
print(url_for('employee_pw', user_email=request.form.get("email")))
print(url_for('employee_pw', _external=True, user_email=request.form.get("email"), change_token=result))
flash(_('An email containing a link to change your password has been sent!'), 'success')
return redirect(url_for('index'))
else:
# error message that will investigate
# TODO: Alert
flash(_("Invalid email or change_token specified! This incident will be investigated!"), "danger")
return redirect(url_for('index'))
else:
flash(_("Invalid route!"))
return redirect(url_for('index'))
def quick_guest_account(days):
settings = read_settings()
req = requests.Session()
login_header = {
"Content-Type":"application/json"
}
if settings['guest_operator_url'] == "" or settings['guest_operator_username'] == "" or settings['guest_operator_password'] == "":
flash(_('Configuration missing for OmniVista URL, Username or Password!'), 'danger')
return redirect(url_for('index'))
login_data = {
"username":settings['guest_operator_username'],
"password":settings['guest_operator_password'],
"identity":"Guest"
}
resp_login = req.post(f"{settings['guest_operator_url']}/sponsor/api/ham/guest/manager/judge", headers=login_header, json=login_data)
print("LOGIN: ", resp_login.status_code, resp_login.reason, resp_login.json())
if resp_login.json()['errorCode'] != 0:
flash(_('Login to OmniVista UPAM failed! Wrong username or password!'), 'danger')
return False
letters = string.ascii_lowercase
random_username = ''.join(random.choice(letters) for _ in range(6))
# You may need to adapt this to your local language
undesireable_words = ["cunt", "pussy", "nigger", "penis", "fotze", "hitler", "fuck"]
for uw in undesireable_words:
if uw in random_username:
print("[!] Detected undesirable word in username, generating a new one!")
# I assume our chances are very low to generate two undesireable words in a row
random_username = ''.join(random.choice(letters) for _ in range(6))
break
# Doesn't contain "l", "I", "O", "0" and "1" on purpose to avoid mistyping, thx Michael
letters = "ABCDEFGHJKMNPQRSTUVWXYZ23456789abcdefghijkmnopqrstuvwxyz"
try:
r = random.SystemRandom()
except NotImplementedError as nie:
print(nie)
flash(_("Your system doesn't provide a secure random generator!"))
return False
# Generate a secure random password for Guest Accounts
random_password = "".join(r.choice(letters) for _ in range(6))
for uw in undesireable_words:
if uw in random_password.lower():
print("[!] Detected undesirable word in password, generating a new one!")