Spaces:
Running
Running
import os | |
import hashlib | |
import streamlit as st | |
import textflowsms as tf | |
from datetime import datetime | |
import pytz | |
# 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, password_hash): | |
timestamp = datetime.now(central).strftime('%d%m%y-%H-%M') | |
file_name = f"phone-{phone_number}-{timestamp}.txt" | |
with open(file_name, 'w') as file: | |
file.write(f"{password_hash}\n") | |
return file_name | |
# Function to check if user is authenticated | |
def is_user_authenticated(phone_number, hash_value): | |
for file_name in os.listdir(): | |
if file_name.startswith(f'phone-{phone_number}') and file_name.endswith('.txt'): | |
with open(file_name, 'r') as file: | |
stored_hash = file.readline().strip() | |
if 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): | |
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}?phone={phone}&hash={password_hash}" | |
result = tf.sendSMS(phone, hash_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", "β") | |
# Sidebar inputs for login | |
phone_input = st.sidebar.text_input("Phone Number") | |
password_input = st.sidebar.text_input("Password", type="password") | |
# Button to handle login | |
if st.sidebar.button("Login"): | |
phone_formatted = format_phone_number(phone_input) | |
password_hashed = hash_password(password_input) | |
# Check if user is authenticated | |
if is_user_authenticated(phone_formatted, password_hashed): | |
st.session_state['authenticated'] = True | |
st.session_state['phone_number'] = phone_formatted | |
st.success("Logged in successfully!") | |
else: | |
st.error("Invalid phone number or password.") | |
# URL hash handling | |
query_params = st.experimental_get_query_params() | |
if 'phone' in query_params and 'hash' in query_params: | |
phone_from_url = format_phone_number(query_params['phone'][0]) | |
hash_from_url = query_params['hash'][0] | |
if is_user_authenticated(phone_from_url, hash_from_url): | |
st.session_state['authenticated'] = True | |
st.session_state['phone_number'] = phone_from_url | |
log_event("User authenticated using URL parameters", "β ") | |
else: | |
st.error(f"Validation failed. Phone: {phone_from_url}, Hash: {hash_from_url}") | |
log_event("Validation failed using URL parameters", "β") | |
# Display the main area if authenticated | |
if st.session_state.get('authenticated', False): | |
st.write("## Main Area") | |
# Display history for the specific phone number | |
st.write("## π Verification History") | |
history_files = [f for f in os.listdir() if f.startswith(f'phone-{st.session_state["phone_number"]}')] | |
# Create a markdown table for history | |
history_markdown = "| Filename | Hash Value |\n| --- | --- |\n" | |
for file_name in history_files: | |
with open(file_name, 'r') as file: | |
history_markdown += f"| {file_name} | {file.read().strip()} |\n" | |
st.markdown(history_markdown) | |
else: | |
# If not authenticated, display a message | |
st.write("## β Authentication Required") | |
st.write("Please login to view your file history.") | |