- Added DownloadTokenManager for creating and verifying JWT download tokens. - Introduced new download routes for creating download tokens and downloading files using tokens. - Restructured file upload routes into a dedicated sub-router. - Updated file upload session management with improved error handling and response structures. - Created a new MCP (Microservice Communication Protocol) router with basic request and response models. - Added base models for MCP requests and responses, including method enumeration.
53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
from typing import Annotated
|
|
|
|
from fastapi import Depends, HTTPException
|
|
from jwt import InvalidTokenError
|
|
import jwt
|
|
|
|
from models.user import User
|
|
from utils.JWT import JWT
|
|
from .dependencies import SessionDep
|
|
|
|
credentials_exception = HTTPException(
|
|
status_code=401,
|
|
detail="Could not validate credentials",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
async def AuthRequired(
|
|
session: SessionDep,
|
|
token: Annotated[str, Depends(JWT.oauth2_scheme)],
|
|
) -> User:
|
|
"""
|
|
AuthRequired 需要登录
|
|
"""
|
|
try:
|
|
payload = jwt.decode(token, JWT.SECRET_KEY, algorithms=["HS256"])
|
|
username = payload.get("sub")
|
|
|
|
if username is None:
|
|
raise credentials_exception
|
|
|
|
# 从数据库获取用户信息
|
|
user = await User.get(session, User.username == username)
|
|
if not user:
|
|
raise credentials_exception
|
|
|
|
return user
|
|
|
|
except InvalidTokenError:
|
|
raise credentials_exception
|
|
|
|
async def AdminRequired(
|
|
user: Annotated[User, Depends(AuthRequired)],
|
|
) -> User:
|
|
"""
|
|
验证是否为管理员。
|
|
|
|
使用方法:
|
|
>>> APIRouter(dependencies=[Depends(AdminRequired)])
|
|
"""
|
|
group = await user.awaitable_attrs.group
|
|
if group.admin:
|
|
return user
|
|
raise HTTPException(status_code=403, detail="Admin Required") |