test ferst
This commit is contained in:
17
api/route/deps.py
Normal file
17
api/route/deps.py
Normal file
@@ -0,0 +1,17 @@
|
||||
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")
|
||||
Reference in New Issue
Block a user