from fastapi import Depends, HTTPException, status from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials import jwt from models import User SECRET_KEY = "your_secret_key_here" ALGORITHM = "HS256" http_bearer = HTTPBearer() def verify_token(credentials: HTTPAuthorizationCredentials = Depends(http_bearer)) -> dict: token = credentials.credentials try: payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) # Optionally, check for a custom claim, e.g., 'iss' or 'backend': # if payload.get('backend') != 'my_backend': # raise HTTPException(status_code=401, detail="Invalid token origin") return payload except jwt.PyJWTError: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}, )