Files
Vorrania/backend/app/security.py
Scarriffle 022a6d5ea8 Backend: passlib entfernt, bcrypt direkt nutzen (Fix fuer Startup-Crash/Restart-Loop)
passlib 1.7.4 vertraegt sich schlecht mit bcrypt 4.x und kann beim Hashen des
Admin-Passworts im Startup (ensure_first_admin) crashen -> Backend-Container in
Restart-Loop -> 502 in der Web-UI. hash_password/verify_password nutzen jetzt
bcrypt direkt (mit 72-Byte-Grenze). passlib aus requirements entfernt.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-22 09:39:36 +02:00

38 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from datetime import datetime, timedelta, timezone
import bcrypt
from jose import JWTError, jwt
from .config import get_settings
settings = get_settings()
# bcrypt akzeptiert maximal 72 Bytes längere Passwörter werden abgeschnitten.
_BCRYPT_MAX_BYTES = 72
def hash_password(password: str) -> str:
pw = password.encode("utf-8")[:_BCRYPT_MAX_BYTES]
return bcrypt.hashpw(pw, bcrypt.gensalt()).decode("utf-8")
def verify_password(password: str, password_hash: str) -> bool:
try:
pw = password.encode("utf-8")[:_BCRYPT_MAX_BYTES]
return bcrypt.checkpw(pw, password_hash.encode("utf-8"))
except (ValueError, TypeError):
return False
def create_access_token(subject: str, role: str) -> str:
expire = datetime.now(timezone.utc) + timedelta(minutes=settings.jwt_expire_minutes)
payload = {"sub": subject, "role": role, "exp": expire}
return jwt.encode(payload, settings.jwt_secret, algorithm=settings.jwt_algorithm)
def decode_access_token(token: str) -> dict | None:
try:
return jwt.decode(token, settings.jwt_secret, algorithms=[settings.jwt_algorithm])
except JWTError:
return None