-
Notifications
You must be signed in to change notification settings - Fork 1
/
ozz_auth.py
383 lines (304 loc) · 12.8 KB
/
ozz_auth.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
import os
from random import randint
from dotenv import load_dotenv
import sqlite3
import streamlit as st
import streamlit_authenticator as stauth
import smtplib
import ssl
from email.message import EmailMessage
from master_ozz.utils import print_line_of_error, ozz_master_root, setup_instance, kingdom
import ipdb
# from QueenHive import init_pollen_dbs
main_root = ozz_master_root() # os.getcwd() # hive root
load_dotenv(os.path.join(main_root, ".env"))
def register_user(authenticator, con, cur):
# write_flying_bee(54, 54)
try:
register_status = authenticator.register_user(
form_name="Sign Up", preauthorization=False, location="main"
)
if ("register_status" not in st.session_state) or (
st.session_state["register_status"] == None
):
st.session_state["register_status"] = register_status
# generate and store verification code
if "verification_code" not in st.session_state:
st.session_state["verification_code"] = randint(100000, 999999)
verification_code = st.session_state["verification_code"]
if register_status:
register_email = st.session_state["register_status"][0]
# send user verification code
send_email(
recipient=register_email,
subject="Divergent-Thinkers. Verify Email",
body=f"""
Your verification code is {verification_code}
Please enter this code in the website to complete your registration
Thank you,
Ozz
""",
)
st.success("A verification code has been sent to your email")
entered_code = st.text_input("Verification Code", max_chars=6)
if st.button("Submit"):
if int(entered_code) == verification_code:
register_email = st.session_state["register_status"][0]
register_password = st.session_state["register_status"][1]
# verification successful
update_db(authenticator, con=con, cur=cur, email=register_email, append_db=True)
send_email(
recipient=register_email,
subject="Welcome On Board Ozz!",
body=f"""
You have successful created a Ozz account. Ensure you keep your login detials safe.
Thank you,
Divergent Thinkers Team
""",
)
authenticator.direct_login(register_email, register_password)
# st.session_state["username"] = register_email
# self.password = register_password,
else:
st.error("Incorrect Code")
# write_flying_bee(54, 54)
except Exception as e:
st.error(e)
def return_db_conn():
con = sqlite3.connect("ozz_db/client_users.db")
cur = con.cursor()
return con, cur
def forgot_password(authenticator):
con, cur = return_db_conn()
try:
(email_forgot_pw, random_password,) = authenticator.forgot_password(
form_name="Reset Password", location="main"
)
if email_forgot_pw:
# notify user and update password
send_email(
recipient=email_forgot_pw,
subject="Divergent Thinkers. Forgot Password",
body=f"""
Dear {authenticator.credentials["usernames"][email_forgot_pw]["name"]},
Your new password for pollenq.com is {random_password}
Please keep this password safe.
Thank you,
PollenQ
""",
)
update_db(authenticator, con=con, cur=cur, email=email_forgot_pw)
st.success("Your new password has been sent your email!")
elif email_forgot_pw == False:
st.error("Email not found")
except Exception as e:
st.error(e)
def send_email(recipient, subject, body):
# Define email sender and receiver
pollenq_gmail = os.environ.get("pollenq_gmail")
pollenq_gmail_app_pw = os.environ.get("pollenq_gmail_app_pw")
em = EmailMessage()
em["From"] = pollenq_gmail
em["To"] = recipient
em["Subject"] = subject
em.set_content(body)
# Add SSL layer of security
context = ssl.create_default_context()
# Log in and send the email
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as smtp:
smtp.login(pollenq_gmail, pollenq_gmail_app_pw)
smtp.sendmail(pollenq_gmail, recipient, em.as_string())
def update_db(authenticator, con, cur, email, append_db=False):
"""Update a user's record, or add a new user"""
# new user detials are stored in session state
if append_db:
detials = st.session_state["new_user_creds"]
else:
detials = authenticator.credentials["usernames"][email]
password = detials["password"]
name = detials["name"]
phone_no = detials["phone_no"]
signup_date = detials["signup_date"]
last_login_date = detials["last_login_date"]
login_count = detials["login_count"]
# add new user
if append_db:
cur.execute(
"INSERT INTO users VALUES(?, ?, ?, ?, ?, ?, ?)",
(
email,
password,
name,
phone_no,
signup_date,
last_login_date,
login_count,
),
)
# update value
else:
cur.execute(
f"""
UPDATE users
SET password = "{password}",
name = "{name}",
phone_no = "{phone_no}",
signup_date = "{signup_date}",
last_login_date = "{last_login_date}",
login_count = "{login_count}"
WHERE email = "{email}"
"""
)
con.commit()
authenticator.credentials = read_user_db(cur=cur)
def reset_password(authenticator, email, location="main"):
con, cur = return_db_conn()
try:
if authenticator.reset_password(email, "", location=location):
update_db(authenticator, con=con, cur=cur, email=email)
send_email(
recipient=email,
subject="Divergent Thinkers. Password Changed",
body=f"""
Dear {authenticator.credentials["usernames"][email]["name"]},
You are recieving this email because your password for Divergent-Thinkers.com has been changed.
If you did not authorize this change please contact us immediately.
Thank you,
Ozz
""",
)
st.success("Password changed successfully")
except Exception as e:
st.error(e)
def check_if_table_exists(cur, conn):
cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='users'")
table_exists = cur.fetchone()
# If the 'users' table doesn't exist, create it
if not table_exists:
cur.execute('''
CREATE TABLE users (
id INTEGER PRIMARY KEY,
password TEXT,
name TEXT,
phone_no TEXT,
signup_date TEXT,
last_login_date TEXT,
login_count INTEGER
)
''')
conn.commit()
def read_user_db(cur):
users = cur.execute("SELECT * FROM users").fetchall()
creds = {}
for user in users:
creds[user[0]] = {
"password": user[1],
"name": user[2],
"phone_no": user[3],
"signup_date": user[4],
"last_login_date": user[5],
"login_count": user[6],
}
return {"usernames": creds}
def signin_main():
"""Return True or False if the user is signed in"""
def setup_user_pollenqdbs():
if 'sneak_key' in st.session_state and st.session_state['sneak_key'] == 'family':
prod = setup_instance(client_username=st.session_state["username"], switch_env=False, force_db_root=False, queenKING=True, prod=False, init=True)
st.session_state['instance_setup'] = True
return prod
if st.session_state["authorized_user"]:
if 'admin__client_user' in st.session_state and st.session_state['admin__client_user'] != False:
st.session_state["username"] = st.session_state['admin__client_user']
st.sidebar.write(f'Swarm *{st.session_state["username"]}')
# st.sidebar.warning("Your Queen is Awaiting")
force_db_root = False
else:
# st.sidebar.info("Request For a Queen")
st.info("Below is a Preview")
force_db_root = True
if st.button("Request Access"):
send_email(recipient=os.environ.get('pollenq_gmail'), subject="DivergentThinker Request", body=f'{st.session_state["username"]} Asking for Ozz')
st.success("Message Sent To Hive Master, We'll talk soon")
prod = setup_instance(client_username=st.session_state["username"], switch_env=False, force_db_root=force_db_root, queenKING=True, init=True)
st.session_state['instance_setup'] = True
return prod
def define_authorized_user():
users_allowed_queen_email = kingdom()
# print(st.session_state["username"])
if st.session_state["username"] in users_allowed_queen_email:
st.session_state["confirmed_user"] = True
else:
st.session_state["confirmed_user"] = False
st.session_state["authorized_user"] = True
st.session_state["admin"] = (
True
if st.session_state["username"] in ["[email protected]"]
else False
)
return setup_user_pollenqdbs()
# def main_func__signIn():
try:
con = sqlite3.connect("ozz_db/client_users.db")
cur = con.cursor()
check_if_table_exists(cur, conn=con)
credentials = read_user_db(cur)
# Create authenticator object
authenticator = stauth.Authenticate(
credentials=credentials,
cookie_name=os.environ.get("cookie_name"),
key=os.environ.get("cookie_key"),
cookie_expiry_days=int(os.environ.get("cookie_expiry_days")),
preauthorized={"emails": "na"},
)
# Check login. Automatically gets stored in session state
if 'sneak_key' in st.session_state and st.session_state['sneak_key'].lower() == 'family':
authentication_status = True
st.session_state['name'] = '[email protected]'
st.session_state['auth_email'] = "[email protected]"
st.session_state['auth_name'] = "Kings Guest"
st.session_state['auth_pw'] = os.environ.get("quantqueen_pw")
name, authentication_status, email = authenticator.direct_login(st.session_state['auth_email'], os.environ.get("quantqueen_pw"))
st.session_state['authentication_status'] = True
authenticator.logout("Logout", location='sidebar')
define_authorized_user()
return authenticator
else:
name, authentication_status, email = authenticator.login("Login", "main")
st.session_state['auth_email'] = email
st.session_state['auth_name'] = name
if authentication_status:
if 'logout' in st.session_state and st.session_state["logout"] != True:
# authenticator.logout("Logout", location='sidebar')
# reset_password(authenticator, email, location='sidebar')
# Returning Customer
if 'authorized_user' in st.session_state and st.session_state['authorized_user'] == True:
if 'instance_setup' in st.session_state and st.session_state['instance_setup']:
return authenticator # no need to setup again right?
define_authorized_user()
return authenticator
else:
update_db(authenticator, con=con, cur=cur, email=email)
define_authorized_user()
return authenticator
# login unsucessful; forgot password or create account
elif authentication_status == False:
st.session_state["authorized_user"] = False
st.error("Email/password is incorrect")
with st.expander("Forgot Password", expanded=True):
forgot_password(authenticator)
with st.expander("New User"):
register_user(authenticator, con, cur)
return authenticator
# no login trial; create account
elif authentication_status == None:
with st.expander("New User Create Account"):
register_user(authenticator, con, cur)
# display_for_unAuth_client_user()
return authenticator
except Exception as e:
print('ERROR auth', e)
print_line_of_error()
if __name__ == "__main__":
st.session_state["logout"] = True
signin_main()