fix: 登录写入安全 Cookie 时不再被当前 httpx 拒绝
This commit is contained in:
@@ -32,6 +32,7 @@ from euniv.client import (
|
|||||||
EunivClient,
|
EunivClient,
|
||||||
SessionExpired,
|
SessionExpired,
|
||||||
_load_cookies,
|
_load_cookies,
|
||||||
|
set_cookie,
|
||||||
as_uuid,
|
as_uuid,
|
||||||
compact_id,
|
compact_id,
|
||||||
guard_https,
|
guard_https,
|
||||||
@@ -859,7 +860,7 @@ class StudySession:
|
|||||||
if refreshed:
|
if refreshed:
|
||||||
self._token = refreshed
|
self._token = refreshed
|
||||||
for domain in ("euniv.zte.com.cn", "api-mall.yxt.com"):
|
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:
|
if resp.status_code == 401:
|
||||||
raise SessionExpired("学习平台登录已失效")
|
raise SessionExpired("学习平台登录已失效")
|
||||||
if resp.status_code == 403:
|
if resp.status_code == 403:
|
||||||
|
|||||||
+41
-8
@@ -273,12 +273,45 @@ def _remember_site(account_id: str, client: httpx.Client, token: str = "") -> No
|
|||||||
_site_sessions[(account_id, token)] = (now, dumped)
|
_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:
|
def _load_cookies(client: httpx.Client | httpx.AsyncClient, dumped: list[tuple[str, str, str, str]]) -> None:
|
||||||
for name, value, domain, path in dumped:
|
for name, value, domain, path in dumped:
|
||||||
kwargs: dict[str, str] = {"path": path or "/"}
|
set_cookie(client.cookies, name, value, domain=domain, path=path or "/")
|
||||||
if domain:
|
|
||||||
kwargs["domain"] = domain
|
|
||||||
client.cookies.set(name, value, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def _by_closeness(items: list[dict], key: str) -> list[dict]:
|
def _by_closeness(items: list[dict], key: str) -> list[dict]:
|
||||||
@@ -1009,7 +1042,7 @@ class EunivClient:
|
|||||||
if profile.get("unionId"):
|
if profile.get("unionId"):
|
||||||
jar["iuacUnionId"] = str(profile["unionId"])
|
jar["iuacUnionId"] = str(profile["unionId"])
|
||||||
for name, value in jar.items():
|
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(
|
self._uac_post(
|
||||||
"/authentication/token/verify.serv",
|
"/authentication/token/verify.serv",
|
||||||
@@ -1074,8 +1107,8 @@ class EunivClient:
|
|||||||
token = str(credential.get("yxt_token") or "")
|
token = str(credential.get("yxt_token") or "")
|
||||||
self._yxt_token = token
|
self._yxt_token = token
|
||||||
if token:
|
if token:
|
||||||
self.client.cookies.set("ELEARNING_00024", token, domain="euniv.zte.com.cn", path="/", secure=True)
|
set_cookie(self.client.cookies, "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=".yunxuetang.cn", path="/", secure=True)
|
||||||
|
|
||||||
def session_token(self) -> str:
|
def session_token(self) -> str:
|
||||||
for cookie in self.client.cookies.jar:
|
for cookie in self.client.cookies.jar:
|
||||||
@@ -1098,7 +1131,7 @@ class EunivClient:
|
|||||||
if refreshed:
|
if refreshed:
|
||||||
self._yxt_token = refreshed
|
self._yxt_token = refreshed
|
||||||
for domain in ("euniv.zte.com.cn", ".yunxuetang.cn"):
|
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:
|
if response.status_code == 401:
|
||||||
raise SessionExpired("学习平台登录已失效")
|
raise SessionExpired("学习平台登录已失效")
|
||||||
if response.status_code == 403:
|
if response.status_code == 403:
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ from euniv.client import (
|
|||||||
make_qr_payload,
|
make_qr_payload,
|
||||||
parse_uac,
|
parse_uac,
|
||||||
qr_data_uri,
|
qr_data_uri,
|
||||||
|
set_cookie,
|
||||||
)
|
)
|
||||||
from euniv.crypto import decrypt_secret
|
from euniv.crypto import decrypt_secret
|
||||||
from euniv.db import SessionLocal
|
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["ucs_token"] == "ucs-qr"
|
||||||
assert stored["yxt_token"] == "yxt-qr"
|
assert stored["yxt_token"] == "yxt-qr"
|
||||||
assert "secret" not in decrypt_secret(account.password_cipher)
|
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()
|
||||||
|
|||||||
@@ -283,6 +283,10 @@ async function submit() {
|
|||||||
});
|
});
|
||||||
const payload = await response.json().catch(() => ({}));
|
const payload = await response.json().catch(() => ({}));
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
if (response.status >= 500) {
|
||||||
|
errorText.value = "登录没有完成,请稍后再试。";
|
||||||
|
return;
|
||||||
|
}
|
||||||
errorText.value = readableDetail(payload.detail, "登录失败");
|
errorText.value = readableDetail(payload.detail, "登录失败");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user