fix(scrapers): login fast paths and re-auth hardening for httpx platforms (#357)

## Problem

On CSES, Kattis, and USACO, `:CP <platform> login` always prompted
for credentials and ran a full web login even when a valid session was
already cached. Submit also had weak stale-session detection.

## Solution

`credentials.lua` now tries cached credentials first before prompting,
delegating fast-path detection to each scraper. CSES `login()` checks
the cached API token and returns immediately if valid. USACO `login()`
and `submit()` call `_check_usaco_login()` upfront. Kattis `submit()`
emits `checking_login` consistently and also triggers re-auth on HTTP
400/403, not just on the `"Request validation failed"` text match.
The premature `Submitting...` log emitted by Lua before the scraper
started is removed — Python's own status events are sufficient.
This commit is contained in:
Barrett Ruth 2026-03-07 02:23:43 -05:00 committed by GitHub
parent 6e9829a115
commit eb0dea777e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 88 additions and 48 deletions

View file

@ -11,19 +11,9 @@ local STATUS_MESSAGES = {
installing_browser = 'Installing browser...',
}
---@param platform string?
function M.login(platform)
platform = platform or state.get_platform()
if not platform then
logger.log(
'No platform specified. Usage: :CP <platform> login',
{ level = vim.log.levels.ERROR }
)
return
end
local display = constants.PLATFORM_DISPLAY_NAMES[platform] or platform
---@param platform string
---@param display string
local function prompt_and_login(platform, display)
vim.ui.input({ prompt = display .. ' username: ' }, function(username)
if not username or username == '' then
logger.log('Cancelled', { level = vim.log.levels.WARN })
@ -37,15 +27,7 @@ function M.login(platform)
return
end
cache.load()
local existing = cache.get_credentials(platform) or {}
local credentials = {
username = username,
password = password,
}
if existing.token then
credentials.token = existing.token
end
local credentials = { username = username, password = password }
local scraper = require('cp.scraper')
scraper.login(platform, credentials, function(ev)
@ -69,6 +51,47 @@ function M.login(platform)
end)
end
---@param platform string?
function M.login(platform)
platform = platform or state.get_platform()
if not platform then
logger.log(
'No platform specified. Usage: :CP <platform> login',
{ level = vim.log.levels.ERROR }
)
return
end
local display = constants.PLATFORM_DISPLAY_NAMES[platform] or platform
cache.load()
local existing = cache.get_credentials(platform) or {}
if existing.username and existing.password then
local scraper = require('cp.scraper')
scraper.login(platform, existing, function(ev)
vim.schedule(function()
local msg = STATUS_MESSAGES[ev.status] or ev.status
logger.log(display .. ': ' .. msg, { level = vim.log.levels.INFO, override = true })
end)
end, function(result)
vim.schedule(function()
if result.success then
logger.log(
display .. ' login successful',
{ level = vim.log.levels.INFO, override = true }
)
else
prompt_and_login(platform, display)
end
end)
end)
return
end
prompt_and_login(platform, display)
end
---@param platform string?
function M.logout(platform)
platform = platform or state.get_platform()