File size: 1,126 Bytes
ce673e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import Header, HTTPException, Depends
from .jwt_utils import verify_jwt_token
from config.database import db
from models.user import UserRole


async def get_current_user(authorization: str = Header(None)) -> dict:
    """Get the current authenticated user from JWT token."""
    if not authorization or not authorization.startswith('Bearer '):
        raise HTTPException(status_code=401, detail="Missing or invalid authorization header")
    token = authorization.replace('Bearer ', '')
    payload = verify_jwt_token(token)
    user = await db.users.find_one({"id": payload['user_id']})
    if not user:
        raise HTTPException(status_code=401, detail="User not found")
    
    # Convert _id to string for consistent handling
    if "_id" in user:
        user["_id"] = str(user["_id"])
        
    return user


async def require_maintainer(user: dict = Depends(get_current_user)) -> dict:
    """Require that the current user has maintainer role."""
    if user.get('role') != UserRole.MAINTAINER.value:
        raise HTTPException(status_code=403, detail="Maintainer access required")
    return user