LinuxDayDemo / app.py
vagrillo's picture
Update app.py
d18fd51 verified
raw
history blame
5.75 kB
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)