optymalizacje_kodu #8

Merged
gru merged 23 commits from optymalizacje_kodu into master 2025-07-31 10:55:39 +02:00
Showing only changes of commit 437f7a26e3 - Show all commits

43
app.py
View File

@@ -89,7 +89,9 @@ referrer_policy = app.config.get("REFERRER_POLICY")
if referrer_policy:
talisman_kwargs["referrer_policy"] = referrer_policy
talisman = Talisman(app, **talisman_kwargs)
talisman = Talisman(app,
session_cookie_secure=app.config["SESSION_COOKIE_SECURE"],
**talisman_kwargs)
register_heif_opener() # pillow_heif dla HEIC
SQLALCHEMY_ECHO = True
@@ -270,6 +272,23 @@ def check_password(stored_hash, password_input):
return False
return False
def set_authorized_cookie(response):
secure_flag = app.config["SESSION_COOKIE_SECURE"] # wartość z config.py
max_age = app.config.get("AUTH_COOKIE_MAX_AGE", 86400)
print("ENV SESSION_COOKIE_SECURE =", os.environ.get("SESSION_COOKIE_SECURE"))
print("CONFIG SESSION_COOKIE_SECURE =", app.config["SESSION_COOKIE_SECURE"])
response.set_cookie(
"authorized",
AUTHORIZED_COOKIE_VALUE,
max_age=max_age,
secure=secure_flag,
httponly=True,
samesite="Lax",
path="/"
)
return response
if app.config["SQLALCHEMY_DATABASE_URI"].startswith("sqlite:///"):
db_path = app.config["SQLALCHEMY_DATABASE_URI"].replace("sqlite:///", "", 1)
@@ -1111,34 +1130,22 @@ def system_auth():
next_page = request.args.get("next") or url_for("main_page")
if is_ip_blocked(ip):
flash(
"Przekroczono limit prób logowania. Dostęp zablokowany na 1 godzinę.",
"danger",
)
flash("Przekroczono limit prób logowania. Dostęp zablokowany na 1 godzinę.", "danger")
return render_template("system_auth.html"), 403
if request.method == "POST":
if request.form["password"] == SYSTEM_PASSWORD:
reset_failed_attempts(ip)
resp = redirect(next_page)
max_age = app.config.get("AUTH_COOKIE_MAX_AGE", 86400)
resp.set_cookie(
"authorized",
AUTHORIZED_COOKIE_VALUE,
max_age=max_age,
secure=app.config["SESSION_COOKIE_SECURE"],
)
return resp
return set_authorized_cookie(resp)
else:
register_failed_attempt(ip)
if is_ip_blocked(ip):
flash(
"Przekroczono limit prób logowania. Dostęp zablokowany na 1 godzinę.",
"danger",
)
flash("Przekroczono limit prób logowania. Dostęp zablokowany na 1 godzinę.", "danger")
return render_template("system_auth.html"), 403
remaining = attempts_remaining(ip)
flash(f"Nieprawidłowe hasło. Pozostało {remaining} prób.", "warning")
return render_template("system_auth.html")
@@ -1295,7 +1302,7 @@ def login():
if user and check_password(user.password_hash, request.form["password"]):
session.permanent = True
login_user(user)
# session["logged"] = True
session.modified = True
flash("Zalogowano pomyślnie", "success")
return redirect(url_for("main_page"))
flash("Nieprawidłowy login lub hasło", "danger")