18 lines
615 B
Python
18 lines
615 B
Python
import os
|
|
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
from jose import jwt, JWTError
|
|
|
|
JWT_SECRET = os.getenv("JWT_SECRET", "changeme")
|
|
JWT_ALGO = "HS256"
|
|
|
|
bearer = HTTPBearer()
|
|
|
|
|
|
async def require_admin(credentials: HTTPAuthorizationCredentials = Depends(bearer)) -> str:
|
|
try:
|
|
payload = jwt.decode(credentials.credentials, JWT_SECRET, algorithms=[JWT_ALGO])
|
|
return payload["sub"]
|
|
except (JWTError, KeyError):
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or expired token")
|