Spaces:
Sleeping
Sleeping
import os | |
import hashlib | |
import streamlit as st | |
import textflowsms as tf | |
from datetime import datetime | |
import pytz | |
import base64 | |
# Central Time Zone Adjustment | |
central = pytz.timezone('US/Central') | |
# Function to format phone number | |
def format_phone_number(phone_number): | |
if len(phone_number) == 10 and not phone_number.startswith('+'): | |
return '+1' + phone_number | |
return phone_number | |
# Function to hash a password | |
def hash_password(password): | |
return hashlib.sha256(password.encode()).hexdigest() | |
# Function to save user data to a file | |
def save_user_data(phone_number, username, password_hash, file_suffix): | |
timestamp = datetime.now(central).strftime('%d%m%y-%H-%M') | |
file_name = f"phone-{timestamp}.{file_suffix}" | |
with open(file_name, 'w') as file: | |
file.write(f"{phone_number},{username},{password_hash}\n") | |
return file_name | |
# Function to check if user is authenticated | |
def is_user_authenticated(phone_number, username, hash_value): | |
for file_name in os.listdir(): | |
if file_name.startswith('phone-') and file_name.endswith('.confirmed'): | |
with open(file_name, 'r') as file: | |
for line in file: | |
stored_phone, stored_username, stored_hash = line.strip().split(',') | |
if stored_phone == phone_number and stored_username == username and stored_hash == hash_value: | |
return True | |
return False | |
# Function to log events using markdown | |
def log_event(message, emoji): | |
timestamp = datetime.now(central).strftime('%Y-%m-%d %H:%M:%S') | |
st.markdown(f"{emoji} **{timestamp}:** {message}") | |
# Function to send verification SMS | |
def send_verification_sms(phone_number, password_hash, is_registration=False): | |
api_key = os.getenv('API_KEY') | |
tf.useKey(api_key) | |
base_url = "https://huggingface.co/spaces/awacke1/RT-SMS-Phone-Verify" | |
phone = format_phone_number(phone_number) | |
hash_message = f"Verify here: {base_url}?hash={password_hash}" | |
if is_registration: | |
message = f"Please confirm your registration by clicking the link: {hash_message}" | |
else: | |
message = f"Please confirm your login by clicking the link: {hash_message}" | |
result = tf.sendSMS(phone, message) | |
if result.ok: | |
st.sidebar.success("Verification link sent via SMS π¨") | |
log_event("Verification SMS sent", "π¨") | |
else: | |
st.sidebar.error("Failed to send SMS β") | |
log_event("Failed to send SMS", "β") | |
# Initialize session state | |
if 'phone_number' not in st.session_state: | |
st.session_state['phone_number'] = '+19522583980' | |
if 'username' not in st.session_state: | |
st.session_state['username'] = '' | |
if 'password' not in st.session_state: | |
st.session_state['password'] = '' | |
if 'hash' not in st.session_state: | |
st.session_state['hash'] = '' | |
if 'authenticated' not in st.session_state: | |
st.session_state['authenticated'] = False | |
# Sidebar inputs | |
user_phone_input = st.sidebar.text_input("π± Mobile Phone", value=st.session_state.get('phone_number', '')) | |
username_input = st.sidebar.text_input("π€ Set Username") | |
password_input = st.sidebar.text_input("π Set Password", type='password') | |
# Registration button | |
if st.sidebar.button('π Register'): | |
st.session_state['phone_number'] = format_phone_number(user_phone_input) | |
st.session_state['username'] = username_input | |
if password_input: | |
hashed_password = hash_password(password_input) | |
st.session_state['password'] = hashed_password | |
file_name = save_user_data(st.session_state['phone_number'], st.session_state['username'], hashed_password, 'pending') | |
st.sidebar.success(f"Registration pending! Data saved in {file_name}") | |
log_event("Registration pending", "π") | |
send_verification_sms(st.session_state['phone_number'], hashed_password, is_registration=True) | |
# Login button | |
if st.sidebar.button('π Login'): | |
st.session_state['phone_number'] = format_phone_number(user_phone_input) | |
if password_input: | |
hashed_password = hash_password(password_input) | |
st.session_state['password'] = hashed_password | |
if is_user_authenticated(st.session_state['phone_number'], st.session_state['username'], hashed_password): | |
send_verification_sms(st.session_state['phone_number'], hashed_password) | |
else: | |
st.sidebar.error("Login Denied - Check your password β") | |
log_event("Login denied", "β") | |
# URL hash handling | |
query_params = st.query_params | |
if 'hash' in query_params: | |
hash_from_url = query_params['hash'][0] | |
for file_name in os.listdir(): | |
if file_name.startswith('phone-') and file_name.endswith('.pending'): | |
with open(file_name, 'r') as pending_file: | |
for line in pending_file: | |
phone, username, password_hash = line.strip().split(',') | |
if password_hash == hash_from_url: | |
# Move user from .pending to .confirmed | |
os.remove(file_name) | |
save_user_data(phone, username, password_hash, 'confirmed') | |
save_user_data(phone, username, password_hash, 'confirmedwithpassword') | |
st.session_state['authenticated'] = True | |
log_event("Registration confirmed", "β ") | |
break | |
elif file_name.startswith('phone-') and file_name.endswith('.confirmed'): | |
with open(file_name, 'r') as confirmed_file: | |
for line in confirmed_file: | |
phone, username, password_hash = line.strip().split(',') | |
if password_hash == hash_from_url: | |
st.session_state['authenticated'] = True | |
log_event("Login successful", "β ") | |
break | |
# Display the main area if authenticated | |
if st.session_state['authenticated']: | |
st.write("## Main Area") | |
# Display active user accounts | |
st.write("## π Active User Accounts") | |
confirmed_files = [f for f in os.listdir() if f.startswith('phone-') and f.endswith('.confirmed')] | |
# Create a markdown table for active user accounts | |
accounts_markdown = "| Phone Number | Username |\n| --- | --- |\n" | |
for file_name in confirmed_files: | |
with open(file_name, 'r') as file: | |
phone, username, _ = file.readline().strip().split(',') | |
accounts_markdown += f"| {phone} | {username} |\n" | |
st.markdown(accounts_markdown) | |
else: | |
# If not authenticated, display a message | |
st.write("## β Authentication Required") | |
st.write("Please register or login using the link sent to your mobile phone.") | |
# Function to create a base64-encoded download link | |
def create_download_link(file_name): | |
with open(file_name, 'rb') as f: | |
bytes = f.read() | |
b64 = base64.b64encode(bytes).decode() | |
href = f'<a href="data:file/txt;base64,{b64}" download="{file_name}">Download {file_name}</a>' | |
return href | |
# Display a list of history files with download links and contents | |
st.write("## ποΈ File History") | |
# Iterate over history files and display them | |
history_files = [f for f in os.listdir() if f.startswith('phone-')] | |
for file_name in history_files: | |
# Create download link | |
download_link = create_download_link(file_name) | |
# Read and display file contents | |
with open(file_name, 'r') as file: | |
file_contents = file.read().strip() | |
# Display file information and contents in markdown | |
st.markdown(f"### {file_name}") | |
st.markdown(download_link, unsafe_allow_html=True) | |
st.markdown("```python\n" + file_contents + "\n```") |