fixing DQ issues

This commit is contained in:
2025-09-19 16:54:13 +02:00
parent 0ce708cf09
commit f042616fdd
45 changed files with 3625 additions and 68 deletions

View File

@@ -1,4 +1,12 @@
# middleware/auth.py
"""Summary description ....
This module provides classes and functions for:
- ....
- ....
- ....
"""
import logging
import os
from datetime import datetime, timedelta
@@ -30,17 +38,17 @@ class AuthManager:
self.token_expire_minutes = int(os.getenv("JWT_EXPIRE_MINUTES", "30"))
def hash_password(self, password: str) -> str:
"""Hash password using bcrypt"""
"""Hash password using bcrypt."""
return pwd_context.hash(password)
def verify_password(self, plain_password: str, hashed_password: str) -> bool:
"""Verify password against hash"""
"""Verify password against hash."""
return pwd_context.verify(plain_password, hashed_password)
def authenticate_user(
self, db: Session, username: str, password: str
) -> Optional[User]:
"""Authenticate user and return user object if valid"""
"""Authenticate user and return user object if valid."""
user = (
db.query(User)
.filter((User.username == username) | (User.email == username))
@@ -64,7 +72,7 @@ class AuthManager:
return user
def create_access_token(self, user: User) -> Dict[str, Any]:
"""Create JWT access token for user"""
"""Create JWT access token for user."""
expires_delta = timedelta(minutes=self.token_expire_minutes)
expire = datetime.utcnow() + expires_delta
@@ -86,7 +94,7 @@ class AuthManager:
}
def verify_token(self, token: str) -> Dict[str, Any]:
"""Verify JWT token and return user data"""
"""Verify JWT token and return user data."""
try:
payload = jwt.decode(token, self.secret_key, algorithms=[self.algorithm])
@@ -126,7 +134,7 @@ class AuthManager:
def get_current_user(
self, db: Session, credentials: HTTPAuthorizationCredentials
) -> User:
"""Get current authenticated user from database"""
"""Get current authenticated user from database."""
user_data = self.verify_token(credentials.credentials)
user = db.query(User).filter(User.id == user_data["user_id"]).first()
@@ -139,7 +147,7 @@ class AuthManager:
return user
def require_role(self, required_role: str):
"""Decorator to require specific role"""
"""Require specific role."""
def decorator(func):
def wrapper(current_user: User, *args, **kwargs):
@@ -155,13 +163,13 @@ class AuthManager:
return decorator
def require_admin(self, current_user: User):
"""Require admin role"""
"""Require admin role."""
if current_user.role != "admin":
raise HTTPException(status_code=403, detail="Admin privileges required")
return current_user
def create_default_admin_user(self, db: Session):
"""Create default admin user if it doesn't exist"""
"""Create default admin user if it doesn't exist."""
admin_user = db.query(User).filter(User.username == "admin").first()
if not admin_user: