fix: 保活按登录成功后的入口再走一遍
This commit is contained in:
+44
-13
@@ -321,28 +321,59 @@ class NqiClient:
|
|||||||
return _public_user(await self._cas_user_payload())
|
return _public_user(await self._cas_user_payload())
|
||||||
|
|
||||||
async def renew_ticket(self) -> bool:
|
async def renew_ticket(self) -> bool:
|
||||||
"""用单点票据进入即席查询。票据还有效时,这一步会把它续上。"""
|
"""登录成功后的入口再走一遍。票据还在就会换成新的应用会话。"""
|
||||||
from app.nqi.adhoc import ensure_session
|
|
||||||
|
|
||||||
saved = self.export_cookies()
|
saved = self.export_cookies()
|
||||||
try:
|
try:
|
||||||
await ensure_session(self)
|
await self.replay_login_landing()
|
||||||
except NqiAuthError:
|
except NqiAuthError:
|
||||||
self.replace_cookies(saved)
|
self.replace_cookies(saved)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def touch_session(self) -> dict:
|
async def touch_session(self) -> dict:
|
||||||
data = await self._cas_user_payload()
|
return await self.replay_login_landing()
|
||||||
|
|
||||||
|
async def replay_login_landing(self) -> dict:
|
||||||
|
"""按浏览器登录成功后的顺序访问门户和即席查询,让单点票据被实际用到。"""
|
||||||
|
ticket = self.ticket()
|
||||||
|
if not ticket:
|
||||||
|
raise NqiAuthError("登录已失效,请重新登录")
|
||||||
|
page = {
|
||||||
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||||
|
"Referer": f"{ORIGIN}/pro-portal/",
|
||||||
|
"Upgrade-Insecure-Requests": "1",
|
||||||
|
}
|
||||||
|
api = {"Referer": f"{ORIGIN}/pro-portal/", "X-Requested-With": "XMLHttpRequest", "tgc": ticket}
|
||||||
|
await self._visit(f"{ORIGIN}/pro-portal/", headers=page)
|
||||||
|
for path in (
|
||||||
|
"/pro-portal/pure/Home!getUserInfo.action",
|
||||||
|
"/pro-portal/pure/Post!postMessage.action",
|
||||||
|
"/pro-portal/pure/LogEnter!getOnLineCount.action",
|
||||||
|
):
|
||||||
|
await self._visit(f"{ORIGIN}{path}", headers=api, required=False)
|
||||||
await self._touch_password()
|
await self._touch_password()
|
||||||
await self._touch_online()
|
user = await self._cas_user_payload()
|
||||||
role_id = str(data.get("roleId") or "")
|
await self._visit(CAS_USER, headers=api, params={"tickets": ticket}, required=False)
|
||||||
if role_id:
|
await self._visit(
|
||||||
try:
|
f"{ORIGIN}/pro-portal/pure/urlAction.action",
|
||||||
await self._signed_get(f"{GIS_API}/base/v2/roleCityMapping/cities", {"roleCodes": role_id})
|
headers=page,
|
||||||
except NqiError:
|
params={"url": "pro-adhoc/index", "__PID": "JXCX", "token": ticket},
|
||||||
pass
|
)
|
||||||
return _public_user(data)
|
await self._visit(f"{ORIGIN}/pro-adhoc/index", headers=page)
|
||||||
|
return _public_user(user)
|
||||||
|
|
||||||
|
async def _visit(self, url: str, *, headers: dict, params: dict | None = None, required: bool = True) -> httpx.Response | None:
|
||||||
|
try:
|
||||||
|
response = await self._request("GET", url, headers=headers, params=params, check_auth=False)
|
||||||
|
except NqiNetworkError:
|
||||||
|
if required:
|
||||||
|
raise
|
||||||
|
return None
|
||||||
|
if response.status_code >= 400 or _landed_on_login(response):
|
||||||
|
if required:
|
||||||
|
raise NqiAuthError("登录已失效,请重新登录")
|
||||||
|
return response
|
||||||
|
return response
|
||||||
|
|
||||||
async def _touch_password(self) -> None:
|
async def _touch_password(self) -> None:
|
||||||
headers = {
|
headers = {
|
||||||
|
|||||||
@@ -386,10 +386,7 @@ async def _touch(cookies: list):
|
|||||||
try:
|
try:
|
||||||
client.import_cookies(cookies)
|
client.import_cookies(cookies)
|
||||||
user = await client.touch_session()
|
user = await client.touch_session()
|
||||||
renewed = await client.renew_ticket()
|
return True, user, client.export_cookies()
|
||||||
if not renewed:
|
|
||||||
logger.info("门户登录仍有效,单点票据没有续上")
|
|
||||||
return renewed, user, client.export_cookies()
|
|
||||||
except NqiAuthError:
|
except NqiAuthError:
|
||||||
return False, None, cookies
|
return False, None, cookies
|
||||||
except (NqiError, OSError):
|
except (NqiError, OSError):
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import asyncio
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
|
||||||
|
from app.nqi.client import NqiClient
|
||||||
|
|
||||||
|
|
||||||
|
def test_keepalive_walks_the_post_login_pages():
|
||||||
|
seen: list[tuple[str, str]] = []
|
||||||
|
|
||||||
|
def handler(request: httpx.Request) -> httpx.Response:
|
||||||
|
seen.append((request.method, request.url.path))
|
||||||
|
if request.url.path.endswith("/getCasUser"):
|
||||||
|
return httpx.Response(200, json={"loginId": "a", "userName": "测试", "cityName": "江门", "orgName": ""})
|
||||||
|
return httpx.Response(200, text="<html>ok</html>")
|
||||||
|
|
||||||
|
async def run():
|
||||||
|
client = NqiClient()
|
||||||
|
await client.aclose()
|
||||||
|
client.client = httpx.AsyncClient(transport=httpx.MockTransport(handler))
|
||||||
|
client.import_cookies([{"name": "CASTGC", "value": "TGT-test", "domain": "nqi.gmcc.net", "path": "/"}])
|
||||||
|
try:
|
||||||
|
user = await client.replay_login_landing()
|
||||||
|
finally:
|
||||||
|
await client.aclose()
|
||||||
|
return user
|
||||||
|
|
||||||
|
user = asyncio.run(run())
|
||||||
|
paths = [path for _method, path in seen]
|
||||||
|
assert user["loginId"] == "a"
|
||||||
|
assert "/pro-portal/" in paths
|
||||||
|
assert "/pro-portal/pure/Home!getUserInfo.action" in paths
|
||||||
|
assert "/pro-portalv2/api/cas/getCasUser" in paths
|
||||||
|
assert "/pro-portal/pure/urlAction.action" in paths
|
||||||
|
assert "/pro-adhoc/index" in paths
|
||||||
|
assert any(item[0] == "POST" and item[1].endswith("/checkUserPasswordUpdated") for item in seen)
|
||||||
Reference in New Issue
Block a user