Spaces:
Running
Running
File size: 1,093 Bytes
515a3fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from datetime import datetime, timedelta
import jwt
from fastapi import HTTPException, Depends
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from .config import settings
security_scheme = HTTPBearer()
def create_jwt_token(user_id: str, email: str) -> str:
"""Creates a signed JWT with 7-day expiration."""
expiration = datetime.utcnow() + timedelta(days=7)
payload = {
"sub": user_id,
"email": email,
"exp": expiration
}
return jwt.encode(payload, settings.JWT_SECRET, algorithm=settings.ALGORITHM)
def verify_jwt(credentials: HTTPAuthorizationCredentials = Depends(security_scheme)):
"""Dependency to verify JWT header."""
token = credentials.credentials
try:
payload = jwt.decode(token, settings.JWT_SECRET, algorithms=[settings.ALGORITHM])
return payload["sub"] # Returns user_id
except jwt.ExpiredSignatureError:
raise HTTPException(status_code=401, detail="Token expired")
except jwt.InvalidTokenError:
raise HTTPException(status_code=401, detail="Invalid token")
|