File size: 5,797 Bytes
8ae78b0 | 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | from datetime import datetime, timedelta
from typing import Optional
from jose import JWTError, jwt
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, APIKeyHeader
from sqlalchemy.orm import Session
import os
from app.core.config import settings
from app.db.base import get_db
from app.db.models import User
from app.models.token import TokenData
# OAuth2 scheme for token authentication
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{settings.API_V1_STR}/auth/login")
# API Key security scheme
API_KEY_NAME = "X-API-Key"
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
# Use API key from settings
async def get_api_key_user(
api_key: str = Depends(api_key_header),
) -> bool:
"""
Validate the API key from the request header.
Args:
api_key: The API key from the request header
Returns:
bool: True if the API key is valid
Raises:
HTTPException: If the API key is invalid
"""
if not api_key:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="API key required",
headers={"WWW-Authenticate": "ApiKey"},
)
if api_key != settings.API_KEY:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid API key",
headers={"WWW-Authenticate": "ApiKey"},
)
return True
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
"""
Create a new JWT access token.
Args:
data: The data to encode in the token
expires_delta: Optional expiration time delta
Returns:
str: The encoded JWT token
"""
to_encode = data.copy()
# Set expiration time
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
# Use configured expiration time from settings
expire = datetime.utcnow() + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode.update({"exp": expire})
# Create the JWT token
encoded_jwt = jwt.encode(
to_encode,
settings.SECRET_KEY,
algorithm=settings.ALGORITHM
)
return encoded_jwt
async def get_current_user(
token: str = Depends(oauth2_scheme),
db: Session = Depends(get_db)
) -> User:
"""
Get the current authenticated user from the token.
Args:
token: The JWT token
db: Database session
Returns:
User: The authenticated user
Raises:
HTTPException: If authentication fails
"""
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
# Decode the JWT token
payload = jwt.decode(
token,
settings.SECRET_KEY,
algorithms=[settings.ALGORITHM]
)
# Extract user_id from token
user_id: str = payload.get("sub")
if user_id is None:
raise credentials_exception
token_data = TokenData(user_id=user_id)
except JWTError as e:
# Log the specific JWT error for debugging
print(f"JWT validation error: {str(e)}")
# If it's a signature verification failure, return a specific error
if "signature" in str(e).lower() or "invalid" in str(e).lower():
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token signature",
headers={"WWW-Authenticate": "Bearer"},
)
raise credentials_exception
# Get the user from the database
user = db.query(User).filter(User.id == token_data.user_id).first()
if user is None:
print(f"User not found in database: {token_data.user_id}")
raise credentials_exception
# Check if token is expired
try:
exp = payload.get("exp")
if exp is None:
print(f"Token has no expiration: {user.id}")
raise credentials_exception
expiry_time = datetime.fromtimestamp(exp)
current_time = datetime.utcnow()
# Add detailed logging for token expiration
time_until_expiry = expiry_time - current_time
print(f"Token expiration check: current={current_time}, expiry={expiry_time}, seconds_remaining={time_until_expiry.total_seconds()}")
if expiry_time < current_time:
print(f"Token expired for user: {user.id}, expired at {expiry_time}")
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Token expired",
headers={"WWW-Authenticate": "Bearer"},
)
except Exception as e:
print(f"Error checking token expiration: {str(e)}")
raise credentials_exception
# Check if user is active
if not user.is_active:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Inactive user"
)
return user
async def get_current_active_user(
current_user: User = Depends(get_current_user)
) -> User:
"""
Get the current active user.
Args:
current_user: The current authenticated user
Returns:
User: The current active user
Raises:
HTTPException: If the user is inactive
"""
if not current_user.is_active:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Inactive user"
)
return current_user |