Spaces:
Running
Running
| import os | |
| from datetime import datetime, timedelta, timezone | |
| import jwt | |
| from fastapi import Depends, HTTPException, status | |
| from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer | |
| from passlib.context import CryptContext | |
| from dotenv import load_dotenv | |
| from pathlib import Path | |
| from database import get_user_by_email, get_user_by_id | |
| load_dotenv(dotenv_path=Path(__file__).resolve().parent / ".env") | |
| SECRET_KEY = os.getenv("SECRET_KEY", "change-me-in-production-use-env") | |
| ALGORITHM = "HS256" | |
| ACCESS_TOKEN_EXPIRE_MINUTES = 15 | |
| pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") | |
| security = HTTPBearer(auto_error=False) | |
| def hash_password(password: str) -> str: | |
| return pwd_context.hash(password) | |
| def verify_password(plain: str, hashed: str) -> bool: | |
| return pwd_context.verify(plain, hashed) | |
| def create_access_token(data: dict) -> str: | |
| to_encode = data.copy() | |
| expire = datetime.now(timezone.utc) + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) | |
| to_encode["exp"] = expire | |
| return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) | |
| def decode_token(token: str) -> dict | None: | |
| try: | |
| return jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) | |
| except jwt.PyJWTError: | |
| return None | |
| async def get_current_user( | |
| credentials: HTTPAuthorizationCredentials | None = Depends(security), | |
| ) -> dict: | |
| if credentials is None: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Not authenticated", | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| payload = decode_token(credentials.credentials) | |
| if payload is None: | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid or expired token", | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| user_id = payload.get("sub") | |
| if not user_id: | |
| raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token") | |
| user = get_user_by_id(int(user_id)) | |
| if user is None: | |
| raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User not found") | |
| return user | |
| async def get_optional_user( | |
| credentials: HTTPAuthorizationCredentials | None = Depends(security), | |
| ) -> dict | None: | |
| if credentials is None: | |
| return None | |
| payload = decode_token(credentials.credentials) | |
| if payload is None: | |
| return None | |
| user_id = payload.get("sub") | |
| if not user_id: | |
| return None | |
| return get_user_by_id(int(user_id)) | |