Revert "Fallback to local captcha when reCAPTCHA stalls"
This reverts commit 326c6c3690.
This commit is contained in:
1 parent
f2324ce76b
commit
2fafff7d69
1 file changed
+7
-45
+7
-45
@@ -108,7 +108,6 @@ SESSION_TTL_SECONDS = int(os.environ.get("OWRT_REMOTE_SESSION_TTL", str(30 * 24
|
|||||||
CAPTCHA_TTL_SECONDS = 600
|
CAPTCHA_TTL_SECONDS = 600
|
||||||
CAPTCHA_MODE_DIGITS = "digits"
|
CAPTCHA_MODE_DIGITS = "digits"
|
||||||
CAPTCHA_MODE_RECAPTCHA = "recaptcha"
|
CAPTCHA_MODE_RECAPTCHA = "recaptcha"
|
||||||
RECAPTCHA_FALLBACK_MARKER = "__OWRT_REMOTE_RECAPTCHA_FALLBACK__:"
|
|
||||||
ROUTER_ACCESS_FLAGS = ("A", "B", "G")
|
ROUTER_ACCESS_FLAGS = ("A", "B", "G")
|
||||||
ROUTER_ACCESS_LABELS = {
|
ROUTER_ACCESS_LABELS = {
|
||||||
"A": "Web only",
|
"A": "Web only",
|
||||||
@@ -116,10 +115,6 @@ ROUTER_ACCESS_LABELS = {
|
|||||||
"G": "Full router access",
|
"G": "Full router access",
|
||||||
}
|
}
|
||||||
RECAPTCHA_VERIFY_URL = "https://www.google.com/recaptcha/api/siteverify"
|
RECAPTCHA_VERIFY_URL = "https://www.google.com/recaptcha/api/siteverify"
|
||||||
try:
|
|
||||||
RECAPTCHA_VERIFY_TIMEOUT_SECONDS = max(2.0, min(20.0, float(os.environ.get("OWRT_REMOTE_RECAPTCHA_TIMEOUT", "6"))))
|
|
||||||
except ValueError:
|
|
||||||
RECAPTCHA_VERIFY_TIMEOUT_SECONDS = 6.0
|
|
||||||
AUTH_CHALLENGE_TTL_SECONDS = 300
|
AUTH_CHALLENGE_TTL_SECONDS = 300
|
||||||
TOTP_PERIOD_SECONDS = 30
|
TOTP_PERIOD_SECONDS = 30
|
||||||
TOTP_DIGITS = 6
|
TOTP_DIGITS = 6
|
||||||
@@ -1108,7 +1103,7 @@ def touch_social_account_last_used(auth, provider, account_id):
|
|||||||
return clean
|
return clean
|
||||||
|
|
||||||
|
|
||||||
def oauth_fetch_json(url, method="GET", headers=None, data=None, timeout=15):
|
def oauth_fetch_json(url, method="GET", headers=None, data=None):
|
||||||
payload = None
|
payload = None
|
||||||
header_map = {str(key): str(value) for key, value in dict(headers or {}).items()}
|
header_map = {str(key): str(value) for key, value in dict(headers or {}).items()}
|
||||||
if data is not None:
|
if data is not None:
|
||||||
@@ -1128,7 +1123,7 @@ def oauth_fetch_json(url, method="GET", headers=None, data=None, timeout=15):
|
|||||||
if not request.has_header(key):
|
if not request.has_header(key):
|
||||||
request.add_header(key, value)
|
request.add_header(key, value)
|
||||||
try:
|
try:
|
||||||
with urllib.request.urlopen(request, timeout=float(timeout or 15)) as response:
|
with urllib.request.urlopen(request, timeout=15) as response:
|
||||||
body = response.read().decode("utf-8", "replace")
|
body = response.read().decode("utf-8", "replace")
|
||||||
return json.loads(body or "{}")
|
return json.loads(body or "{}")
|
||||||
except urllib.error.HTTPError as exc:
|
except urllib.error.HTTPError as exc:
|
||||||
@@ -2703,18 +2698,9 @@ def verify_recaptcha_token(secret_key, response_token, remote_ip="", expected_ho
|
|||||||
if remote_ip:
|
if remote_ip:
|
||||||
payload["remoteip"] = str(remote_ip).strip()
|
payload["remoteip"] = str(remote_ip).strip()
|
||||||
try:
|
try:
|
||||||
result = oauth_fetch_json(
|
result = oauth_fetch_json(RECAPTCHA_VERIFY_URL, method="POST", data=payload)
|
||||||
RECAPTCHA_VERIFY_URL,
|
|
||||||
method="POST",
|
|
||||||
data=payload,
|
|
||||||
timeout=RECAPTCHA_VERIFY_TIMEOUT_SECONDS,
|
|
||||||
)
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
detail = " ".join(str(exc or "").split())[:180]
|
return False, f"Не удалось проверить Google reCAPTCHA: {exc}"
|
||||||
message = "Google reCAPTCHA недоступна с VPS. Переключил вход на локальную цифровую капчу ниже: повтори цифры и нажми Войти еще раз."
|
|
||||||
if detail:
|
|
||||||
message = f"{message} Причина: {detail}"
|
|
||||||
return False, f"{RECAPTCHA_FALLBACK_MARKER}{message}"
|
|
||||||
if not result.get("success"):
|
if not result.get("success"):
|
||||||
errors = result.get("error-codes", [])
|
errors = result.get("error-codes", [])
|
||||||
detail = ", ".join(str(item) for item in errors if str(item).strip())
|
detail = ", ".join(str(item) for item in errors if str(item).strip())
|
||||||
@@ -2728,13 +2714,6 @@ def verify_recaptcha_token(secret_key, response_token, remote_ip="", expected_ho
|
|||||||
return True, ""
|
return True, ""
|
||||||
|
|
||||||
|
|
||||||
def recaptcha_fallback_message(value):
|
|
||||||
text = str(value or "")
|
|
||||||
if not text.startswith(RECAPTCHA_FALLBACK_MARKER):
|
|
||||||
return ""
|
|
||||||
return text[len(RECAPTCHA_FALLBACK_MARKER):].strip()
|
|
||||||
|
|
||||||
|
|
||||||
def login_recaptcha_script(auth=None):
|
def login_recaptcha_script(auth=None):
|
||||||
state = effective_captcha_state(auth)
|
state = effective_captcha_state(auth)
|
||||||
if state.get("effective_mode") != CAPTCHA_MODE_RECAPTCHA:
|
if state.get("effective_mode") != CAPTCHA_MODE_RECAPTCHA:
|
||||||
@@ -12974,13 +12953,6 @@ if (passwordLoginForm) {{
|
|||||||
const bodyText = await response.text();
|
const bodyText = await response.text();
|
||||||
const loginError = extractLoginError(bodyText);
|
const loginError = extractLoginError(bodyText);
|
||||||
if (!response.ok) {{
|
if (!response.ok) {{
|
||||||
const switchedToDigitsCaptcha = bodyText.includes('id="hubCaptchaCode"') && bodyText.includes('name="captcha_token"');
|
|
||||||
if (switchedToDigitsCaptcha) {{
|
|
||||||
document.open();
|
|
||||||
document.write(bodyText);
|
|
||||||
document.close();
|
|
||||||
return;
|
|
||||||
}}
|
|
||||||
setLoginRuntimeStatus(
|
setLoginRuntimeStatus(
|
||||||
loginError || `Вход не завершился. Сервер вернул HTTP ${{response.status}}.`,
|
loginError || `Вход не завершился. Сервер вернул HTTP ${{response.status}}.`,
|
||||||
'bad',
|
'bad',
|
||||||
@@ -13313,11 +13285,9 @@ document.addEventListener('click', async (event) => {
|
|||||||
_base_login_html = login_html
|
_base_login_html = login_html
|
||||||
|
|
||||||
|
|
||||||
def login_html(error="", force_local_captcha=False):
|
def login_html(error=""):
|
||||||
auth = normalize_auth_state(load_auth())
|
auth = normalize_auth_state(load_auth())
|
||||||
page = _base_login_html(error)
|
page = _base_login_html(error)
|
||||||
if force_local_captcha:
|
|
||||||
return page
|
|
||||||
state = effective_captcha_state(auth)
|
state = effective_captcha_state(auth)
|
||||||
if state.get("effective_mode") != CAPTCHA_MODE_RECAPTCHA:
|
if state.get("effective_mode") != CAPTCHA_MODE_RECAPTCHA:
|
||||||
return page
|
return page
|
||||||
@@ -14081,22 +14051,14 @@ a{{display:inline-flex;margin-top:18px;color:#93c5fd}}
|
|||||||
otp = payload.get("otp", "")
|
otp = payload.get("otp", "")
|
||||||
captcha_ok, captcha_error = self.verify_login_captcha(payload)
|
captcha_ok, captcha_error = self.verify_login_captcha(payload)
|
||||||
if not captcha_ok:
|
if not captcha_ok:
|
||||||
fallback_message = recaptcha_fallback_message(captcha_error)
|
self.send_bytes(401, login_html(captcha_error or "Неверная капча").encode("utf-8"), "text/html; charset=utf-8")
|
||||||
self.send_bytes(
|
|
||||||
401,
|
|
||||||
login_html(
|
|
||||||
fallback_message or captcha_error or "Неверная капча",
|
|
||||||
force_local_captcha=bool(fallback_message),
|
|
||||||
).encode("utf-8"),
|
|
||||||
"text/html; charset=utf-8",
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
ok, error_text, auth, auth_method = verify_password_login(username, password, otp)
|
ok, error_text, auth, auth_method = verify_password_login(username, password, otp)
|
||||||
if ok:
|
if ok:
|
||||||
token, _ = self.create_login_session((auth or {}).get("username", username), auth_method)
|
token, _ = self.create_login_session((auth or {}).get("username", username), auth_method)
|
||||||
self.redirect("/", [("Set-Cookie", self.session_cookie(token))])
|
self.redirect("/", [("Set-Cookie", self.session_cookie(token))])
|
||||||
return
|
return
|
||||||
self.send_bytes(401, login_html(error_text or "Неверный логин или пароль").encode("utf-8"), "text/html; charset=utf-8")
|
self.send_bytes(401, login_html(error_text or "Неверный логин или пароль").encode("utf-8"), "text/html; charset=utf-8")
|
||||||
|
|
||||||
def update_auth(self):
|
def update_auth(self):
|
||||||
payload = self.read_payload()
|
payload = self.read_payload()
|
||||||
|
|||||||
Reference in new issue
Block a user