- 新增 scripts/package_metrix.py 打包应用包(user_auth=metrix、admin_auth=none、数据目录 /data) - 托管模式只认平台代理身份,停用应用登录与改密码,新增 /api/session - 后端按 X-Forwarded-Prefix 注入 <base href>,前端改为相对路径构建并按 basePath 请求 - 托管时隐藏登录页、退出按钮和修改密码页,顶栏显示 Metrix 用户名 - 移除授权到期校验、激活码和连点品牌图标打开的授权延期窗口 - 移除 Metrix 平台数据源/仓库模式及相关配置、接口、设置页和 requests 依赖 - 新增托管鉴权与 base href 注入测试,更新使用与开发文档
51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
from fastapi import APIRouter, Body, Request
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.auth import (
|
|
create_jwt_token,
|
|
get_auth_config,
|
|
is_hosted,
|
|
resolve_platform_context,
|
|
save_auth_password,
|
|
)
|
|
|
|
|
|
router = APIRouter(tags=["auth"])
|
|
|
|
|
|
@router.get("/api/session")
|
|
async def get_session(request: Request):
|
|
if not is_hosted():
|
|
return {"hosted": False, "user": None}
|
|
context = resolve_platform_context(request)
|
|
user = {"name": context.payload["name"], "role": context.payload["role"]} if context else None
|
|
return {"hosted": True, "user": user}
|
|
|
|
|
|
@router.post("/api/login")
|
|
async def login(
|
|
username: str = Body(..., embed=True),
|
|
password: str = Body(..., embed=True),
|
|
):
|
|
auth = get_auth_config()
|
|
if username != auth["username"] or password != auth["password"]:
|
|
return JSONResponse(status_code=401, content={"detail": "账号或密码错误"})
|
|
|
|
token = create_jwt_token({"user": username})
|
|
return {"success": True, "token": token}
|
|
|
|
|
|
@router.post("/api/change-password")
|
|
async def change_password(
|
|
current_password: str = Body(..., embed=True),
|
|
new_password: str = Body(..., embed=True),
|
|
):
|
|
auth = get_auth_config()
|
|
if current_password != auth["password"]:
|
|
return JSONResponse(status_code=400, content={"detail": "当前密码错误"})
|
|
if len(new_password) < 4:
|
|
return JSONResponse(status_code=400, content={"detail": "新密码长度不能少于 4 位"})
|
|
|
|
save_auth_password(new_password)
|
|
return {"success": True, "message": "密码修改成功"}
|