PRINTFILM: AI short-video marketing and AI short-drama creation platform
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from sqlalchemy.ext.asyncio import AsyncSession
from app.database import get_db
from app.models import User
from app.services.auth import decode_token, get_user_by_id
security = HTTPBearer(
auto_error=False,
bearerFormat="JWT",
description="站内登录令牌:通过 POST /api/auth/login 获取 access_token,请求时发送 Authorization: Bearer <access_token>。",
)
async def _user_from_bearer(db: AsyncSession, token: str) -> User | None:
"""站内 Bearer 登录令牌仅接受有效 JWT。"""
sub = decode_token(token)
if not sub:
return None
try:
user_id = int(sub)
except (TypeError, ValueError):
return None
return await get_user_by_id(db, user_id)
async def get_current_user(
creds: HTTPAuthorizationCredentials | None = Depends(security),
db: AsyncSession = Depends(get_db),
) -> User:
if not creds:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="未登录")
user = await _user_from_bearer(db, creds.credentials)
if not user:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="登录已失效")
return user
async def get_current_admin(user: User = Depends(get_current_user)) -> User:
# Require role=admin for /api/admin routes
if (user.role or "user") != "admin":
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="需要管理员权限")
return user