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:
+15
View File
@@ -12,6 +12,7 @@ from euniv.client import (
make_qr_payload,
parse_uac,
qr_data_uri,
set_cookie,
)
from euniv.crypto import decrypt_secret
from euniv.db import SessionLocal
@@ -150,3 +151,17 @@ def test_qr_login_stores_scan_mark(monkeypatch) -> None:
assert stored["ucs_token"] == "ucs-qr"
assert stored["yxt_token"] == "yxt-qr"
assert "secret" not in decrypt_secret(account.password_cipher)
def test_secure_cookie_is_written_through_the_jar() -> None:
client = EunivClient("6650000146", "secret-pass")
try:
set_cookie(client.client.cookies, "PORTALSSOCookie", "token", domain="zte.com.cn", path="/", secure=True)
client.restore({"yxt_token": "learning-token", "ucs_token": "ucs", "ucs_account_id": "6650000146"})
portal = [item for item in client.client.cookies.jar if item.name == "PORTALSSOCookie"]
learning = [item for item in client.client.cookies.jar if item.name == "ELEARNING_00024"]
assert portal and portal[0].secure is True
assert len(learning) == 2
assert all(item.secure for item in learning)
finally:
client.close()