diff --git a/backend/euniv/autostudy.py b/backend/euniv/autostudy.py index 685e6be..4b59282 100644 --- a/backend/euniv/autostudy.py +++ b/backend/euniv/autostudy.py @@ -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: diff --git a/backend/euniv/client.py b/backend/euniv/client.py index 0bb8e0d..f467e0c 100644 --- a/backend/euniv/client.py +++ b/backend/euniv/client.py @@ -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: diff --git a/backend/tests/test_uac.py b/backend/tests/test_uac.py index c1c9f24..73370f2 100644 --- a/backend/tests/test_uac.py +++ b/backend/tests/test_uac.py @@ -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() diff --git a/web/src/features/auth/LoginView.vue b/web/src/features/auth/LoginView.vue index f31238d..a382a69 100644 --- a/web/src/features/auth/LoginView.vue +++ b/web/src/features/auth/LoginView.vue @@ -283,6 +283,10 @@ async function submit() { }); const payload = await response.json().catch(() => ({})); if (!response.ok) { + if (response.status >= 500) { + errorText.value = "登录没有完成,请稍后再试。"; + return; + } errorText.value = readableDetail(payload.detail, "登录失败"); return; }