fix(easo): 补全三步认证链路(accessToken → hose-access-token → EASO JWT)
服务端对 /engine/realm/$EASO/actions/jwt 校验 hose-access-token Cookie, 直接用 accessToken 不稳定(返回 errorCode 401)。 新增 _fetch_hose_token():用主站 token 换取 business-center JWT(30天有效) 新增 _fetch_easo_jwt():携带 Cookie 换取短期 EASO Bearer JWT(2小时有效) get_easo_client() 自动执行完整链路,调用层无需关心认证细节。
This commit is contained in:
1 parent
232821a06b
commit
5f1986d8b8
1 file changed
+84
-18
@@ -3,11 +3,19 @@
|
||||
与快报主站的 EkbClient 并列,同属 api/ 传输层。
|
||||
区别:EASO 工作流子域使用 Bearer JWT 认证,域名固定为 unity.ekuaibao.com。
|
||||
|
||||
Bearer JWT 通过快报主站接口换取:
|
||||
GET /api/engine/realm/$EASO/actions/jwt → {"id": "<token>"}
|
||||
EASO JWT 完整换取链路(三步,均在 get_easo_client() 内自动完成):
|
||||
1. 主站 accessToken
|
||||
→ GET business-center.ekuaibao.com/auth/oauth2/grant/ekb/accessToken
|
||||
→ hose-access-token(长期 JWT,通过 Cookie 传递给下一步)
|
||||
|
||||
使用方:services/platform/workflow_service 通过 get_easo_client() 获取实例,
|
||||
并在 _cmd 函数中以 with 上下文管理器管理生命周期。
|
||||
2. accessToken + hose-access-token Cookie
|
||||
→ GET app.ekuaibao.com/api/engine/realm/$EASO/actions/jwt
|
||||
→ EASO Bearer JWT(短期,约 2 小时有效)
|
||||
|
||||
3. EASO Bearer JWT → Authorization: Bearer <token>
|
||||
→ unity.ekuaibao.com 工作流接口
|
||||
|
||||
调用方无需关心上述细节,通过 get_easo_client() 直接获取可用的 EasoClient 实例。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -17,6 +25,13 @@ from typing import Any
|
||||
from ekb_cli.errors import AuthenticationError, ApiResponseError, NotLoggedInError
|
||||
|
||||
EASO_BASE_URL = "https://unity.ekuaibao.com"
|
||||
_EKB_BASE_URL = "https://app.ekuaibao.com"
|
||||
_BUSINESS_CENTER_URL = "https://business-center.ekuaibao.com"
|
||||
_UA = (
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||
"Chrome/147.0.0.0 Safari/537.36"
|
||||
)
|
||||
|
||||
|
||||
class EasoClient:
|
||||
@@ -32,11 +47,7 @@ class EasoClient:
|
||||
"corporationid": corp_id,
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": (
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||
"Chrome/147.0.0.0 Safari/537.36"
|
||||
),
|
||||
"User-Agent": _UA,
|
||||
},
|
||||
timeout=30,
|
||||
)
|
||||
@@ -68,27 +79,82 @@ class EasoClient:
|
||||
self._client.close()
|
||||
|
||||
|
||||
def get_easo_client() -> EasoClient:
|
||||
"""从当前配置创建 EasoClient(自动通过快报主站换取 EASO Bearer JWT)。
|
||||
# ── 内部工具函数 ──────────────────────────────────────────────
|
||||
|
||||
def _fetch_hose_token(access_token: str, corp_id: str) -> str:
|
||||
"""Step 1:用主站 accessToken 换取 hose-access-token(business-center JWT)。
|
||||
|
||||
hose-access-token 有效期约 30 天,作为 Cookie 传给 JWT 换取接口。
|
||||
"""
|
||||
resp = httpx.get(
|
||||
f"{_BUSINESS_CENTER_URL}/auth/oauth2/grant/ekb/accessToken",
|
||||
params={"corpId": corp_id, "ekbAccessToken": access_token, "environment": "app"},
|
||||
timeout=10,
|
||||
)
|
||||
try:
|
||||
resp.raise_for_status()
|
||||
except httpx.HTTPStatusError as e:
|
||||
raise ApiResponseError(e.response.status_code, f"hose-access-token 换取失败: {e}")
|
||||
data = resp.json()
|
||||
token = data.get("data", "")
|
||||
if not token:
|
||||
raise ApiResponseError(0, f"hose-access-token 换取失败,响应结构异常:{data}")
|
||||
return token
|
||||
|
||||
|
||||
def _fetch_easo_jwt(access_token: str, corp_id: str, staff_id: str, hose_token: str) -> str:
|
||||
"""Step 2:携带 hose-access-token Cookie 换取 EASO Bearer JWT(有效期约 2 小时)。"""
|
||||
resp = httpx.get(
|
||||
f"{_EKB_BASE_URL}/api/engine/realm/$EASO/actions/jwt",
|
||||
params={"corpId": corp_id},
|
||||
headers={
|
||||
"accesstoken": access_token,
|
||||
"corpid": corp_id,
|
||||
"staffid": staff_id,
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": _UA,
|
||||
},
|
||||
cookies={"hose-access-token": hose_token},
|
||||
timeout=30,
|
||||
)
|
||||
try:
|
||||
resp.raise_for_status()
|
||||
except httpx.HTTPStatusError as e:
|
||||
if e.response.status_code == 401:
|
||||
raise AuthenticationError()
|
||||
raise ApiResponseError(e.response.status_code, str(e))
|
||||
raw = resp.json()
|
||||
if isinstance(raw, dict) and raw.get("errorCode") == 401:
|
||||
raise AuthenticationError(raw.get("errorMessage", "认证已过期,请重新执行 `ekb auth add` 更新 token"))
|
||||
bearer = raw.get("id") or raw.get("value", "")
|
||||
if not bearer:
|
||||
raise ApiResponseError(0, f"EASO JWT 换取失败,响应结构异常:{raw}")
|
||||
return bearer
|
||||
|
||||
|
||||
# ── 工厂函数 ─────────────────────────────────────────────────
|
||||
|
||||
def get_easo_client() -> EasoClient:
|
||||
"""从当前配置创建 EasoClient,自动完成三步认证链路。
|
||||
|
||||
调用方(services/platform 层)无需关心 hose-access-token 或 EASO JWT 细节。
|
||||
未登录时抛出 NotLoggedInError。
|
||||
"""
|
||||
from ekb_cli import config
|
||||
from ekb_cli.api import get_client
|
||||
|
||||
corp = config.get_current_corp()
|
||||
if not corp:
|
||||
raise NotLoggedInError()
|
||||
corp_id = config.get_current_corp_id()
|
||||
access_token = corp["access_token"]
|
||||
staff_id = corp["staff_id"]
|
||||
|
||||
with get_client() as ekb:
|
||||
raw = ekb.get("/engine/realm/$EASO/actions/jwt")
|
||||
bearer = raw.get("id") or raw.get("value", "")
|
||||
if not bearer:
|
||||
raise ApiResponseError(0, f"EASO JWT 换取失败,响应结构异常:{raw}")
|
||||
hose_token = _fetch_hose_token(access_token, corp_id)
|
||||
bearer = _fetch_easo_jwt(access_token, corp_id, staff_id, hose_token)
|
||||
|
||||
return EasoClient(
|
||||
corp_id=corp_id,
|
||||
access_token=corp["access_token"],
|
||||
access_token=access_token,
|
||||
bearer_token=bearer,
|
||||
)
|
||||
Reference in new issue
Block a user