fix: 平台登录失败后刷新验证码

This commit is contained in:
2026-10-30 12:09:20 +08:00
parent 559548d577
commit 811dc448cb
3 changed files with 51 additions and 11 deletions
+8 -9
View File
@@ -13,7 +13,7 @@ from app.config import installed, result_dir
from app.install import InstallError, perform_install
from app.db import get_session, session_scope
from app.models import AppSetting, CollectionSource, ExportTask, NqiAccount, User, utcnow
from app.nqi.client import NqiAuthError, NqiClient, NqiError, needs_captcha
from app.nqi.client import NqiAuthError, NqiClient, NqiError
from app.nqi.flows import drop, purge, remember, take
from app.security import hash_password, issue_token, verify_password
from app.tasks.runner import check_concurrency, check_period, check_refresh, enqueue_export
@@ -233,14 +233,13 @@ async def nqi_finish(body: dict) -> dict:
user = await client.submit_login(challenge, sms_code)
except NqiError as exc:
message = str(exc)
if needs_captcha(message):
challenge.config_ready = False
try:
await client.refresh_captcha(challenge)
except NqiError:
pass
return {**_captcha_body(session_id, challenge), "status": "need_captcha", "message": message}
raise _bad(message) from exc
challenge.config_ready = False
challenge.captcha = ""
try:
await client.refresh_captcha(challenge)
except NqiError:
pass
return {**_captcha_body(session_id, challenge), "status": "need_captcha", "message": message}
challenge.password = ""
cookies = client.export_cookies()
await client.aclose()
+39
View File
@@ -142,6 +142,45 @@ def test_admin_is_judged_locally_when_users_use_nqi(client: TestClient, monkeypa
assert calls == ["open"]
def test_platform_login_failure_refreshes_captcha(client: TestClient, monkeypatch):
from app.nqi.client import LoginChallenge, NqiError
logged = client.post("/api/auth/local", json={"username": "admin", "password": "admin-pass"})
headers = {"Authorization": f"Bearer {logged.json()['token']}"}
saved = client.put(
"/api/admin/settings",
json={"loginMode": "nqi", "credentialRefreshSeconds": 300, "workerConcurrency": 4},
headers=headers,
)
assert saved.status_code == 200
async def open_login(self):
return LoginChallenge(execution="exec", public_key=PUBLIC_KEY, captcha_png=b"old", page_url="https://nqi.example/login")
async def prepare_login(self, challenge, username, password, captcha):
raise NqiError("认证失败2!")
async def refresh_captcha(self, challenge):
challenge.captcha_png = b"new"
challenge.captcha_type = "image/png"
return b"new"
monkeypatch.setattr("app.api.http.NqiClient.open_login", open_login)
monkeypatch.setattr("app.api.http.NqiClient.prepare_login", prepare_login)
monkeypatch.setattr("app.api.http.NqiClient.refresh_captcha", refresh_captcha)
started = client.post("/api/auth/nqi/start", json={"purpose": "login"})
assert started.status_code == 200
failed = client.post(
"/api/auth/login",
json={"username": "platform-user", "password": "secret", "captcha": "abcd", "sessionId": started.json()["sessionId"]},
)
assert failed.status_code == 200
body = failed.json()
assert body["status"] == "need_captcha"
assert body["message"] == "认证失败2!"
assert body["captcha"].startswith("data:image/png;base64,")
def test_pool_add_is_allowed_when_login_is_local(client: TestClient, monkeypatch):
from app.nqi.client import NqiError
+4 -2
View File
@@ -80,7 +80,8 @@ async function beginLogin() {
stopCountdown();
smsCode.value = "";
captcha.value = "";
if (result.captcha) captchaUrl.value = result.captcha;
captchaUrl.value = result.captcha || "";
captchaBroken.value = !captchaUrl.value;
errorText.value = result.message || "请重新输入验证码";
return;
}
@@ -218,7 +219,8 @@ async function finishNqi() {
stopCountdown();
smsCode.value = "";
captcha.value = "";
if (result.captcha) captchaUrl.value = result.captcha;
captchaUrl.value = result.captcha || "";
captchaBroken.value = !captchaUrl.value;
errorText.value = result.message || "请重新输入验证码";
return;
}