Files
zmb-webui/backend/routers/auth.py
T
patrick f49793e6f2 Refactor: Java-Klassen aus Services entfernt + kritische Bugs gefixt
- AuthService, SystemInfo, IdentitiesManager Klassen → Modul-Funktionen
- grp.getall() → grp.getgrall() (Bug: Methode existierte nie)
- open('/proc/loadavg') ohne context manager gefixt (File-Handle-Leak)
- rx_packets/tx_packets null-check im Frontend (toLocaleString auf undefined)
- PoolCard onClick: /pools/{name} → /zfs (Route existierte nicht, löste Seitenreload aus)
- Alle Router-Imports auf Modul-Aliase umgestellt

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-05 14:11:32 +02:00

54 lines
1.4 KiB
Python

"""
Authentication endpoints
"""
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from pydantic import BaseModel
from services import auth as auth_service
from models import Token
router = APIRouter(prefix="/api/auth", tags=["auth"])
security = HTTPBearer()
class LoginRequest(BaseModel):
username: str
password: str
@router.post("/login", response_model=Token)
async def login(request: LoginRequest):
"""
Login with username and password
Returns JWT access token
"""
user = auth_service.authenticate_user(request.username, request.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid credentials",
headers={"WWW-Authenticate": "Bearer"},
)
access_token = auth_service.create_access_token(request.username)
return {"access_token": access_token, "token_type": "bearer"}
@router.post("/verify")
async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)):
"""
Verify JWT token validity
"""
token = credentials.credentials
username = auth_service.verify_token(token)
if not username:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token"
)
return {"valid": True, "username": username}