vagrillo commited on
Commit
acc59a7
·
verified ·
1 Parent(s): 946bcca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -24
app.py CHANGED
@@ -1,5 +1,6 @@
1
  from flask import Flask, request, render_template, redirect, url_for, make_response
2
  import datetime
 
3
 
4
  app = Flask(__name__)
5
  SECRET_PASSWORD = "VeronaTrento25!"
@@ -20,7 +21,7 @@ def set_auth_cookie(response):
20
  "true",
21
  expires=expires,
22
  httponly=True,
23
- secure=False # Imposta True in produzione con HTTPS
24
  )
25
  return response
26
 
@@ -28,7 +29,33 @@ def set_auth_cookie(response):
28
  def index():
29
  # Se l'utente è già autenticato, mostra il contenuto normale
30
  if is_authenticated():
31
- return render_template('index.html') # Sostituisci con il tuo template
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  # Altrimenti reindirizza alla pagina di login
34
  return redirect(url_for('login'))
@@ -49,9 +76,79 @@ def login():
49
  response = set_auth_cookie(response)
50
  return response
51
  else:
52
- error = "Password errata. Riprova."
53
 
54
- return render_template('login.html', error=error)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  @app.route('/logout')
57
  def logout():
@@ -60,27 +157,14 @@ def logout():
60
  response.set_cookie(COOKIE_NAME, '', expires=0)
61
  return response
62
 
63
- # Aggiungi questo decoratore a tutte le route che vuoi proteggere
64
- def require_auth(f):
65
- """Decorator per proteggere le route"""
66
- def decorated_function(*args, **kwargs):
67
- if not is_authenticated():
68
- return redirect(url_for('login'))
69
- return f(*args, **kwargs)
70
- decorated_function.__name__ = f.__name__
71
- return decorated_function
72
-
73
- # Esempio di utilizzo del decorator su altre route
74
  @app.route('/protected')
75
- @require_auth
76
- def protected_route():
 
77
  return "Questa è una pagina protetta!"
78
 
79
- # Le tue route esistenti vanno modificate aggiungendo @require_auth
80
- # @app.route('/tua_route')
81
- # @require_auth
82
- # def tua_funzione():
83
- # return render_template('tuo_template.html')
84
-
85
  if __name__ == '__main__':
86
- app.run(debug=True)
 
 
 
1
  from flask import Flask, request, render_template, redirect, url_for, make_response
2
  import datetime
3
+ import os
4
 
5
  app = Flask(__name__)
6
  SECRET_PASSWORD = "VeronaTrento25!"
 
21
  "true",
22
  expires=expires,
23
  httponly=True,
24
+ secure=False
25
  )
26
  return response
27
 
 
29
  def index():
30
  # Se l'utente è già autenticato, mostra il contenuto normale
31
  if is_authenticated():
32
+ return """
33
+ <!DOCTYPE html>
34
+ <html>
35
+ <head>
36
+ <title>Sito Protetto</title>
37
+ <style>
38
+ body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
39
+ .welcome { background: #e8f5e8; padding: 20px; border-radius: 5px; margin-bottom: 20px; }
40
+ .content { background: #f0f0f0; padding: 20px; border-radius: 5px; }
41
+ a { color: #007bff; text-decoration: none; margin-right: 15px; }
42
+ </style>
43
+ </head>
44
+ <body>
45
+ <div class="welcome">
46
+ <h2>✅ Accesso Autorizzato!</h2>
47
+ <p>Sei autenticato correttamente. La navigazione è libera per 24 ore.</p>
48
+ <p><a href="/logout">🔓 Logout</a></p>
49
+ </div>
50
+
51
+ <div class="content">
52
+ <h3>Benvenuto nel sito protetto</h3>
53
+ <p>Questo è il contenuto principale del tuo sito web.</p>
54
+ <p>Qui puoi inserire tutto il contenuto che vuoi proteggere con password.</p>
55
+ </div>
56
+ </body>
57
+ </html>
58
+ """
59
 
60
  # Altrimenti reindirizza alla pagina di login
61
  return redirect(url_for('login'))
 
76
  response = set_auth_cookie(response)
77
  return response
78
  else:
79
+ error = "Password errata. Riprova."
80
 
81
+ return """
82
+ <!DOCTYPE html>
83
+ <html>
84
+ <head>
85
+ <title>Login Richiesto</title>
86
+ <style>
87
+ body {
88
+ font-family: Arial, sans-serif;
89
+ max-width: 400px;
90
+ margin: 100px auto;
91
+ padding: 20px;
92
+ background: #f5f5f5;
93
+ }
94
+ .login-form {
95
+ background: white;
96
+ padding: 30px;
97
+ border-radius: 10px;
98
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
99
+ }
100
+ h2 {
101
+ color: #333;
102
+ text-align: center;
103
+ margin-bottom: 20px;
104
+ }
105
+ input[type="password"] {
106
+ width: 100%;
107
+ padding: 12px;
108
+ margin: 15px 0;
109
+ border: 1px solid #ddd;
110
+ border-radius: 5px;
111
+ box-sizing: border-box;
112
+ font-size: 16px;
113
+ }
114
+ button {
115
+ background: #007bff;
116
+ color: white;
117
+ padding: 12px 20px;
118
+ border: none;
119
+ border-radius: 5px;
120
+ cursor: pointer;
121
+ width: 100%;
122
+ font-size: 16px;
123
+ }
124
+ button:hover {
125
+ background: #0056b3;
126
+ }
127
+ .error {
128
+ color: red;
129
+ margin-bottom: 15px;
130
+ text-align: center;
131
+ padding: 10px;
132
+ background: #ffe6e6;
133
+ border-radius: 5px;
134
+ }
135
+ </style>
136
+ </head>
137
+ <body>
138
+ <div class="login-form">
139
+ <h2>🔒 Accesso Protetto</h2>
140
+ <p style="text-align: center; color: #666;">Inserisci la password per accedere al sito</p>
141
+ {% if error %}
142
+ <div class="error">{{ error }}</div>
143
+ {% endif %}
144
+ <form method="POST">
145
+ <input type="password" name="password" placeholder="Inserisci la password" required>
146
+ <button type="submit">🔑 Accedi</button>
147
+ </form>
148
+ </div>
149
+ </body>
150
+ </html>
151
+ """.replace("{% if error %}", f"{% if error %}" if error else "").replace("{{ error }}", error or "")
152
 
153
  @app.route('/logout')
154
  def logout():
 
157
  response.set_cookie(COOKIE_NAME, '', expires=0)
158
  return response
159
 
160
+ # Proteggi tutte le route aggiuntive che potresti avere
 
 
 
 
 
 
 
 
 
 
161
  @app.route('/protected')
162
+ def protected():
163
+ if not is_authenticated():
164
+ return redirect(url_for('login'))
165
  return "Questa è una pagina protetta!"
166
 
 
 
 
 
 
 
167
  if __name__ == '__main__':
168
+ # Configurazione per Hugging Face Spaces
169
+ port = int(os.environ.get('PORT', 5000))
170
+ app.run(host='0.0.0.0', port=port, debug=False)