File size: 5,619 Bytes
7261fc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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-{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('phone-') and phone_number in file_name:
            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}?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", "❌")

# Initialize session state
if 'phone_number' not in st.session_state:
    st.session_state['phone_number'] = '+19522583980'
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', ''))
password_input = st.sidebar.text_input("πŸ”‘ Set Password", type='password')

# Save button
if st.sidebar.button('πŸ’Ύ Save Settings'):
    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
        file_name = save_user_data(st.session_state['phone_number'], hashed_password)
        st.sidebar.success(f"Settings saved successfully! Data saved in {file_name}")
        log_event("Settings saved successfully", "πŸ’Ύ")
        send_verification_sms(st.session_state['phone_number'], hashed_password)

# URL hash handling
query_params = st.experimental_get_query_params()
if 'phone' in query_params:
    phone_from_url = format_phone_number(query_params['phone'][0])
    st.session_state['phone_number'] = phone_from_url
    is_auth = any(phone_from_url in file for file in os.listdir() if file.startswith('phone-'))
    st.session_state['authenticated'] = is_auth

    if is_auth:
        log_event("User authenticated using phone number from URL", "βœ…")
    else:
        log_event("No authentication records found for the phone number in URL", "❌")

# Display the main area if authenticated
if st.session_state['authenticated']:
    st.write("## Main Area")
    
    # Display history for the specific phone number
    st.write("## πŸ“œ Authenticated! Below is Your Verification History")
    history_files = [f for f in os.listdir() if f.startswith('phone-') and st.session_state['phone_number'] in f]
    
    # 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)

    st.markdown(history_markdown)
else:
    # If not authenticated, display a message
    st.write("## ❗ Authentication Required")
    st.write("Please authenticate using the link sent to your mobile phone.")


# Import additional libraries
import base64

# 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```")