Spaces:
Paused
Paused
File size: 4,262 Bytes
f3bcb28 | 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | """
JWT token handling for user authentication.
Provides functions to:
- Decode JWT tokens
- Extract user information
- Validate token signatures
"""
import logging
from typing import Optional
from fastapi import Header, HTTPException, status
from jose import JWTError, jwt
from datetime import datetime
from backend.core.config import settings
logger = logging.getLogger(__name__)
# JWT Configuration
ALGORITHM = "HS256"
SECRET_KEY = getattr(settings, "jwt_secret_key", "dev-secret-key-change-in-production")
class TokenPayload:
"""JWT Token payload"""
def __init__(self, user_id: int, email: str, exp: Optional[datetime] = None):
self.user_id = user_id
self.email = email
self.exp = exp
def decode_jwt_token(token: str) -> Optional[TokenPayload]:
"""
Decode a JWT token and extract user information.
Args:
token: JWT token string
Returns:
TokenPayload if valid, None otherwise
"""
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
user_id: int = payload.get("sub")
email: str = payload.get("email")
if user_id is None:
return None
return TokenPayload(user_id=int(user_id), email=email)
except JWTError as e:
logger.warning(f"JWT decode error: {e}")
return None
except Exception as e:
logger.error(f"Unexpected error decoding JWT: {e}")
return None
async def get_current_user_id(
authorization: Optional[str] = Header(None, alias="Authorization"),
) -> int:
"""
Extract user_id from JWT token in Authorization header.
Falls back to user_id=1 for development if no token provided.
Args:
authorization: Authorization header with Bearer token
Returns:
user_id extracted from token
Raises:
HTTPException: If token is invalid or missing in production
"""
# Development fallback
if not authorization:
logger.warning(
"No Authorization header, using default user_id=1 for development"
)
return 1
# Extract token from "Bearer <token>"
if not authorization.startswith("Bearer "):
logger.warning("Invalid Authorization header format, using default user_id=1")
return 1
token = authorization.replace("Bearer ", "")
# Decode token
payload = decode_jwt_token(token)
if not payload:
logger.warning("Invalid JWT token, using default user_id=1 for development")
return 1
logger.debug(f"Extracted user_id={payload.user_id} from JWT token")
return payload.user_id
async def require_user_id(
authorization: Optional[str] = Header(None, alias="Authorization"),
) -> int:
"""
Extract user_id from JWT token - strict mode (no fallback).
Args:
authorization: Authorization header with Bearer token
Returns:
user_id extracted from token
Raises:
HTTPException: If token is invalid or missing
"""
if not authorization:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Missing Authorization header",
)
if not authorization.startswith("Bearer "):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid Authorization header format",
)
token = authorization.replace("Bearer ", "")
payload = decode_jwt_token(token)
if not payload:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid or expired token"
)
return payload.user_id
# Optional: For compatibility with existing code
async def get_current_user_id_optional(
authorization: Optional[str] = Header(None, alias="Authorization"),
) -> Optional[int]:
"""
Extract user_id from JWT token - returns None if not found.
Args:
authorization: Authorization header with Bearer token
Returns:
user_id if token is valid, None otherwise
"""
if not authorization or not authorization.startswith("Bearer "):
return None
token = authorization.replace("Bearer ", "")
payload = decode_jwt_token(token)
return payload.user_id if payload else None
|