from datetime import datetime, timedelta
|
from typing import Annotated
|
|
from fastapi import Depends, HTTPException, status
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
from jose import JWTError, jwt
|
from passlib.context import CryptContext
|
from sqlalchemy.orm import Session
|
|
from app.config import settings
|
from app.database import get_db
|
from app.models import User
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
bearer = HTTPBearer(auto_error=False)
|
|
|
def hash_password(password: str) -> str:
|
return pwd_context.hash(password)
|
|
|
def verify_password(plain: str, hashed: str) -> bool:
|
return pwd_context.verify(plain, hashed)
|
|
|
def create_access_token(user_id: int, role: str) -> str:
|
expire = datetime.utcnow() + timedelta(hours=settings.jwt_expire_hours)
|
payload = {"sub": str(user_id), "role": role, "exp": expire}
|
return jwt.encode(payload, settings.jwt_secret, algorithm=settings.jwt_algorithm)
|
|
|
def get_current_user(
|
creds: Annotated[HTTPAuthorizationCredentials | None, Depends(bearer)],
|
db: Annotated[Session, Depends(get_db)],
|
) -> User:
|
if not creds or not creds.credentials:
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="未登录")
|
try:
|
payload = jwt.decode(
|
creds.credentials, settings.jwt_secret, algorithms=[settings.jwt_algorithm]
|
)
|
user_id = int(payload.get("sub", 0))
|
except (JWTError, ValueError):
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="无效令牌")
|
user = db.get(User, user_id)
|
if not user or not user.is_active:
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="用户不可用")
|
return user
|
|
|
def require_admin(user: Annotated[User, Depends(get_current_user)]) -> User:
|
if user.role != "admin":
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="需要管理员权限")
|
return user
|