feat: 对接 UAC 登录并支持 iCenter 扫码

This commit is contained in:
2026-11-15 05:48:04 +08:00
parent 6340309e66
commit 158578d17c
7 changed files with 651 additions and 88 deletions
+73 -1
View File
@@ -11,12 +11,22 @@ from dataclasses import dataclass
from sqlalchemy import select
from sqlalchemy.orm import Session
from euniv.client import EunivClient, JigsawRejected, ManualJigsaw, SessionExpired
from euniv.client import (
QR_PASSWORD_MARK,
EunivClient,
JigsawRejected,
ManualJigsaw,
QrWaiting,
SessionExpired,
is_qr_password,
)
from euniv.crypto import decrypt_secret, encrypt_secret
from euniv.models import Account
_PENDING_SECONDS = 180
_QR_SECONDS = 70
_pending: dict[str, "PendingLogin"] = {}
_pending_qr: dict[str, "PendingQr"] = {}
_credential_guard = threading.Lock()
_credential_locks: dict[str, threading.Lock] = {}
@@ -38,6 +48,12 @@ class PendingLogin:
created: float
@dataclass
class PendingQr:
client: EunivClient
created: float
def _dump(credential: dict) -> str:
return json.dumps(credential, ensure_ascii=False, sort_keys=True)
@@ -48,6 +64,16 @@ def _purge_pending() -> None:
if now - pending.created > _PENDING_SECONDS:
pending.client.close()
_pending.pop(attempt_id, None)
for attempt_id, pending in list(_pending_qr.items()):
if now - pending.created > _QR_SECONDS:
pending.client.close()
_pending_qr.pop(attempt_id, None)
def _drop_qr(attempt_id: str) -> None:
pending = _pending_qr.pop(attempt_id, None)
if pending is not None:
pending.client.close()
def _drop_employee(employee_id: str) -> None:
@@ -180,6 +206,46 @@ def submit_jigsaw(db: Session, attempt_id: str, offset: int) -> dict:
pending.client.close()
def start_qr_login(client_factory=EunivClient) -> dict:
_purge_pending()
client = None
try:
client = client_factory("", "")
_unused_qr, image = client.start_qr()
except Exception:
if client is not None:
client.close()
raise
attempt_id = secrets.token_urlsafe(18)
_pending_qr[attempt_id] = PendingQr(client, time.time())
return {"attempt_id": attempt_id, "qr_image": image, "ttl": 60}
def poll_qr_login(db: Session, attempt_id: str, client_factory=EunivClient) -> dict:
_purge_pending()
pending = _pending_qr.get(attempt_id)
if pending is None:
raise RuntimeError("请重新扫码")
try:
profile = pending.client.poll_qr()
except QrWaiting:
return {"status": "waiting"}
except Exception:
_drop_qr(attempt_id)
raise
_pending_qr.pop(attempt_id, None)
employee_id = str(profile.get("accountId") or profile.get("userName") or "").strip()
if not employee_id:
pending.client.close()
raise RuntimeError("扫码登录没有返回工号")
try:
_persist(db, employee_id, QR_PASSWORD_MARK, pending.client, profile)
_request_course_sync(employee_id)
return {"employee_id": employee_id}
finally:
pending.client.close()
def fetch_home(db: Session, employee_id: str, client_factory=EunivClient) -> dict:
"""向 e 学现查首页信息。个人信息只出现在返回值里,不写入数据库。凭证变化时更新密文。"""
return _live(db, employee_id, client_factory, lambda client: client.home_snapshot())
@@ -233,6 +299,8 @@ def _live(db: Session, employee_id: str, client_factory, read):
try:
if credential.get("yxt_token"):
client.restore(credential)
elif is_qr_password(password):
raise RuntimeError("扫码登录已失效,请重新扫码登录")
else:
try:
client.authorize(client.login(manual=False, auto_attempts=attempts))
@@ -241,6 +309,8 @@ def _live(db: Session, employee_id: str, client_factory, read):
try:
data = read(client)
except SessionExpired:
if is_qr_password(password):
raise RuntimeError("扫码登录已失效,请重新扫码登录") from None
try:
client.authorize(client.login(manual=False, auto_attempts=attempts))
except ManualJigsaw:
@@ -316,6 +386,8 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
account.credential_cipher = encrypt_secret(_dump(updated))
valid = True
checked_alive = True
elif is_qr_password(password):
valid = False
else:
profile = client.login(manual=False, auto_attempts=attempts)
_persist(db, employee_id, password, client, profile)