fix(core): clean predictor debt

This commit is contained in:
dasemu committed 2026-03-13 10:51:29 +01:00
1 parent e952cea417
commit e6add1b727
10 files changed
+268 -58

No files matched your search

+20 -4
View File
@@ -6,10 +6,10 @@ import logging
import os
from datetime import datetime, timedelta, timezone
import bcrypt
import jwt
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from passlib.context import CryptContext
from app.api.schemas import TokenResponse, UserCreate, UserResponse
from storage import SQLiteStore
@@ -19,7 +19,6 @@ logger = logging.getLogger(__name__)
_TOKEN_EXPIRE_DAYS = 14
_ALGORITHM = "HS256"
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/auth/login")
auth_router = APIRouter(prefix="/api/v1/auth", tags=["auth"])
@@ -43,6 +42,23 @@ def _create_token(user_id: int, email: str) -> str:
return jwt.encode(payload, _get_secret_key(), algorithm=_ALGORITHM)
def _hash_password(password: str) -> str:
"""Return a bcrypt password hash."""
return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
def _verify_password(password: str, password_hash: str) -> bool:
"""Validate a plaintext password against a stored bcrypt hash."""
try:
return bcrypt.checkpw(
password.encode("utf-8"),
password_hash.encode("utf-8"),
)
except ValueError:
logger.warning("Encountered an invalid stored password hash.")
return False
def get_current_user(token: str = Depends(oauth2_scheme)) -> dict:
"""FastAPI dependency that validates the JWT and returns user info."""
try:
@@ -82,7 +98,7 @@ def get_current_user(token: str = Depends(oauth2_scheme)) -> dict:
def register(body: UserCreate) -> UserResponse:
"""Create a new user account."""
store = SQLiteStore()
hashed = pwd_context.hash(body.password)
hashed = _hash_password(body.password)
now_iso = datetime.now(timezone.utc).isoformat()
with store.connect() as conn:
@@ -117,7 +133,7 @@ def login(form: OAuth2PasswordRequestForm = Depends()) -> TokenResponse:
(form.username,),
).fetchone()
if row is None or not pwd_context.verify(form.password, row["password_hash"]):
if row is None or not _verify_password(form.password, row["password_hash"]):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="invalid credentials",