Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import streamlit as st
|
|
4 |
import textflowsms as tf
|
5 |
from datetime import datetime
|
6 |
import pytz
|
|
|
7 |
|
8 |
# Central Time Zone Adjustment
|
9 |
central = pytz.timezone('US/Central')
|
@@ -19,21 +20,22 @@ def hash_password(password):
|
|
19 |
return hashlib.sha256(password.encode()).hexdigest()
|
20 |
|
21 |
# Function to save user data to a file
|
22 |
-
def save_user_data(phone_number, password_hash):
|
23 |
timestamp = datetime.now(central).strftime('%d%m%y-%H-%M')
|
24 |
-
file_name = f"phone-{timestamp}.
|
25 |
with open(file_name, 'w') as file:
|
26 |
-
file.write(f"{password_hash}\n")
|
27 |
return file_name
|
28 |
|
29 |
# Function to check if user is authenticated
|
30 |
-
def is_user_authenticated(phone_number, hash_value):
|
31 |
for file_name in os.listdir():
|
32 |
-
if file_name.startswith('phone-') and
|
33 |
with open(file_name, 'r') as file:
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
37 |
return False
|
38 |
|
39 |
# Function to log events using markdown
|
@@ -42,7 +44,7 @@ def log_event(message, emoji):
|
|
42 |
st.markdown(f"{emoji} **{timestamp}:** {message}")
|
43 |
|
44 |
# Function to send verification SMS
|
45 |
-
def send_verification_sms(phone_number, password_hash):
|
46 |
api_key = os.getenv('API_KEY')
|
47 |
tf.useKey(api_key)
|
48 |
|
@@ -50,7 +52,12 @@ def send_verification_sms(phone_number, password_hash):
|
|
50 |
phone = format_phone_number(phone_number)
|
51 |
hash_message = f"Verify here: {base_url}?hash={password_hash}"
|
52 |
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
54 |
if result.ok:
|
55 |
st.sidebar.success("Verification link sent via SMS π¨")
|
56 |
log_event("Verification SMS sent", "π¨")
|
@@ -61,6 +68,8 @@ def send_verification_sms(phone_number, password_hash):
|
|
61 |
# Initialize session state
|
62 |
if 'phone_number' not in st.session_state:
|
63 |
st.session_state['phone_number'] = '+19522583980'
|
|
|
|
|
64 |
if 'password' not in st.session_state:
|
65 |
st.session_state['password'] = ''
|
66 |
if 'hash' not in st.session_state:
|
@@ -70,56 +79,78 @@ if 'authenticated' not in st.session_state:
|
|
70 |
|
71 |
# Sidebar inputs
|
72 |
user_phone_input = st.sidebar.text_input("π± Mobile Phone", value=st.session_state.get('phone_number', ''))
|
|
|
73 |
password_input = st.sidebar.text_input("π Set Password", type='password')
|
74 |
|
75 |
-
#
|
76 |
-
if st.sidebar.button('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
st.session_state['phone_number'] = format_phone_number(user_phone_input)
|
78 |
if password_input:
|
79 |
hashed_password = hash_password(password_input)
|
80 |
st.session_state['password'] = hashed_password
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
85 |
|
86 |
# URL hash handling
|
87 |
query_params = st.experimental_get_query_params()
|
88 |
-
if '
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
# Display the main area if authenticated
|
100 |
if st.session_state['authenticated']:
|
101 |
st.write("## Main Area")
|
102 |
|
103 |
-
# Display
|
104 |
-
st.write("## π
|
105 |
-
|
106 |
|
107 |
-
# Create a markdown table for
|
108 |
-
|
109 |
-
for file_name in
|
110 |
with open(file_name, 'r') as file:
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
st.markdown(history_markdown)
|
115 |
else:
|
116 |
# If not authenticated, display a message
|
117 |
st.write("## β Authentication Required")
|
118 |
-
st.write("Please
|
119 |
-
|
120 |
-
|
121 |
-
# Import additional libraries
|
122 |
-
import base64
|
123 |
|
124 |
# Function to create a base64-encoded download link
|
125 |
def create_download_link(file_name):
|
|
|
4 |
import textflowsms as tf
|
5 |
from datetime import datetime
|
6 |
import pytz
|
7 |
+
import base64
|
8 |
|
9 |
# Central Time Zone Adjustment
|
10 |
central = pytz.timezone('US/Central')
|
|
|
20 |
return hashlib.sha256(password.encode()).hexdigest()
|
21 |
|
22 |
# Function to save user data to a file
|
23 |
+
def save_user_data(phone_number, username, password_hash, file_suffix):
|
24 |
timestamp = datetime.now(central).strftime('%d%m%y-%H-%M')
|
25 |
+
file_name = f"phone-{timestamp}.{file_suffix}"
|
26 |
with open(file_name, 'w') as file:
|
27 |
+
file.write(f"{phone_number},{username},{password_hash}\n")
|
28 |
return file_name
|
29 |
|
30 |
# Function to check if user is authenticated
|
31 |
+
def is_user_authenticated(phone_number, username, hash_value):
|
32 |
for file_name in os.listdir():
|
33 |
+
if file_name.startswith('phone-') and file_name.endswith('.confirmed'):
|
34 |
with open(file_name, 'r') as file:
|
35 |
+
for line in file:
|
36 |
+
stored_phone, stored_username, stored_hash = line.strip().split(',')
|
37 |
+
if stored_phone == phone_number and stored_username == username and stored_hash == hash_value:
|
38 |
+
return True
|
39 |
return False
|
40 |
|
41 |
# Function to log events using markdown
|
|
|
44 |
st.markdown(f"{emoji} **{timestamp}:** {message}")
|
45 |
|
46 |
# Function to send verification SMS
|
47 |
+
def send_verification_sms(phone_number, password_hash, is_registration=False):
|
48 |
api_key = os.getenv('API_KEY')
|
49 |
tf.useKey(api_key)
|
50 |
|
|
|
52 |
phone = format_phone_number(phone_number)
|
53 |
hash_message = f"Verify here: {base_url}?hash={password_hash}"
|
54 |
|
55 |
+
if is_registration:
|
56 |
+
message = f"Please confirm your registration by clicking the link: {hash_message}"
|
57 |
+
else:
|
58 |
+
message = f"Please confirm your login by clicking the link: {hash_message}"
|
59 |
+
|
60 |
+
result = tf.sendSMS(phone, message)
|
61 |
if result.ok:
|
62 |
st.sidebar.success("Verification link sent via SMS π¨")
|
63 |
log_event("Verification SMS sent", "π¨")
|
|
|
68 |
# Initialize session state
|
69 |
if 'phone_number' not in st.session_state:
|
70 |
st.session_state['phone_number'] = '+19522583980'
|
71 |
+
if 'username' not in st.session_state:
|
72 |
+
st.session_state['username'] = ''
|
73 |
if 'password' not in st.session_state:
|
74 |
st.session_state['password'] = ''
|
75 |
if 'hash' not in st.session_state:
|
|
|
79 |
|
80 |
# Sidebar inputs
|
81 |
user_phone_input = st.sidebar.text_input("π± Mobile Phone", value=st.session_state.get('phone_number', ''))
|
82 |
+
username_input = st.sidebar.text_input("π€ Set Username")
|
83 |
password_input = st.sidebar.text_input("π Set Password", type='password')
|
84 |
|
85 |
+
# Registration button
|
86 |
+
if st.sidebar.button('π Register'):
|
87 |
+
st.session_state['phone_number'] = format_phone_number(user_phone_input)
|
88 |
+
st.session_state['username'] = username_input
|
89 |
+
if password_input:
|
90 |
+
hashed_password = hash_password(password_input)
|
91 |
+
st.session_state['password'] = hashed_password
|
92 |
+
file_name = save_user_data(st.session_state['phone_number'], st.session_state['username'], hashed_password, 'pending')
|
93 |
+
st.sidebar.success(f"Registration pending! Data saved in {file_name}")
|
94 |
+
log_event("Registration pending", "π")
|
95 |
+
send_verification_sms(st.session_state['phone_number'], hashed_password, is_registration=True)
|
96 |
+
|
97 |
+
# Login button
|
98 |
+
if st.sidebar.button('π Login'):
|
99 |
st.session_state['phone_number'] = format_phone_number(user_phone_input)
|
100 |
if password_input:
|
101 |
hashed_password = hash_password(password_input)
|
102 |
st.session_state['password'] = hashed_password
|
103 |
+
if is_user_authenticated(st.session_state['phone_number'], st.session_state['username'], hashed_password):
|
104 |
+
send_verification_sms(st.session_state['phone_number'], hashed_password)
|
105 |
+
else:
|
106 |
+
st.sidebar.error("Login Denied - Check your password β")
|
107 |
+
log_event("Login denied", "β")
|
108 |
|
109 |
# URL hash handling
|
110 |
query_params = st.experimental_get_query_params()
|
111 |
+
if 'hash' in query_params:
|
112 |
+
hash_from_url = query_params['hash'][0]
|
113 |
+
for file_name in os.listdir():
|
114 |
+
if file_name.startswith('phone-') and file_name.endswith('.pending'):
|
115 |
+
with open(file_name, 'r') as pending_file:
|
116 |
+
for line in pending_file:
|
117 |
+
phone, username, password_hash = line.strip().split(',')
|
118 |
+
if password_hash == hash_from_url:
|
119 |
+
# Move user from .pending to .confirmed
|
120 |
+
os.remove(file_name)
|
121 |
+
save_user_data(phone, username, password_hash, 'confirmed')
|
122 |
+
save_user_data(phone, username, password_hash, 'confirmedwithpassword')
|
123 |
+
st.session_state['authenticated'] = True
|
124 |
+
log_event("Registration confirmed", "β
")
|
125 |
+
break
|
126 |
+
elif file_name.startswith('phone-') and file_name.endswith('.confirmed'):
|
127 |
+
with open(file_name, 'r') as confirmed_file:
|
128 |
+
for line in confirmed_file:
|
129 |
+
phone, username, password_hash = line.strip().split(',')
|
130 |
+
if password_hash == hash_from_url:
|
131 |
+
st.session_state['authenticated'] = True
|
132 |
+
log_event("Login successful", "β
")
|
133 |
+
break
|
134 |
|
135 |
# Display the main area if authenticated
|
136 |
if st.session_state['authenticated']:
|
137 |
st.write("## Main Area")
|
138 |
|
139 |
+
# Display active user accounts
|
140 |
+
st.write("## π Active User Accounts")
|
141 |
+
confirmed_files = [f for f in os.listdir() if f.startswith('phone-') and f.endswith('.confirmed')]
|
142 |
|
143 |
+
# Create a markdown table for active user accounts
|
144 |
+
accounts_markdown = "| Phone Number | Username |\n| --- | --- |\n"
|
145 |
+
for file_name in confirmed_files:
|
146 |
with open(file_name, 'r') as file:
|
147 |
+
phone, username, _ = file.readline().strip().split(',')
|
148 |
+
accounts_markdown += f"| {phone} | {username} |\n"
|
149 |
+
st.markdown(accounts_markdown)
|
|
|
150 |
else:
|
151 |
# If not authenticated, display a message
|
152 |
st.write("## β Authentication Required")
|
153 |
+
st.write("Please register or login using the link sent to your mobile phone.")
|
|
|
|
|
|
|
|
|
154 |
|
155 |
# Function to create a base64-encoded download link
|
156 |
def create_download_link(file_name):
|