from datetime import datetime, timedelta from jose import JWTError, jwt from passlib.context import CryptContext import os from dotenv import load_dotenv load_dotenv() # JWT Configuration SECRET_KEY = os.getenv("JWT_SECRET", "your-super-secret-jwt-key-change-in-production") ALGORITHM = "HS256" ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", 30)) # Password hashing context pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") class AuthService: """ Service class for authentication-related operations """ @staticmethod def create_access_token(data: dict, expires_delta: timedelta = None): """ Create a new access token """ to_encode = data.copy() if expires_delta: expire = datetime.utcnow() + expires_delta else: expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) to_encode.update({"exp": expire}) encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) return encoded_jwt @staticmethod def verify_token(token: str): """ Verify and decode a JWT token """ try: payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) user_id: int = payload.get("sub") if user_id is None: raise ValueError("Could not validate credentials") return user_id except JWTError: raise ValueError("Could not validate credentials") @staticmethod def get_password_hash(password: str) -> str: """Hash a plain text password""" return pwd_context.hash(password) @staticmethod def verify_password(plain_password: str, hashed_password: str) -> bool: """Verify a plain text password against its hash""" return pwd_context.verify(plain_password, hashed_password) @staticmethod def create_refresh_token(data: dict, expires_delta: timedelta = None): """ Create a new refresh token """ to_encode = data.copy() if expires_delta: expire = datetime.utcnow() + expires_delta else: # Refresh token expires in 7 days expire = datetime.utcnow() + timedelta(days=7) to_encode.update({"exp": expire, "type": "refresh"}) encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) return encoded_jwt