fix(kattis): fix login, language ID, and submit path
Problem: Kattis login used `/login/email` without a CSRF token (always failed), the homepage login check used GET requests that Kattis blocks from httpx, the C++ language ID was `"C++17"` (rejected by the API), and `submit.lua` passed a relative source file path to Python whose cwd is the plugin directory. Solution: Switch login to `POST /login` with `script=true` (200 = success, 403 = bad credentials), remove the broken `_check_kattis_login` entirely, add a submit retry on `"Request validation failed"`, correct the Kattis cpp language ID to `"C++"`, and absolutize the source file path in `submit.lua` via `fnamemodify(..., ':p')` before spawning.
This commit is contained in:
parent
543480a4fe
commit
01fc2f26e9
3 changed files with 25 additions and 28 deletions
|
|
@ -54,6 +54,7 @@ function M.submit(opts)
|
||||||
logger.log('Source file not found', { level = vim.log.levels.ERROR })
|
logger.log('Source file not found', { level = vim.log.levels.ERROR })
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
source_file = vim.fn.fnamemodify(source_file, ':p')
|
||||||
|
|
||||||
prompt_credentials(platform, function(creds)
|
prompt_credentials(platform, function(creds)
|
||||||
vim.cmd.update()
|
vim.cmd.update()
|
||||||
|
|
|
||||||
|
|
@ -221,25 +221,17 @@ async def _save_kattis_cookies(client: httpx.AsyncClient) -> None:
|
||||||
_COOKIE_PATH.write_text(json.dumps(cookies))
|
_COOKIE_PATH.write_text(json.dumps(cookies))
|
||||||
|
|
||||||
|
|
||||||
async def _check_kattis_login(client: httpx.AsyncClient) -> bool:
|
|
||||||
try:
|
|
||||||
r = await client.get(BASE_URL + "/", headers=HEADERS, timeout=HTTP_TIMEOUT)
|
|
||||||
text = r.text.lower()
|
|
||||||
return "sign out" in text or "logout" in text or "my profile" in text
|
|
||||||
except Exception:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
async def _do_kattis_login(
|
async def _do_kattis_login(
|
||||||
client: httpx.AsyncClient, username: str, password: str
|
client: httpx.AsyncClient, username: str, password: str
|
||||||
) -> bool:
|
) -> bool:
|
||||||
|
client.cookies.clear()
|
||||||
r = await client.post(
|
r = await client.post(
|
||||||
f"{BASE_URL}/login/email",
|
f"{BASE_URL}/login",
|
||||||
data={"user": username, "password": password, "script": "true"},
|
data={"user": username, "password": password, "script": "true"},
|
||||||
headers=HEADERS,
|
headers=HEADERS,
|
||||||
timeout=HTTP_TIMEOUT,
|
timeout=HTTP_TIMEOUT,
|
||||||
)
|
)
|
||||||
return r.status_code == 200 and "login failed" not in r.text.lower()
|
return r.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
class KattisScraper(BaseScraper):
|
class KattisScraper(BaseScraper):
|
||||||
|
|
@ -330,9 +322,7 @@ class KattisScraper(BaseScraper):
|
||||||
|
|
||||||
async with httpx.AsyncClient(follow_redirects=True) as client:
|
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||||
await _load_kattis_cookies(client)
|
await _load_kattis_cookies(client)
|
||||||
print(json.dumps({"status": "checking_login"}), flush=True)
|
if not client.cookies:
|
||||||
logged_in = bool(client.cookies) and await _check_kattis_login(client)
|
|
||||||
if not logged_in:
|
|
||||||
print(json.dumps({"status": "logging_in"}), flush=True)
|
print(json.dumps({"status": "logging_in"}), flush=True)
|
||||||
ok = await _do_kattis_login(client, username, password)
|
ok = await _do_kattis_login(client, username, password)
|
||||||
if not ok:
|
if not ok:
|
||||||
|
|
@ -351,18 +341,35 @@ class KattisScraper(BaseScraper):
|
||||||
}
|
}
|
||||||
if contest_id != problem_id:
|
if contest_id != problem_id:
|
||||||
data["contest"] = contest_id
|
data["contest"] = contest_id
|
||||||
try:
|
|
||||||
r = await client.post(
|
async def _do_submit() -> httpx.Response:
|
||||||
|
return await client.post(
|
||||||
f"{BASE_URL}/submit",
|
f"{BASE_URL}/submit",
|
||||||
data=data,
|
data=data,
|
||||||
files={"sub_file[]": (f"solution.{ext}", source, "text/plain")},
|
files={"sub_file[]": (f"solution.{ext}", source, "text/plain")},
|
||||||
headers=HEADERS,
|
headers=HEADERS,
|
||||||
timeout=HTTP_TIMEOUT,
|
timeout=HTTP_TIMEOUT,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
r = await _do_submit()
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return self._submit_error(f"Submit request failed: {e}")
|
return self._submit_error(f"Submit request failed: {e}")
|
||||||
|
|
||||||
|
if r.text == "Request validation failed":
|
||||||
|
_COOKIE_PATH.unlink(missing_ok=True)
|
||||||
|
print(json.dumps({"status": "logging_in"}), flush=True)
|
||||||
|
ok = await _do_kattis_login(client, username, password)
|
||||||
|
if not ok:
|
||||||
|
return self._submit_error("Login failed (bad credentials?)")
|
||||||
|
await _save_kattis_cookies(client)
|
||||||
|
try:
|
||||||
|
r = await _do_submit()
|
||||||
|
r.raise_for_status()
|
||||||
|
except Exception as e:
|
||||||
|
return self._submit_error(f"Submit request failed: {e}")
|
||||||
|
|
||||||
sid_m = re.search(r"Submission ID:\s*(\d+)", r.text, re.IGNORECASE)
|
sid_m = re.search(r"Submission ID:\s*(\d+)", r.text, re.IGNORECASE)
|
||||||
sid = sid_m.group(1) if sid_m else ""
|
sid = sid_m.group(1) if sid_m else ""
|
||||||
return SubmitResult(
|
return SubmitResult(
|
||||||
|
|
@ -376,21 +383,10 @@ class KattisScraper(BaseScraper):
|
||||||
return self._login_error("Missing username or password")
|
return self._login_error("Missing username or password")
|
||||||
|
|
||||||
async with httpx.AsyncClient(follow_redirects=True) as client:
|
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||||
await _load_kattis_cookies(client)
|
|
||||||
if client.cookies:
|
|
||||||
print(json.dumps({"status": "checking_login"}), flush=True)
|
|
||||||
if await _check_kattis_login(client):
|
|
||||||
return LoginResult(
|
|
||||||
success=True,
|
|
||||||
error="",
|
|
||||||
credentials={"username": username, "password": password},
|
|
||||||
)
|
|
||||||
|
|
||||||
print(json.dumps({"status": "logging_in"}), flush=True)
|
print(json.dumps({"status": "logging_in"}), flush=True)
|
||||||
ok = await _do_kattis_login(client, username, password)
|
ok = await _do_kattis_login(client, username, password)
|
||||||
if not ok:
|
if not ok:
|
||||||
return self._login_error("Login failed (bad credentials?)")
|
return self._login_error("Login failed (bad credentials?)")
|
||||||
|
|
||||||
await _save_kattis_cookies(client)
|
await _save_kattis_cookies(client)
|
||||||
return LoginResult(
|
return LoginResult(
|
||||||
success=True,
|
success=True,
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ LANGUAGE_IDS = {
|
||||||
"python": "python",
|
"python": "python",
|
||||||
},
|
},
|
||||||
"kattis": {
|
"kattis": {
|
||||||
"cpp": "C++17",
|
"cpp": "C++",
|
||||||
"python": "Python 3",
|
"python": "Python 3",
|
||||||
},
|
},
|
||||||
"codechef": {
|
"codechef": {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue