File size: 4,385 Bytes
f40c733 | 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 | from datetime import datetime, timedelta
from typing import Optional
import os
from fastapi import HTTPException, status, Depends, Request
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from sqlmodel import Session
from jose import JWTError, jwt
from backend.src.database import get_session
from backend.src.models.user import User
# Initialize security scheme
security = HTTPBearer()
# Get secret key from environment - check both BETTER_AUTH_SECRET and SECRET_KEY
SECRET_KEY = os.getenv("BETTER_AUTH_SECRET") or os.getenv("SECRET_KEY", "fallback_secret_key_for_development")
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
"""Create a new JWT access token"""
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=15)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
def verify_token(token: str, credentials_exception):
"""Verify JWT token and extract user information"""
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
user_id: str = payload.get("sub")
if user_id is None:
raise credentials_exception
return user_id
except JWTError:
raise credentials_exception
def get_current_user(
credentials: HTTPAuthorizationCredentials = Depends(security),
session: Session = Depends(get_session)
):
"""Dependency to get current user from JWT token"""
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
user_id = verify_token(credentials.credentials, credentials_exception)
user = session.get(User, user_id)
if user is None:
raise credentials_exception
return user
class JWTBearer(HTTPBearer):
"""Custom JWT Bearer authentication class with enhanced functionality"""
def __init__(self, auto_error: bool = True):
super(JWTBearer, self).__init__(auto_error=auto_error)
async def __call__(self, request: Request):
credentials: Optional[HTTPAuthorizationCredentials] = await super(JWTBearer, self).__call__(request)
if credentials:
if not credentials.scheme == "Bearer":
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Invalid authentication scheme."
)
token = credentials.credentials
user_id = self.verify_jwt(token)
if not user_id:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Invalid token or expired token."
)
# Attach user info to request
request.state.user_id = user_id
return token
else:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Invalid authorization code."
)
def verify_jwt(self, jwtoken: str) -> Optional[str]:
"""
Verify JWT token and return user ID if valid
Args:
jwtoken: JWT token to verify
Returns:
str: User ID if token is valid, None otherwise
"""
try:
payload = jwt.decode(jwtoken, SECRET_KEY, algorithms=[ALGORITHM])
user_id = payload.get("sub")
return user_id
except jwt.ExpiredSignatureError:
return None
except jwt.InvalidTokenError:
return None
def verify_token_detailed(token: str) -> Optional[dict]:
"""
Verify JWT token and return detailed payload information
Args:
token: JWT token to verify
Returns:
dict: Payload information if token is valid, None otherwise
"""
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
return payload
except jwt.ExpiredSignatureError:
return {"error": "EXPIRED_TOKEN", "message": "Token has expired"}
except jwt.InvalidTokenError:
return {"error": "INVALID_TOKEN", "message": "Invalid token format"} |