awacke1 commited on
Commit
c545f34
Β·
1 Parent(s): 56f4a9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -71
app.py CHANGED
@@ -21,7 +21,7 @@ def hash_password(password):
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}.txt"
25
  with open(file_name, 'w') as file:
26
  file.write(f"{password_hash}\n")
27
  return file_name
@@ -29,7 +29,7 @@ def save_user_data(phone_number, password_hash):
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 phone_number in file_name:
33
  with open(file_name, 'r') as file:
34
  stored_hash = file.readline().strip()
35
  if stored_hash == hash_value:
@@ -48,7 +48,7 @@ def send_verification_sms(phone_number, password_hash):
48
 
49
  base_url = "https://huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify"
50
  phone = format_phone_number(phone_number)
51
- hash_message = f"Verify here: {base_url}?hash={password_hash}"
52
 
53
  result = tf.sendSMS(phone, hash_message)
54
  if result.ok:
@@ -58,52 +58,44 @@ def send_verification_sms(phone_number, password_hash):
58
  st.sidebar.error("Failed to send SMS ❌")
59
  log_event("Failed to send SMS", "❌")
60
 
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:
67
- st.session_state['hash'] = ''
68
- if 'authenticated' not in st.session_state:
69
- st.session_state['authenticated'] = False
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
- # Save button
76
- if st.sidebar.button('πŸ’Ύ Save Settings'):
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
- file_name = save_user_data(st.session_state['phone_number'], hashed_password)
82
- st.sidebar.success(f"Settings saved successfully! Data saved in {file_name}")
83
- log_event("Settings saved successfully", "πŸ’Ύ")
84
- send_verification_sms(st.session_state['phone_number'], hashed_password)
85
 
86
  # URL hash handling
87
  query_params = st.experimental_get_query_params()
88
- if 'phone' in query_params:
89
  phone_from_url = format_phone_number(query_params['phone'][0])
90
- st.session_state['phone_number'] = phone_from_url
91
- is_auth = any(phone_from_url in file for file in os.listdir() if file.startswith('phone-'))
92
- st.session_state['authenticated'] = is_auth
93
 
94
- if is_auth:
95
- log_event("User authenticated using phone number from URL", "βœ…")
 
 
96
  else:
97
- log_event("No authentication records found for the phone number in URL", "❌")
 
98
 
99
  # Display the main area if authenticated
100
- if st.session_state['authenticated']:
101
  st.write("## Main Area")
102
-
103
  # Display history for the specific phone number
104
- st.write("## πŸ“œ Authenticated! Below is Your Verification History")
105
- history_files = [f for f in os.listdir() if f.startswith('phone-') and st.session_state['phone_number'] in f]
106
-
107
  # Create a markdown table for history
108
  history_markdown = "| Filename | Hash Value |\n| --- | --- |\n"
109
  for file_name in history_files:
@@ -111,38 +103,7 @@ if st.session_state['authenticated']:
111
  history_markdown += f"| {file_name} | {file.read().strip()} |\n"
112
  st.markdown(history_markdown)
113
 
114
- st.markdown(history_markdown)
115
  else:
116
  # If not authenticated, display a message
117
  st.write("## ❗ Authentication Required")
118
- st.write("Please authenticate using the link sent to your mobile phone.")
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):
126
- with open(file_name, 'rb') as f:
127
- bytes = f.read()
128
- b64 = base64.b64encode(bytes).decode()
129
- href = f'<a href="data:file/txt;base64,{b64}" download="{file_name}">Download {file_name}</a>'
130
- return href
131
-
132
- # Display a list of history files with download links and contents
133
- st.write("## πŸ—‚οΈ File History")
134
-
135
- # Iterate over history files and display them
136
- history_files = [f for f in os.listdir() if f.startswith('phone-')]
137
- for file_name in history_files:
138
- # Create download link
139
- download_link = create_download_link(file_name)
140
-
141
- # Read and display file contents
142
- with open(file_name, 'r') as file:
143
- file_contents = file.read().strip()
144
-
145
- # Display file information and contents in markdown
146
- st.markdown(f"### {file_name}")
147
- st.markdown(download_link, unsafe_allow_html=True)
148
- st.markdown("```python\n" + file_contents + "\n```")
 
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-{phone_number}-{timestamp}.txt"
25
  with open(file_name, 'w') as file:
26
  file.write(f"{password_hash}\n")
27
  return file_name
 
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(f'phone-{phone_number}') and file_name.endswith('.txt'):
33
  with open(file_name, 'r') as file:
34
  stored_hash = file.readline().strip()
35
  if stored_hash == hash_value:
 
48
 
49
  base_url = "https://huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify"
50
  phone = format_phone_number(phone_number)
51
+ hash_message = f"Verify here: {base_url}?phone={phone}&hash={password_hash}"
52
 
53
  result = tf.sendSMS(phone, hash_message)
54
  if result.ok:
 
58
  st.sidebar.error("Failed to send SMS ❌")
59
  log_event("Failed to send SMS", "❌")
60
 
61
+ # Sidebar inputs for login
62
+ phone_input = st.sidebar.text_input("Phone Number")
63
+ password_input = st.sidebar.text_input("Password", type="password")
64
+
65
+ # Button to handle login
66
+ if st.sidebar.button("Login"):
67
+ phone_formatted = format_phone_number(phone_input)
68
+ password_hashed = hash_password(password_input)
69
+
70
+ # Check if user is authenticated
71
+ if is_user_authenticated(phone_formatted, password_hashed):
72
+ st.session_state['authenticated'] = True
73
+ st.session_state['phone_number'] = phone_formatted
74
+ st.success("Logged in successfully!")
75
+ else:
76
+ st.error("Invalid phone number or password.")
 
 
 
 
 
 
 
 
77
 
78
  # URL hash handling
79
  query_params = st.experimental_get_query_params()
80
+ if 'phone' in query_params and 'hash' in query_params:
81
  phone_from_url = format_phone_number(query_params['phone'][0])
82
+ hash_from_url = query_params['hash'][0]
 
 
83
 
84
+ if is_user_authenticated(phone_from_url, hash_from_url):
85
+ st.session_state['authenticated'] = True
86
+ st.session_state['phone_number'] = phone_from_url
87
+ log_event("User authenticated using URL parameters", "βœ…")
88
  else:
89
+ st.error(f"Validation failed. Phone: {phone_from_url}, Hash: {hash_from_url}")
90
+ log_event("Validation failed using URL parameters", "❌")
91
 
92
  # Display the main area if authenticated
93
+ if st.session_state.get('authenticated', False):
94
  st.write("## Main Area")
 
95
  # Display history for the specific phone number
96
+ st.write("## πŸ“œ Verification History")
97
+ history_files = [f for f in os.listdir() if f.startswith(f'phone-{st.session_state["phone_number"]}')]
98
+
99
  # Create a markdown table for history
100
  history_markdown = "| Filename | Hash Value |\n| --- | --- |\n"
101
  for file_name in history_files:
 
103
  history_markdown += f"| {file_name} | {file.read().strip()} |\n"
104
  st.markdown(history_markdown)
105
 
 
106
  else:
107
  # If not authenticated, display a message
108
  st.write("## ❗ Authentication Required")
109
+ st.write("Please login to view your file history.")