25 lines
635 B
Python
25 lines
635 B
Python
|
|
from fastapi import Depends, HTTPException, status
|
||
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||
|
|
import jwt
|
||
|
|
|
||
|
|
from .config import settings
|
||
|
|
|
||
|
|
security = HTTPBearer()
|
||
|
|
|
||
|
|
|
||
|
|
def verify_token(
|
||
|
|
credentials: HTTPAuthorizationCredentials = Depends(security),
|
||
|
|
) -> dict:
|
||
|
|
try:
|
||
|
|
payload = jwt.decode(
|
||
|
|
credentials.credentials,
|
||
|
|
settings.jwt_secret,
|
||
|
|
algorithms=["HS256"],
|
||
|
|
)
|
||
|
|
return payload
|
||
|
|
except jwt.InvalidTokenError:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||
|
|
detail="Invalid or expired token",
|
||
|
|
)
|