web-app / src /middleware /auth.py
asmaakbar's picture
update
88c34af
Raw History Blame Contribute Delete
4.37 kB
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 src.database import get_session
from 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"}