diff --git a/src/ekb_cli/api/platform/easo_client.py b/src/ekb_cli/api/platform/easo_client.py index 7e6a1f0..e135088 100644 --- a/src/ekb_cli/api/platform/easo_client.py +++ b/src/ekb_cli/api/platform/easo_client.py @@ -1,19 +1,23 @@ """EASO HTTP 客户端(传输层)。 与快报主站的 EkbClient 并列,同属 api/ 传输层。 -区别:EASO 工作流子域使用 Bearer JWT 认证,域名固定为 unity.ekuaibao.com。 +区别:EASO 工作流子域使用 Bearer realm token 认证,域名固定为 unity.ekuaibao.com。 -EASO JWT 完整换取链路(三步,均在 get_easo_client() 内自动完成): +EASO 完整认证链路(四步,均在 get_easo_client() 内自动完成): 1. 主站 accessToken → GET business-center.ekuaibao.com/auth/oauth2/grant/ekb/accessToken - → hose-access-token(长期 JWT,通过 Cookie 传递给下一步) + → hose-access-token(长期 JWT,约 30 天) - 2. accessToken + hose-access-token Cookie - → GET app.ekuaibao.com/api/engine/realm/$EASO/actions/jwt - → EASO Bearer JWT(短期,约 2 小时有效) + 2. 主站 accessToken + corpId/staffId Headers + → GET app.ekuaibao.com/api/engine/realm/$UNITY/actions/jwt + → UNITY Bearer JWT(约 2 小时,aud=UNITY) - 3. EASO Bearer JWT → Authorization: Bearer - → unity.ekuaibao.com 工作流接口 + 3. UNITY JWT + hoseaccesstoken Header + → GET unity.ekuaibao.com/api/realms/EASO?corpId=... + → EASO realm accessToken(约 2 小时,短字符串) + + 4. EASO realm accessToken → Authorization: Bearer + → unity.ekuaibao.com 工作流接口(/api/easo/...) 调用方无需关心上述细节,通过 get_easo_client() 直接获取可用的 EasoClient 实例。 """ @@ -35,20 +39,28 @@ _UA = ( class EasoClient: - """EASO 工作流客户端(unity.ekuaibao.com),使用 Bearer JWT 认证。""" + """EASO 工作流客户端(unity.ekuaibao.com),使用 EASO realm token 认证。""" - def __init__(self, corp_id: str, access_token: str, bearer_token: str): + def __init__( + self, + corp_id: str, + access_token: str, + easo_token: str, + hose_token: str, + ): self.corp_id = corp_id self._client = httpx.Client( base_url=EASO_BASE_URL, headers={ - "Authorization": f"Bearer {bearer_token}", + "Authorization": f"Bearer {easo_token}", "accesstoken": access_token, "corporationid": corp_id, + "hoseaccesstoken": hose_token, "Accept": "application/json", "Content-Type": "application/json", "User-Agent": _UA, }, + cookies={"hose-access-token": hose_token}, timeout=30, ) @@ -82,10 +94,7 @@ class EasoClient: # ── 内部工具函数 ────────────────────────────────────────────── 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 换取接口。 - """ + """Step 1:用主站 accessToken 换取 hose-access-token(business-center JWT,约 30 天)。""" resp = httpx.get( f"{_BUSINESS_CENTER_URL}/auth/oauth2/grant/ekb/accessToken", params={"corpId": corp_id, "ekbAccessToken": access_token, "environment": "app"}, @@ -102,10 +111,13 @@ def _fetch_hose_token(access_token: str, corp_id: str) -> str: 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 小时)。""" +def _fetch_unity_jwt(access_token: str, corp_id: str, staff_id: str) -> str: + """Step 2:换取 UNITY Bearer JWT(aud=UNITY,约 2 小时)。 + + 该 JWT 用于访问 unity.ekuaibao.com 基础接口(如 /api/store/me、/api/realms/EASO)。 + """ resp = httpx.get( - f"{_EKB_BASE_URL}/api/engine/realm/$EASO/actions/jwt", + f"{_EKB_BASE_URL}/api/engine/realm/$UNITY/actions/jwt", params={"corpId": corp_id}, headers={ "accesstoken": access_token, @@ -115,6 +127,38 @@ def _fetch_easo_jwt(access_token: str, corp_id: str, staff_id: str, hose_token: "Content-Type": "application/json", "User-Agent": _UA, }, + 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")) + unity_jwt = raw.get("id") or raw.get("value", "") + if not unity_jwt: + raise ApiResponseError(0, f"UNITY JWT 换取失败,响应结构异常:{raw}") + return unity_jwt + + +def _fetch_easo_realm_token(corp_id: str, unity_jwt: str, hose_token: str) -> str: + """Step 3:用 UNITY JWT 向 unity.ekuaibao.com 换取 EASO realm accessToken(约 2 小时)。 + + 这是实际用于 /api/easo/... 工作流接口的短字符串 Bearer token。 + """ + resp = httpx.get( + f"{EASO_BASE_URL}/api/realms/EASO", + params={"corpId": corp_id}, + headers={ + "Authorization": f"Bearer {unity_jwt}", + "hoseaccesstoken": hose_token, + "Accept": "application/json", + "Content-Type": "application/json", + "User-Agent": _UA, + }, cookies={"hose-access-token": hose_token}, timeout=30, ) @@ -126,19 +170,19 @@ def _fetch_easo_jwt(access_token: str, corp_id: str, staff_id: str, hose_token: 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 + raise AuthenticationError(raw.get("errorMessage", "EASO 认证失败")) + easo_token = (raw.get("value") or {}).get("accessToken", "") + if not easo_token: + raise ApiResponseError(0, f"EASO realm token 换取失败,响应结构异常:{raw}") + return easo_token # ── 工厂函数 ───────────────────────────────────────────────── def get_easo_client() -> EasoClient: - """从当前配置创建 EasoClient,自动完成三步认证链路。 + """从当前配置创建 EasoClient,自动完成四步认证链路。 - 调用方(services/platform 层)无需关心 hose-access-token 或 EASO JWT 细节。 + 调用方(services/platform 层)无需关心 hose-access-token、UNITY JWT 或 EASO realm token 细节。 未登录时抛出 NotLoggedInError。 """ from ekb_cli import config @@ -151,10 +195,12 @@ def get_easo_client() -> EasoClient: staff_id = corp["staff_id"] hose_token = _fetch_hose_token(access_token, corp_id) - bearer = _fetch_easo_jwt(access_token, corp_id, staff_id, hose_token) + unity_jwt = _fetch_unity_jwt(access_token, corp_id, staff_id) + easo_token = _fetch_easo_realm_token(corp_id, unity_jwt, hose_token) return EasoClient( corp_id=corp_id, access_token=access_token, - bearer_token=bearer, + easo_token=easo_token, + hose_token=hose_token, )