awacke1 commited on
Commit
1e829ad
Β·
1 Parent(s): 245df17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -55
app.py CHANGED
@@ -2,89 +2,88 @@ import os
2
  import hashlib
3
  import streamlit as st
4
  import textflowsms as tf
 
5
 
 
 
 
6
 
7
- # Function to ensure phone number is correctly formatted
8
  def format_phone_number(phone_number):
9
  if len(phone_number) == 10 and not phone_number.startswith('+'):
10
  return '+1' + phone_number
11
  return phone_number
12
 
13
- # Initialize session state variables if they don't exist
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  if 'phone_number' not in st.session_state:
15
  st.session_state['phone_number'] = '+19522583980' # Default phone number
16
  if 'password' not in st.session_state:
17
  st.session_state['password'] = ''
18
  if 'hash' not in st.session_state:
19
  st.session_state['hash'] = ''
 
 
20
 
21
- # Function to hash a password
22
- def hash_password(password):
23
- return hashlib.sha256(password.encode()).hexdigest()[:6]
24
-
25
- # Function to append to the log file or create it if it doesn't exist
26
- def append_to_log(phone_number):
27
- if not os.path.exists('LoggedConfirmations.txt'):
28
- with open('LoggedConfirmations.txt', 'w') as log_file:
29
- log_file.write(f"{phone_number}\n")
30
- else:
31
- with open('LoggedConfirmations.txt', 'a') as log_file:
32
- log_file.write(f"{phone_number}\n")
33
-
34
- # Mobile Phone field with emoji
35
  user_phone_input = st.sidebar.text_input("πŸ“± Mobile Phone", value=st.session_state.get('phone_number', ''))
36
-
37
- # Password field with emoji
38
  password_input = st.sidebar.text_input("πŸ”‘ Set Password", type='password')
39
 
40
- # Button to save the phone number and password
41
  if st.sidebar.button('πŸ’Ύ Save Settings'):
42
  st.session_state['phone_number'] = format_phone_number(user_phone_input)
43
- st.session_state['password'] = hash_password(password_input)
44
- st.sidebar.success("Settings saved successfully!")
 
 
 
 
45
 
46
  # Function to send verification SMS
47
  def send_verification_sms():
48
-
49
- # Load the API key from an environment variable
50
- api_key = os.getenv('API_KEY')
51
- tf.useKey(api_key)
52
- base_url = "https://huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify"
53
- base_url2 = "huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify" # SMS - remove the https:// since SMS only likes plain text links without protocol
54
- phone=user_phone_input
55
- phone_session=st.session_state['phone_number']
56
- st.write('Sending SMS to phone number ' + phone)
57
- hashmessage=f"Verify here: {base_url}?hash={st.session_state['password']}"
58
- hashmessage2=f"Verify here {base_url}?hash={st.session_state['password']}"
59
- #st.write('Hash message: ' + hashmessage)
60
- result = tf.sendSMS(phone, hashmessage2)
61
-
62
- if(result.ok):
63
- st.write('Success with result data:')
64
- st.write(result.data)
65
- st.sidebar.success("Verification link sent via SMS")
66
- else:
67
- st.write('Failure with result message:' + result.message)
68
- #st.sidebar.success("Verification link sent via SMS")
69
 
70
-
71
- # Check if the hash parameter is provided
72
  query_params = st.experimental_get_query_params()
73
  if 'hash' in query_params:
74
  st.session_state['hash'] = query_params['hash'][0]
75
- append_to_log(st.session_state['phone_number'])
76
- st.write("βœ… User has authenticated using text")
 
77
 
78
- # Show verification button if hash is not in session
79
- if not st.session_state['hash']:
 
 
 
80
  if st.sidebar.button('πŸ“¨ Send Verify Request'):
81
  send_verification_sms()
82
 
83
- # Display history log
84
  st.write("## πŸ“œ Verification History")
85
- if os.path.exists('LoggedConfirmations.txt'):
86
- with open('LoggedConfirmations.txt', 'r') as log_file:
87
- history = log_file.read()
88
- st.text(history)
89
- else:
90
- st.write("No verification history found.")
 
2
  import hashlib
3
  import streamlit as st
4
  import textflowsms as tf
5
+ from datetime import datetime
6
 
7
+ # Central Time Zone Adjustment
8
+ import pytz
9
+ central = pytz.timezone('US/Central')
10
 
11
+ # Function to format phone number
12
  def format_phone_number(phone_number):
13
  if len(phone_number) == 10 and not phone_number.startswith('+'):
14
  return '+1' + phone_number
15
  return phone_number
16
 
17
+ # Function to hash a password
18
+ 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}.txt"
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 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:
36
+ return True
37
+ return False
38
+
39
+ # Initialize session state
40
  if 'phone_number' not in st.session_state:
41
  st.session_state['phone_number'] = '+19522583980' # Default phone number
42
  if 'password' not in st.session_state:
43
  st.session_state['password'] = ''
44
  if 'hash' not in st.session_state:
45
  st.session_state['hash'] = ''
46
+ if 'authenticated' not in st.session_state:
47
+ st.session_state['authenticated'] = False
48
 
49
+ # Sidebar inputs
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  user_phone_input = st.sidebar.text_input("πŸ“± Mobile Phone", value=st.session_state.get('phone_number', ''))
 
 
51
  password_input = st.sidebar.text_input("πŸ”‘ Set Password", type='password')
52
 
53
+ # Save button
54
  if st.sidebar.button('πŸ’Ύ Save Settings'):
55
  st.session_state['phone_number'] = format_phone_number(user_phone_input)
56
+ hashed_password = hash_password(password_input)
57
+ st.session_state['password'] = hashed_password
58
+ file_name = save_user_data(st.session_state['phone_number'], hashed_password)
59
+ st.sidebar.success(f"Settings saved successfully! Data saved in {file_name}")
60
+ # Record save event with timestamp
61
+ st.session_state['save_time'] = datetime.now(central).strftime('%Y-%m-%d %H:%M:%S')
62
 
63
  # Function to send verification SMS
64
  def send_verification_sms():
65
+ # [SMS sending logic remains the same]
66
+ # ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
+ # URL hash handling
 
69
  query_params = st.experimental_get_query_params()
70
  if 'hash' in query_params:
71
  st.session_state['hash'] = query_params['hash'][0]
72
+ st.session_state['authenticated'] = is_user_authenticated(st.session_state['phone_number'], st.session_state['hash'])
73
+ if st.session_state['authenticated']:
74
+ st.write("βœ… User has authenticated using text")
75
 
76
+ # Display the main area if authenticated
77
+ if st.session_state['authenticated']:
78
+ st.write("## Main Area")
79
+ # [Display main area content]
80
+ else:
81
  if st.sidebar.button('πŸ“¨ Send Verify Request'):
82
  send_verification_sms()
83
 
84
+ # Display history for the specific phone number
85
  st.write("## πŸ“œ Verification History")
86
+ history_files = [f for f in os.listdir() if f.startswith('phone-') and st.session_state['phone_number'] in f]
87
+ for file_name in history_files:
88
+ with open(file_name, 'r') as file:
89
+ st.write(f"{file_name}: {file.read()}")