2026-03-23 09:27:01 +08:00
|
|
|
|
import hashlib
|
2025-09-01 22:37:03 +08:00
|
|
|
|
import re
|
|
|
|
|
|
|
2026-03-23 09:27:01 +08:00
|
|
|
|
from fastapi import Depends, Header, HTTPException, status
|
2025-05-02 23:56:59 +08:00
|
|
|
|
from fastapi.security import OAuth2PasswordBearer
|
2025-09-02 01:08:42 +08:00
|
|
|
|
from jose import JWTError
|
2025-12-04 10:11:28 +08:00
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
2026-03-23 09:27:01 +08:00
|
|
|
|
from sqlalchemy import select
|
2025-05-02 23:56:59 +08:00
|
|
|
|
|
2026-03-17 10:16:44 +08:00
|
|
|
|
from yuxi.storage.postgres.manager import pg_manager
|
2026-03-23 09:27:01 +08:00
|
|
|
|
from yuxi.storage.postgres.models_business import User, APIKey
|
2025-05-02 23:56:59 +08:00
|
|
|
|
from server.utils.auth_utils import AuthUtils
|
2026-03-23 09:27:01 +08:00
|
|
|
|
from yuxi.utils.datetime_utils import utc_now_naive
|
2025-05-02 23:56:59 +08:00
|
|
|
|
|
|
|
|
|
|
# 定义OAuth2密码承载器,指定token URL
|
|
|
|
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/auth/token", auto_error=False)
|
|
|
|
|
|
|
|
|
|
|
|
# 公开路径列表,无需登录即可访问
|
|
|
|
|
|
PUBLIC_PATHS = [
|
2025-09-01 22:37:03 +08:00
|
|
|
|
r"^/api/auth/token$", # 登录
|
2025-05-05 16:31:35 +08:00
|
|
|
|
r"^/api/auth/check-first-run$", # 检查是否首次运行
|
2025-09-01 22:37:03 +08:00
|
|
|
|
r"^/api/auth/initialize$", # 初始化系统
|
|
|
|
|
|
r"^/api$", # Health Check
|
|
|
|
|
|
r"^/api/system/health$", # Health Check
|
|
|
|
|
|
r"^/api/system/info$", # 获取系统信息配置
|
2025-05-02 23:56:59 +08:00
|
|
|
|
]
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-12-04 10:11:28 +08:00
|
|
|
|
# 获取数据库会话(异步版本)
|
|
|
|
|
|
async def get_db():
|
2026-01-21 19:15:52 +08:00
|
|
|
|
async with pg_manager.get_async_session_context() as db:
|
2025-05-02 23:56:59 +08:00
|
|
|
|
yield db
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2026-03-23 09:27:01 +08:00
|
|
|
|
async def _verify_api_key(key: str, db: AsyncSession) -> tuple[User | None, APIKey | None]:
|
|
|
|
|
|
"""验证 API Key 并返回关联用户和 APIKey 对象"""
|
|
|
|
|
|
key_hash = hashlib.sha256(key.encode()).hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
result = await db.execute(select(APIKey).filter(APIKey.key_hash == key_hash))
|
|
|
|
|
|
api_key = result.scalar_one_or_none()
|
|
|
|
|
|
|
|
|
|
|
|
if api_key is None:
|
|
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
if not api_key.is_enabled:
|
|
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
if api_key.expires_at and utc_now_naive() > api_key.expires_at:
|
|
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
if api_key.user_id:
|
|
|
|
|
|
result = await db.execute(select(User).filter(User.id == api_key.user_id))
|
|
|
|
|
|
user = result.scalar_one_or_none()
|
|
|
|
|
|
if user and not user.is_deleted:
|
|
|
|
|
|
return user, api_key
|
|
|
|
|
|
|
|
|
|
|
|
if api_key.department_id:
|
|
|
|
|
|
result = await db.execute(
|
|
|
|
|
|
select(User).filter(User.department_id == api_key.department_id, User.role.in_(["admin", "superadmin"]))
|
|
|
|
|
|
)
|
|
|
|
|
|
user = result.scalar_one_or_none()
|
|
|
|
|
|
if user and not user.is_deleted:
|
|
|
|
|
|
return user, api_key
|
|
|
|
|
|
|
|
|
|
|
|
result = await db.execute(select(User).filter(User.role == "superadmin", User.is_deleted == 0).limit(1))
|
|
|
|
|
|
user = result.scalar_one_or_none()
|
|
|
|
|
|
if user:
|
|
|
|
|
|
return user, api_key
|
|
|
|
|
|
|
|
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-12-04 10:11:28 +08:00
|
|
|
|
# 获取当前用户(异步版本)
|
2026-03-23 09:27:01 +08:00
|
|
|
|
async def get_current_user(
|
|
|
|
|
|
authorization: str | None = Header(None),
|
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
|
|
|
|
|
):
|
2025-05-02 23:56:59 +08:00
|
|
|
|
credentials_exception = HTTPException(
|
|
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
|
|
detail="无效的凭证",
|
|
|
|
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-03-23 09:27:01 +08:00
|
|
|
|
if authorization is None:
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
if not authorization.startswith("Bearer "):
|
2025-05-02 23:56:59 +08:00
|
|
|
|
return None
|
|
|
|
|
|
|
2026-03-23 09:27:01 +08:00
|
|
|
|
token = authorization.split("Bearer ")[1]
|
|
|
|
|
|
if not token:
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
# 根据 token 前缀判断认证方式
|
|
|
|
|
|
if token.startswith("yxkey_"):
|
|
|
|
|
|
# API Key 认证
|
|
|
|
|
|
user, api_key_obj = await _verify_api_key(token, db)
|
|
|
|
|
|
if user is not None and api_key_obj is not None:
|
|
|
|
|
|
api_key_obj.last_used_at = utc_now_naive()
|
|
|
|
|
|
await db.commit()
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
# JWT Token 认证
|
2025-05-02 23:56:59 +08:00
|
|
|
|
try:
|
|
|
|
|
|
payload = AuthUtils.verify_access_token(token)
|
|
|
|
|
|
user_id = payload.get("sub")
|
|
|
|
|
|
if user_id is None:
|
|
|
|
|
|
raise credentials_exception
|
|
|
|
|
|
except JWTError:
|
|
|
|
|
|
raise credentials_exception
|
2025-05-04 21:04:01 +08:00
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
2026-03-23 09:27:01 +08:00
|
|
|
|
detail=str(e),
|
2025-05-04 21:04:01 +08:00
|
|
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
|
|
|
|
)
|
2025-05-02 23:56:59 +08:00
|
|
|
|
|
2026-01-21 19:15:52 +08:00
|
|
|
|
result = await db.execute(select(User).filter(User.id == int(user_id)))
|
2025-12-04 10:11:28 +08:00
|
|
|
|
user = result.scalar_one_or_none()
|
2025-05-02 23:56:59 +08:00
|
|
|
|
if user is None:
|
|
|
|
|
|
raise credentials_exception
|
|
|
|
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-05-02 23:56:59 +08:00
|
|
|
|
# 获取已登录用户(抛出401如果未登录)
|
2025-05-24 11:29:45 +08:00
|
|
|
|
async def get_required_user(user: User | None = Depends(get_current_user)):
|
2025-05-02 23:56:59 +08:00
|
|
|
|
if user is None:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
|
|
detail="请登录后再访问",
|
|
|
|
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
|
|
|
|
)
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
2025-12-04 10:12:01 +08:00
|
|
|
|
|
2025-05-02 23:56:59 +08:00
|
|
|
|
# 获取管理员用户
|
|
|
|
|
|
async def get_admin_user(current_user: User = Depends(get_required_user)):
|
|
|
|
|
|
if current_user.role not in ["admin", "superadmin"]:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
|
|
|
|
detail="需要管理员权限",
|
|
|
|
|
|
)
|
|
|
|
|
|
return current_user
|
|
|
|
|
|
|
2025-12-04 10:12:01 +08:00
|
|
|
|
|
2025-05-02 23:56:59 +08:00
|
|
|
|
# 获取超级管理员用户
|
|
|
|
|
|
async def get_superadmin_user(current_user: User = Depends(get_required_user)):
|
|
|
|
|
|
if current_user.role != "superadmin":
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
|
|
|
|
detail="需要超级管理员权限",
|
|
|
|
|
|
)
|
|
|
|
|
|
return current_user
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-05-02 23:56:59 +08:00
|
|
|
|
# 检查路径是否为公开路径
|
|
|
|
|
|
def is_public_path(path: str) -> bool:
|
2025-09-01 22:37:03 +08:00
|
|
|
|
path = path.rstrip("/") # 去除尾部斜杠以便于匹配
|
2025-05-02 23:56:59 +08:00
|
|
|
|
for pattern in PUBLIC_PATHS:
|
|
|
|
|
|
if re.match(pattern, path):
|
|
|
|
|
|
return True
|
|
|
|
|
|
return False
|