Spaces:
Sleeping
Sleeping
| import os | |
| import secrets | |
| from datetime import datetime, timedelta | |
| from typing import Optional | |
| from jose import JWTError, jwt | |
| from passlib.context import CryptContext | |
| from fastapi import Depends, HTTPException, status | |
| from fastapi.security import OAuth2PasswordBearer | |
| from sqlalchemy.orm import Session | |
| from database import get_db | |
| SECRET_KEY_FILE = os.path.join(os.path.dirname(__file__), ".secret_key") | |
| ALGORITHM = "HS256" | |
| ACCESS_TOKEN_EXPIRE_DAYS = 7 | |
| def _get_secret_key() -> str: | |
| if os.path.exists(SECRET_KEY_FILE): | |
| with open(SECRET_KEY_FILE) as f: | |
| key = f.read().strip() | |
| if key: | |
| return key | |
| key = secrets.token_hex(32) | |
| with open(SECRET_KEY_FILE, "w") as f: | |
| f.write(key) | |
| return key | |
| SECRET_KEY = _get_secret_key() | |
| pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") | |
| oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/token") | |
| 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(user_id: int, username: str) -> str: | |
| expire = datetime.utcnow() + timedelta(days=ACCESS_TOKEN_EXPIRE_DAYS) | |
| payload = {"sub": str(user_id), "username": username, "exp": expire} | |
| return jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM) | |
| def decode_token(token: str) -> Optional[dict]: | |
| try: | |
| return jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) | |
| except JWTError: | |
| return None | |
| def get_current_user( | |
| token: str = Depends(oauth2_scheme), | |
| db: Session = Depends(get_db), | |
| ): | |
| # Import here to avoid circular imports | |
| from models import User | |
| credentials_exc = HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Invalid or expired session. Please log in again.", | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| payload = decode_token(token) | |
| if not payload: | |
| raise credentials_exc | |
| user_id = payload.get("sub") | |
| if not user_id: | |
| raise credentials_exc | |
| user = db.query(User).filter(User.id == int(user_id)).first() | |
| if not user or not user.is_active: | |
| raise credentials_exc | |
| return user | |
| def get_current_admin(current_user=Depends(get_current_user)): | |
| if not current_user.is_admin: | |
| raise HTTPException( | |
| status_code=status.HTTP_403_FORBIDDEN, | |
| detail="Admin access required.", | |
| ) | |
| return current_user | |