fix: 登录写入安全 Cookie 时不再被当前 httpx 拒绝

This commit is contained in:
2026-11-16 18:12:41 +08:00
parent 2a8b4e5381
commit 0954c7e134
4 changed files with 62 additions and 9 deletions
+2 -1
View File
@@ -32,6 +32,7 @@ from euniv.client import (
EunivClient,
SessionExpired,
_load_cookies,
set_cookie,
as_uuid,
compact_id,
guard_https,
@@ -859,7 +860,7 @@ class StudySession:
if refreshed:
self._token = refreshed
for domain in ("euniv.zte.com.cn", "api-mall.yxt.com"):
self._client.cookies.set("ELEARNING_00024", refreshed, domain=domain, path="/")
set_cookie(self._client.cookies, "ELEARNING_00024", refreshed, domain=domain, path="/", secure=True)
if resp.status_code == 401:
raise SessionExpired("学习平台登录已失效")
if resp.status_code == 403:
+41 -8
View File
@@ -273,12 +273,45 @@ def _remember_site(account_id: str, client: httpx.Client, token: str = "") -> No
_site_sessions[(account_id, token)] = (now, dumped)
def set_cookie(
cookies: httpx.Cookies,
name: str,
value: str,
domain: str = "",
path: str = "/",
secure: bool = False,
) -> None:
"""httpx 0.28 的 Cookies.set 不能传 secure,直接写入 jar。"""
from http.cookiejar import Cookie
path = path or "/"
domain = domain or ""
cookies.jar.set_cookie(
Cookie(
version=0,
name=name,
value=value,
port=None,
port_specified=False,
domain=domain,
domain_specified=bool(domain),
domain_initial_dot=domain.startswith("."),
path=path,
path_specified=bool(path),
secure=secure,
expires=None,
discard=True,
comment=None,
comment_url=None,
rest={"HttpOnly": None},
rfc2109=False,
)
)
def _load_cookies(client: httpx.Client | httpx.AsyncClient, dumped: list[tuple[str, str, str, str]]) -> None:
for name, value, domain, path in dumped:
kwargs: dict[str, str] = {"path": path or "/"}
if domain:
kwargs["domain"] = domain
client.cookies.set(name, value, **kwargs)
set_cookie(client.cookies, name, value, domain=domain, path=path or "/")
def _by_closeness(items: list[dict], key: str) -> list[dict]:
@@ -1009,7 +1042,7 @@ class EunivClient:
if profile.get("unionId"):
jar["iuacUnionId"] = str(profile["unionId"])
for name, value in jar.items():
self.client.cookies.set(name, value, domain=domain, path="/", secure=True)
set_cookie(self.client.cookies, name, value, domain=domain, path="/", secure=True)
self._uac_post(
"/authentication/token/verify.serv",
@@ -1074,8 +1107,8 @@ class EunivClient:
token = str(credential.get("yxt_token") or "")
self._yxt_token = token
if token:
self.client.cookies.set("ELEARNING_00024", token, domain="euniv.zte.com.cn", path="/", secure=True)
self.client.cookies.set("ELEARNING_00024", token, domain=".yunxuetang.cn", path="/", secure=True)
set_cookie(self.client.cookies, "ELEARNING_00024", token, domain="euniv.zte.com.cn", path="/", secure=True)
set_cookie(self.client.cookies, "ELEARNING_00024", token, domain=".yunxuetang.cn", path="/", secure=True)
def session_token(self) -> str:
for cookie in self.client.cookies.jar:
@@ -1098,7 +1131,7 @@ class EunivClient:
if refreshed:
self._yxt_token = refreshed
for domain in ("euniv.zte.com.cn", ".yunxuetang.cn"):
self.client.cookies.set("ELEARNING_00024", refreshed, domain=domain, path="/", secure=True)
set_cookie(self.client.cookies, "ELEARNING_00024", refreshed, domain=domain, path="/", secure=True)
if response.status_code == 401:
raise SessionExpired("学习平台登录已失效")
if response.status_code == 403: