flux-ide-backend / auth /jwt_handler.py
simhaa-hat's picture
Upload folder using huggingface_hub
92697d3 verified
Raw History Blame Contribute Delete
1.87 kB
from datetime import datetime, timedelta
from typing import Optional, Dict
import jwt
import os
# Secret key for JWT - in production, use environment variable
SECRET_KEY = os.getenv('JWT_SECRET_KEY', 'your-secret-key-change-in-production')
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 # 24 hours
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
"""
Create JWT access token
Args:
data: Payload data to encode
expires_delta: Optional custom expiration time
Returns:
Encoded JWT token
"""
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode.update({"exp": expire, "iat": datetime.utcnow()})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
def verify_token(token: str) -> Optional[Dict]:
"""
Verify and decode JWT token
Args:
token: JWT token to verify
Returns:
Decoded payload or None if invalid
"""
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
return payload
except jwt.ExpiredSignatureError:
return None
except jwt.InvalidTokenError:
return None
def decode_token(token: str) -> Optional[Dict]:
"""
Decode token without verification (for debugging)
Args:
token: JWT token to decode
Returns:
Decoded payload or None
"""
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM], options={"verify_signature": False})
return payload
except Exception:
return None