"feat: Integrate new API for user authentication in backend"

This commit is contained in:
2025-05-26 07:16:08 +02:00
parent ebaef3a9b5
commit 3b53e78799
2 changed files with 41 additions and 24 deletions

View File

@@ -18,7 +18,7 @@ from flask_wtf.csrf import CSRFProtect
from config.settings import ( from config.settings import (
SECRET_KEY, TAPO_USERNAME, TAPO_PASSWORD, PRINTERS, SECRET_KEY, TAPO_USERNAME, TAPO_PASSWORD, PRINTERS,
FLASK_HOST, FLASK_PORT, FLASK_DEBUG, SESSION_LIFETIME, FLASK_HOST, FLASK_PORT, FLASK_DEBUG, SESSION_LIFETIME,
SCHEDULER_INTERVAL, SCHEDULER_ENABLED, get_ssl_context SCHEDULER_INTERVAL, SCHEDULER_ENABLED, get_ssl_context, FLASK_FALLBACK_PORT
) )
from utils.logging_config import setup_logging, get_logger, log_startup_info from utils.logging_config import setup_logging, get_logger, log_startup_info
from models import User, Printer, Job, Stats, get_db_session, init_database, create_initial_admin from models import User, Printer, Job, Stats, get_db_session, init_database, create_initial_admin
@@ -1324,26 +1324,41 @@ def init_app():
# App starten # App starten
if __name__ == "__main__": if __name__ == "__main__":
try: init_app()
# App initialisieren ssl_context = get_ssl_context()
init_app()
# SSL-Kontext ermitteln # Starte den Haupt-Server mit SSL auf Port 443
ssl_context = get_ssl_context() if ssl_context:
print(f"Starte Flask-Server mit SSL auf {FLASK_HOST}:{FLASK_PORT}")
app.run(host=FLASK_HOST, port=FLASK_PORT, debug=FLASK_DEBUG, ssl_context=ssl_context)
else:
print(f"Starte Flask-Server ohne SSL auf {FLASK_HOST}:{FLASK_PORT}")
app.run(host=FLASK_HOST, port=FLASK_PORT, debug=FLASK_DEBUG)
# Konsolen-Ausgabe für HTTPS # Starte einen zweiten Server auf Port 80 als Fallback
protocol = "HTTPS" if ssl_context else "HTTP" # Dies wird in einem separaten Thread ausgeführt
app_logger.info(f"MYP startet auf {protocol}://{FLASK_HOST}:{FLASK_PORT} (Debug: {FLASK_DEBUG})") def run_fallback_server():
try:
from werkzeug.serving import make_server
fallback_app = Flask("fallback_app")
# App starten @fallback_app.route('/', defaults={'path': ''})
app.run( @fallback_app.route('/<path:path>')
host=FLASK_HOST, def catch_all(path):
port=FLASK_PORT, # Leite alle Anfragen an HTTPS weiter
debug=FLASK_DEBUG, host = request.host.split(':')[0]
ssl_context=ssl_context return redirect(f"https://{host}:{FLASK_PORT}/{path}")
)
except Exception as e: server = make_server(FLASK_HOST, FLASK_FALLBACK_PORT, fallback_app)
app_logger.critical(f"Kritischer Fehler beim Starten der Anwendung: {str(e)}") print(f"Starte Fallback-Server auf {FLASK_HOST}:{FLASK_FALLBACK_PORT}")
server.serve_forever()
except Exception as e:
print(f"Fehler beim Starten des Fallback-Servers: {str(e)}")
# Starte den Fallback-Server in einem separaten Thread
fallback_thread = threading.Thread(target=run_fallback_server)
fallback_thread.daemon = True
fallback_thread.start()
# Content Security Policy anpassen # Content Security Policy anpassen
@app.after_request @app.after_request

View File

@@ -27,7 +27,8 @@ LOG_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
# Flask-Konfiguration # Flask-Konfiguration
FLASK_HOST = "0.0.0.0" FLASK_HOST = "0.0.0.0"
FLASK_PORT = 5000 FLASK_PORT = 443
FLASK_FALLBACK_PORT = 80
FLASK_DEBUG = True FLASK_DEBUG = True
SESSION_LIFETIME = timedelta(days=7) SESSION_LIFETIME = timedelta(days=7)
@@ -35,6 +36,7 @@ SESSION_LIFETIME = timedelta(days=7)
SSL_ENABLED = True SSL_ENABLED = True
SSL_CERT_PATH = "/opt/myp/ssl/myp.crt" SSL_CERT_PATH = "/opt/myp/ssl/myp.crt"
SSL_KEY_PATH = "/opt/myp/ssl/myp.key" SSL_KEY_PATH = "/opt/myp/ssl/myp.key"
SSL_HOSTNAME = "raaspberry"
# Scheduler-Konfiguration # Scheduler-Konfiguration
SCHEDULER_INTERVAL = 60 # Sekunden SCHEDULER_INTERVAL = 60 # Sekunden
@@ -102,8 +104,8 @@ def get_ssl_context():
if os.path.exists(script_path): if os.path.exists(script_path):
os.system(f"chmod +x {script_path}") os.system(f"chmod +x {script_path}")
# Zertifikate erstellen # Zertifikate erstellen mit spezifischem Hostnamen
os.system(f"{script_path} -c {SSL_CERT_PATH} -k {SSL_KEY_PATH}") os.system(f"{script_path} -c {SSL_CERT_PATH} -k {SSL_KEY_PATH} -h {SSL_HOSTNAME}")
else: else:
print(f"WARNUNG: SSL-Zertifikat-Generator nicht gefunden: {script_path}") print(f"WARNUNG: SSL-Zertifikat-Generator nicht gefunden: {script_path}")
return None return None