awacke1 commited on
Commit
7261fc6
β€’
1 Parent(s): fe68f15

Create backup.0517.app.py

Browse files
Files changed (1) hide show
  1. backup.0517.app.py +148 -0
backup.0517.app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import hashlib
3
+ 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')
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
+ # Function to log events using markdown
40
+ def log_event(message, emoji):
41
+ timestamp = datetime.now(central).strftime('%Y-%m-%d %H:%M:%S')
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
+
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:
55
+ st.sidebar.success("Verification link sent via SMS πŸ“¨")
56
+ log_event("Verification SMS sent", "πŸ“¨")
57
+ else:
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:
110
+ with open(file_name, 'r') as file:
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```")