zombee11/scorecardapi
1
1from fastapi import Depends, HTTPException, status2from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer3from jose import JWTError, jwt4 5from app.core.config import get_settings6 7 8bearer = HTTPBearer(auto_error=False)9 10 11def require_auth(credentials: HTTPAuthorizationCredentials | None = Depends(bearer)) -> dict:12 settings = get_settings()13 if not settings.enable_auth:14 return {"sub": "local-dev"}15 if not credentials:16 raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Missing bearer token")17 try:18 return jwt.decode(19 credentials.credentials,20 settings.jwt_secret_key,21 algorithms=[settings.jwt_algorithm],22 )23 except JWTError as exc:24 raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token") from exc25 26 