alabuga/backend/app/core/security.py
danilgryaznev e050bd46ef 1 vers
2025-09-21 19:30:55 +02:00

43 lines
1.5 KiB
Python

"""Утилиты безопасности: хеширование паролей и JWT."""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from typing import Any
from jose import JWTError, jwt
from passlib.context import CryptContext
from app.core.config import settings
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
def verify_password(plain_password: str, hashed_password: str) -> bool:
"""Проверяем соответствие пароля и хеша."""
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password: str) -> str:
"""Создаём безопасный хеш."""
return pwd_context.hash(password)
def create_access_token(subject: str, expires_delta: timedelta | None = None) -> str:
"""Формируем JWT для аутентификации."""
expire = datetime.now(timezone.utc) + (expires_delta or timedelta(minutes=settings.access_token_expire_minutes))
to_encode: dict[str, Any] = {"sub": subject, "exp": expire}
return jwt.encode(to_encode, settings.secret_key, algorithm=settings.jwt_algorithm)
def decode_access_token(token: str) -> dict[str, Any]:
"""Расшифровываем и валидируем JWT."""
try:
return jwt.decode(token, settings.secret_key, algorithms=[settings.jwt_algorithm])
except JWTError as exc: # pragma: no cover - FastAPI обработает ошибку
raise ValueError("Недействительный токен") from exc