File size: 5,752 Bytes
0d635bf
 
acc59a7
0d635bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d18fd51
 
 
d15a538
0d635bf
 
d18fd51
0d635bf
 
 
 
acc59a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d635bf
 
 
 
 
 
 
 
 
 
 
 
 
bf50961
0d635bf
 
 
 
 
 
acc59a7
0d635bf
07da134
 
acc59a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07da134
 
 
 
 
 
 
 
acc59a7
 
 
 
 
 
 
07da134
 
 
0d635bf
 
 
 
 
d18fd51
0d635bf
 
acc59a7
0d635bf
acc59a7
 
 
0d635bf
 
 
acc59a7
4100ddb
acc59a7
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from flask import Flask, request, render_template, redirect, url_for, make_response
import datetime
import os

app = Flask(__name__)
SECRET_PASSWORD = "VeronaTrento25!"
COOKIE_NAME = "authenticated"

def is_authenticated():
    """Verifica se l'utente è autenticato tramite cookie"""
    auth_cookie = request.cookies.get(COOKIE_NAME)
    if auth_cookie == "true":
        return True
    return False

def set_auth_cookie(response):
    """Imposta il cookie di autenticazione per 24 ore"""
    expires = datetime.datetime.now() + datetime.timedelta(hours=24)
    response.set_cookie(
        COOKIE_NAME, 
        "true", 
        expires=expires,
        httponly=True,
        secure=False,
        path='/',  # Aggiungi questo
        samesite='Lax'  # Aggiungi questo
    )
    return response

    
@app.route('/')
def index():
    # Se l'utente è già autenticato, mostra il contenuto normale
    if is_authenticated():
        return """
        <!DOCTYPE html>
        <html>
        <head>
            <title>Sito Protetto</title>
            <style>
                body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
                .welcome { background: #e8f5e8; padding: 20px; border-radius: 5px; margin-bottom: 20px; }
                .content { background: #f0f0f0; padding: 20px; border-radius: 5px; }
                a { color: #007bff; text-decoration: none; margin-right: 15px; }
            </style>
        </head>
        <body>
            <div class="welcome">
                <h2>✅ Accesso Autorizzato!</h2>
                <p>Sei autenticato correttamente. La navigazione è libera per 24 ore.</p>
                <p><a href="/logout">🔓 Logout</a></p>
            </div>
            
            <div class="content">
                <h3>Benvenuto nel sito protetto</h3>
                <p>Questo è il contenuto principale del tuo sito web.</p>
                <p>Qui puoi inserire tutto il contenuto che vuoi proteggere con password.</p>
            </div>
        </body>
        </html>
        """
    
    # Altrimenti reindirizza alla pagina di login
    return redirect(url_for('login'))

@app.route('/login', methods=['GET', 'POST'])
def login():
    # Se l'utente è già autenticato, reindirizza alla home
    if is_authenticated():
        return redirect(url_for('index'))
    
    error = None
    if request.method == 'POST':
        password = request.form.get('password')
        
        if password == SECRET_PASSWORD:
            # Password corretta, crea risposta e imposta cookie
            response = make_response(redirect(url_for('index')))
            response = set_auth_cookie(response)
            return response
        else:
            error = "❌ Password errata. Riprova."
    
    # Costruisci l'HTML dinamicamente
    html_content = """
    <!DOCTYPE html>
    <html>
    <head>
        <title>Login Richiesto</title>
        <style>
            body { 
                font-family: Arial, sans-serif; 
                max-width: 400px; 
                margin: 100px auto; 
                padding: 20px;
                background: #f5f5f5;
            }
            .login-form { 
                background: white; 
                padding: 30px; 
                border-radius: 10px;
                box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            }
            h2 {
                color: #333;
                text-align: center;
                margin-bottom: 20px;
            }
            input[type="password"] { 
                width: 100%; 
                padding: 12px; 
                margin: 15px 0; 
                border: 1px solid #ddd; 
                border-radius: 5px;
                box-sizing: border-box;
                font-size: 16px;
            }
            button { 
                background: #007bff; 
                color: white; 
                padding: 12px 20px; 
                border: none; 
                border-radius: 5px; 
                cursor: pointer;
                width: 100%;
                font-size: 16px;
            }
            button:hover {
                background: #0056b3;
            }
            .error { 
                color: red; 
                margin-bottom: 15px;
                text-align: center;
                padding: 10px;
                background: #ffe6e6;
                border-radius: 5px;
            }
        </style>
    </head>
    <body>
        <div class="login-form">
            <h2>🔒 Accesso Protetto</h2>
            <p style="text-align: center; color: #666;">Inserisci la password per accedere al sito</p>
    """
    
    # Aggiungi la sezione error se presente
    if error:
        html_content += f'<div class="error">{error}</div>'
    
    # Aggiungi il form
    html_content += """
            <form method="POST">
                <input type="password" name="password" placeholder="Inserisci la password" required>
                <button type="submit">🔑 Accedi</button>
            </form>
        </div>
    </body>
    </html>
    """
    
    return html_content

@app.route('/logout')
def logout():
    """Route per il logout (elimina il cookie)"""
    response = make_response(redirect(url_for('login')))
    response.set_cookie(COOKIE_NAME, '', expires=0, path='/')  # Aggiungi path='/'
    return response

# Proteggi tutte le route aggiuntive che potresti avere
@app.route('/protected')
def protected():
    if not is_authenticated():
        return redirect(url_for('login'))
    return "Questa è una pagina protetta!"

if __name__ == '__main__':
    # Configurazione per Hugging Face Spaces
    port = int(os.environ.get('PORT', 7860))
    app.run(host='0.0.0.0', port=port, debug=False)