feat: 实现v3版本的中心机与部署壳拆分登录架构
此提交完成了核心架构改造: 1. 新增钉钉直连接登录链路,使用浏览器本地跳转而非云端中转 2. 实现壳本机会话持久化,重启后不丢失登录态 3. 重构后端API鉴权,支持Bearer Token身份验证 4. 重写ProfilePanel与LoginPage页面样式与交互逻辑 5. 新增文档说明完整的拆分重构方案 主要变更包括: - 新增/auth/exchange、/api/auth/dingtalk等登录相关接口 - 重构plugins/allowed_platforms接口支持新鉴权方式 - 实现云端会话token管理与本地会话落盘 - 替换旧登录页为分屏设计,支持钉钉扫码直连登录
This commit is contained in:
+109
-16
@@ -27,6 +27,7 @@ import re
|
||||
import secrets
|
||||
import sqlite3
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import zipfile
|
||||
from contextlib import asynccontextmanager, contextmanager
|
||||
@@ -470,12 +471,20 @@ def health() -> dict:
|
||||
|
||||
|
||||
@app.get("/api/plugins", response_model=dict)
|
||||
def plugins(account: str = Query(..., description="账号"),
|
||||
x_api_token: str = Header(default="", alias="X-API-Token")) -> dict:
|
||||
"""按账号返回可用插件清单(权限隔离):无权限的账号返回空列表。"""
|
||||
_check_token(x_api_token)
|
||||
if not account.strip():
|
||||
raise HTTPException(400, "account 不能为空")
|
||||
def plugins(account: str = Query(default="", description="账号(旧底座按此传)"),
|
||||
x_api_token: str = Header(default="", alias="X-API-Token"),
|
||||
authorization: str = Header(default="", alias="Authorization")) -> dict:
|
||||
"""按账号返回可用插件清单(权限隔离):无权限的账号返回空列表。
|
||||
|
||||
§2.2:优先用云端会话 token(Authorization: Bearer <token>)解出工号,不再明文传 account;
|
||||
兜底兼容旧底座 `account?` + X-API-Token 的调用方式。
|
||||
"""
|
||||
acct = _cloud_account(authorization)
|
||||
if not acct:
|
||||
_check_token(x_api_token)
|
||||
acct = (account or "").strip()
|
||||
if not acct:
|
||||
raise HTTPException(400, "缺少有效会话或 account")
|
||||
with _db() as conn:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
@@ -485,24 +494,28 @@ def plugins(account: str = Query(..., description="账号"),
|
||||
WHERE ap.account = ?
|
||||
ORDER BY p.id
|
||||
""",
|
||||
(account.strip(),),
|
||||
(acct,),
|
||||
).fetchall()
|
||||
return {"account": account.strip(), "plugins": [PluginOut(**dict(r)).model_dump() for r in rows]}
|
||||
return {"account": acct, "plugins": [PluginOut(**dict(r)).model_dump() for r in rows]}
|
||||
|
||||
|
||||
@app.get("/api/platforms/allowed", response_model=dict)
|
||||
def platforms_allowed(account: str = Query(..., description="账号"),
|
||||
x_api_token: str = Header(default="", alias="X-API-Token")) -> dict:
|
||||
"""按账号返回可登录平台白名单(与插件权限同模式)。"""
|
||||
_check_token(x_api_token)
|
||||
if not account.strip():
|
||||
raise HTTPException(400, "account 不能为空")
|
||||
def platforms_allowed(account: str = Query(default="", description="账号(旧底座按此传)"),
|
||||
x_api_token: str = Header(default="", alias="X-API-Token"),
|
||||
authorization: str = Header(default="", alias="Authorization")) -> dict:
|
||||
"""按账号返回可登录平台白名单(与插件权限同模式)。支持云端会话 token 或旧底座 account 两种身份来源。"""
|
||||
acct = _cloud_account(authorization)
|
||||
if not acct:
|
||||
_check_token(x_api_token)
|
||||
acct = (account or "").strip()
|
||||
if not acct:
|
||||
raise HTTPException(400, "缺少有效会话或 account")
|
||||
with _db() as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT platform_id FROM account_platforms WHERE account = ? ORDER BY platform_id",
|
||||
(account.strip(),),
|
||||
(acct,),
|
||||
).fetchall()
|
||||
return {"account": account.strip(), "allowed": [dict(r)["platform_id"] for r in rows]}
|
||||
return {"account": acct, "allowed": [dict(r)["platform_id"] for r in rows]}
|
||||
|
||||
|
||||
def _load_manifest(raw: str) -> dict:
|
||||
@@ -1089,6 +1102,86 @@ def dingtalk_inline_qr(back: str = Query(default="", description="(兼容保留
|
||||
return {"goto": _q(auth_url)}
|
||||
|
||||
|
||||
# ==================== 员工端登录 /auth/exchange(v3 最简方案,壳本机直连接 code) ====================
|
||||
# 安全要点:
|
||||
# 1. 钉钉扫码是浏览器跳转,redirect_uri 固定为壳本机 http://localhost:8799(多壳共用);
|
||||
# 浏览器把 code 直接交给壳,云端不参与中转(无 /auth/dingtalk/* 、无 poll、无内部一次性 code)。
|
||||
# 2. /auth/exchange 是壳登录唯一云端接口:壳把浏览器带回的钉钉 code POST 到此,
|
||||
# 云端用 AppSecret 换身份 → 只验证不建号 → 签发云端会话 token(供壳调 /api/plugins 鉴权,见 §2.2)。
|
||||
# 3. code 一次性、短时效;账号不存在时只验证不建号,返回 exists=False,壳端提示联系管理员。
|
||||
# 说明:本链路独立新增,不改动现有 /api/admin/dingtalk/scan→/cb(后台登录仍走原路)。
|
||||
_CLOUD_TOKEN_TTL = 7 * 24 * 3600 # 云端会话 token 有效期(秒,7 天)
|
||||
_cloud_tokens: dict = {} # token -> {exp, account, nickname};壳持此调云端 API(§2.2)
|
||||
_cloud_lock = threading.Lock()
|
||||
|
||||
|
||||
def _auth_account_lookup(account: str) -> dict:
|
||||
"""仅查询统一账号是否存在(登录链路不建号,建号只在管理员入口 /api/admin/dingtalk/auth)。
|
||||
|
||||
返回 {"exists": bool, "nickname": 账号记录昵称或空},绝不 INSERT accounts。
|
||||
"""
|
||||
if not account.strip():
|
||||
return {"exists": False, "nickname": ""}
|
||||
with _db() as conn:
|
||||
row = conn.execute(
|
||||
"SELECT nickname FROM accounts WHERE account = ?", (account.strip(),)).fetchone()
|
||||
return {"exists": bool(row), "nickname": (row["nickname"] if row else "") or ""}
|
||||
|
||||
|
||||
def _cloud_account(bearer: str) -> str:
|
||||
"""从 Bearer 云端会话 token 解出工号(§2.2);无效/过期/缺失返回空串。
|
||||
|
||||
壳登录经 /auth/exchange 拿到的云端 token,调 /api/plugins 等带此作为身份(代替明文传 account)。
|
||||
"""
|
||||
token = (bearer or "").strip()
|
||||
if token.lower().startswith("bearer "):
|
||||
token = token[7:].strip()
|
||||
if not token:
|
||||
return ""
|
||||
now = time.time()
|
||||
with _cloud_lock:
|
||||
rec = _cloud_tokens.get(token)
|
||||
if rec and rec["exp"] >= now:
|
||||
return rec["account"]
|
||||
if rec: # 过期顺手清理,防内存无限增长
|
||||
_cloud_tokens.pop(token, None)
|
||||
return ""
|
||||
|
||||
|
||||
class AuthExchangeIn(BaseModel):
|
||||
code: str
|
||||
|
||||
|
||||
@app.post("/auth/exchange")
|
||||
def auth_exchange(d: AuthExchangeIn) -> dict:
|
||||
"""壳 → 云端:壳 POST 钉钉授权码 code,换取员工工号 + 云端会话 token。
|
||||
|
||||
只验证不建号:账号不存在时返回 exists=False,壳端提示联系管理员,不建立会话。
|
||||
成功后签发云端会话 token(_cloud_tokens),壳之后调 /api/plugins 等带此 token(§2.2)。
|
||||
"""
|
||||
code = (d.code or "").strip()
|
||||
if not code:
|
||||
raise HTTPException(400, "缺少 code")
|
||||
if not DINGTALK_APP_KEY or not DINGTALK_APP_SECRET:
|
||||
raise HTTPException(503, "未配置钉钉开放平台应用(DINGTALK_APP_KEY / DINGTALK_APP_SECRET)")
|
||||
info = _dingtalk_userinfo(code) # 用 AppSecret 换身份(复用现有实现,只读不建号)
|
||||
account = (info.get("account") or "").strip()
|
||||
if not account:
|
||||
raise HTTPException(401, "扫码未解析到有效账号,请重新扫码")
|
||||
acct = _auth_account_lookup(account) # 只验证不建号
|
||||
token = secrets.token_urlsafe(32)
|
||||
now = time.time()
|
||||
with _cloud_lock:
|
||||
# 惰性清理过期云端会话,防内存无限增长
|
||||
for k in [k for k, v in _cloud_tokens.items() if v["exp"] < now]:
|
||||
_cloud_tokens.pop(k, None)
|
||||
_cloud_tokens[token] = {"exp": now + _CLOUD_TOKEN_TTL, "account": account,
|
||||
"nickname": acct["nickname"] or (info.get("nickname") or "")}
|
||||
return {"token": token,
|
||||
"account": account, "nickname": acct["nickname"] or (info.get("nickname") or ""),
|
||||
"exists": bool(acct["exists"]), "is_admin": False}
|
||||
|
||||
|
||||
class PwdIn(BaseModel):
|
||||
account: str
|
||||
old_password: str
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user