feat: 支持托管到Metrix应用包并移除授权与平台模式
- 新增 scripts/package_metrix.py 打包应用包(user_auth=metrix、admin_auth=none、数据目录 /data) - 托管模式只认平台代理身份,停用应用登录与改密码,新增 /api/session - 后端按 X-Forwarded-Prefix 注入 <base href>,前端改为相对路径构建并按 basePath 请求 - 托管时隐藏登录页、退出按钮和修改密码页,顶栏显示 Metrix 用户名 - 移除授权到期校验、激活码和连点品牌图标打开的授权延期窗口 - 移除 Metrix 平台数据源/仓库模式及相关配置、接口、设置页和 requests 依赖 - 新增托管鉴权与 base href 注入测试,更新使用与开发文档
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
import asyncio
|
||||
import os
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest import mock
|
||||
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import Response
|
||||
|
||||
from app import main
|
||||
from app.api.routers import auth as auth_router
|
||||
from app.auth import create_jwt_token
|
||||
|
||||
|
||||
PROXY_HEADERS = {
|
||||
"x-metrix-proxy-token": "proxy-secret",
|
||||
"x-metrix-user-id": "7",
|
||||
"x-metrix-username": "alice",
|
||||
"x-metrix-display-name": "%E5%BC%A0%E4%B8%89",
|
||||
"x-metrix-app-role": "user",
|
||||
}
|
||||
|
||||
|
||||
def make_request(path: str, headers: dict[str, str] | None = None) -> Request:
|
||||
raw = [(key.lower().encode("latin-1"), value.encode("latin-1")) for key, value in (headers or {}).items()]
|
||||
return Request({"type": "http", "method": "GET", "path": path, "headers": raw, "query_string": b""})
|
||||
|
||||
|
||||
async def passthrough(_request: Request) -> Response:
|
||||
return Response("ok")
|
||||
|
||||
|
||||
def run_middleware(request: Request) -> Response:
|
||||
return asyncio.run(main.auth_middleware(request, passthrough))
|
||||
|
||||
|
||||
class HostedAuthTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
patcher = mock.patch.dict(os.environ, {"METRIX_APP_PROXY_TOKEN": "proxy-secret"})
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
def test_platform_identity_is_accepted(self):
|
||||
request = make_request("/api/config", PROXY_HEADERS)
|
||||
response = run_middleware(request)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(request.state.auth_context.kind, "platform")
|
||||
self.assertEqual(request.state.auth_context.payload["name"], "张三")
|
||||
|
||||
def test_wrong_proxy_token_is_rejected(self):
|
||||
headers = {**PROXY_HEADERS, "x-metrix-proxy-token": "forged"}
|
||||
self.assertEqual(run_middleware(make_request("/api/database/tables", headers)).status_code, 401)
|
||||
|
||||
def test_unknown_role_is_rejected(self):
|
||||
headers = {**PROXY_HEADERS, "x-metrix-app-role": "guest"}
|
||||
self.assertEqual(run_middleware(make_request("/api/history", headers)).status_code, 401)
|
||||
|
||||
def test_app_jwt_is_not_accepted(self):
|
||||
token = create_jwt_token({"user": "root"})
|
||||
request = make_request("/api/config", {"authorization": f"Bearer {token}"})
|
||||
self.assertEqual(run_middleware(request).status_code, 401)
|
||||
|
||||
def test_login_and_change_password_are_disabled(self):
|
||||
for path in ("/api/login", "/api/change-password"):
|
||||
self.assertEqual(run_middleware(make_request(path, PROXY_HEADERS)).status_code, 404)
|
||||
|
||||
def test_frontend_and_session_pass_through(self):
|
||||
for path in ("/", "/upload", "/assets/index.js", "/api/session"):
|
||||
self.assertEqual(run_middleware(make_request(path)).status_code, 200)
|
||||
|
||||
def test_session_reports_platform_user(self):
|
||||
result = asyncio.run(auth_router.get_session(make_request("/api/session", PROXY_HEADERS)))
|
||||
self.assertEqual(result, {"hosted": True, "user": {"name": "张三", "role": "user"}})
|
||||
|
||||
|
||||
class StandaloneAuthTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
patcher = mock.patch.dict(os.environ, {"METRIX_APP_PROXY_TOKEN": ""})
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
def test_proxy_headers_are_ignored(self):
|
||||
self.assertEqual(run_middleware(make_request("/api/config", PROXY_HEADERS)).status_code, 401)
|
||||
|
||||
def test_app_jwt_is_accepted(self):
|
||||
token = create_jwt_token({"user": "root"})
|
||||
request = make_request("/api/history", {"authorization": f"Bearer {token}"})
|
||||
self.assertEqual(run_middleware(request).status_code, 200)
|
||||
|
||||
def test_login_and_session_are_open(self):
|
||||
for path in ("/api/login", "/api/session"):
|
||||
self.assertEqual(run_middleware(make_request(path)).status_code, 200)
|
||||
|
||||
def test_session_reports_standalone(self):
|
||||
result = asyncio.run(auth_router.get_session(make_request("/api/session", PROXY_HEADERS)))
|
||||
self.assertEqual(result, {"hosted": False, "user": None})
|
||||
|
||||
|
||||
class RenderIndexTests(unittest.TestCase):
|
||||
def render(self, html: str, prefix: str):
|
||||
with TemporaryDirectory() as folder:
|
||||
index = Path(folder) / "index.html"
|
||||
index.write_text(html, encoding="utf-8")
|
||||
return main._render_index(index, prefix)
|
||||
|
||||
def test_hosted_prefix_replaces_base_tag(self):
|
||||
response = self.render('<html><head><base href="/" /></head></html>', "/apps/capacityreport")
|
||||
self.assertIn('<base href="/apps/capacityreport/" />', response.body.decode())
|
||||
self.assertEqual(response.headers["cache-control"], "no-store")
|
||||
self.assertEqual(response.headers["vary"], "X-Forwarded-Prefix")
|
||||
|
||||
def test_invalid_prefix_falls_back_to_root(self):
|
||||
response = self.render("<html><head></head></html>", '/apps/x"><script>')
|
||||
self.assertIn('<base href="/" />', response.body.decode())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user