feat: 保持浏览器登录,并显示我的学时里的本月学时

This commit is contained in:
2026-10-27 04:52:39 +08:00
parent afda5a883f
commit 7bbb80ee9c
10 changed files with 318 additions and 35 deletions
+70 -2
View File
@@ -4,9 +4,11 @@ from __future__ import annotations
import json
import secrets
import threading
import time
from dataclasses import dataclass
from sqlalchemy import select
from sqlalchemy.orm import Session
from euniv.client import EunivClient, JigsawRejected, ManualJigsaw, SessionExpired
@@ -55,14 +57,24 @@ def _persist(db: Session, employee_id: str, password: str, client: EunivClient,
db.commit()
def login_and_store(db: Session, employee_id: str, password: str, client_factory=EunivClient) -> dict:
def login_and_store(
db: Session,
employee_id: str,
password: str,
client_factory=EunivClient,
manual: bool = False,
) -> dict:
_purge_pending()
account = db.get(Account, employee_id)
if account and account.password_cipher and account.credential_cipher:
if decrypt_secret(account.password_cipher) == password and _credential_still_valid(account, client_factory):
return {"employee_id": employee_id}
_drop_employee(employee_id)
client = client_factory(employee_id, password)
held = False
try:
try:
profile = client.login()
profile = client.login(manual=manual)
except ManualJigsaw as exc:
attempt_id = secrets.token_urlsafe(18)
_pending[attempt_id] = PendingLogin(client, employee_id, password, time.time())
@@ -75,6 +87,17 @@ def login_and_store(db: Session, employee_id: str, password: str, client_factory
client.close()
def _credential_still_valid(account: Account, client_factory) -> bool:
client = client_factory(account.employee_id, decrypt_secret(account.password_cipher))
try:
client.restore(json.loads(decrypt_secret(account.credential_cipher)))
return bool(client.credential_alive())
except Exception:
return False
finally:
client.close()
def submit_jigsaw(db: Session, attempt_id: str, offset: int) -> dict:
_purge_pending()
pending = _pending.get(attempt_id)
@@ -129,5 +152,50 @@ def fetch_home(db: Session, employee_id: str, client_factory=EunivClient) -> dic
client.close()
def revoke_credential(db: Session, employee_id: str) -> None:
account = db.get(Account, employee_id)
if account is None:
return
account.credential_cipher = ""
db.commit()
def refresh_stored_credentials(client_factory=EunivClient) -> None:
from euniv.db import SessionLocal
with SessionLocal() as db:
accounts = list(db.scalars(select(Account)))
for account in accounts:
if not account.credential_cipher:
continue
client = client_factory(account.employee_id, decrypt_secret(account.password_cipher))
try:
previous = json.loads(decrypt_secret(account.credential_cipher))
client.restore(previous)
if client.credential_alive():
updated = client.export_credential()
if _dump(updated) != _dump(previous):
account.credential_cipher = encrypt_secret(_dump(updated))
db.commit()
continue
profile = client.login()
_persist(db, account.employee_id, decrypt_secret(account.password_cipher), client, profile)
except Exception:
continue
finally:
client.close()
def start_credential_refresh(interval_seconds: int = 1800) -> threading.Event:
stop = threading.Event()
def loop() -> None:
while not stop.wait(interval_seconds):
refresh_stored_credentials()
threading.Thread(target=loop, name="euniv-credential-refresh", daemon=True).start()
return stop
def stored_password(account: Account) -> str:
return decrypt_secret(account.password_cipher)