mirror of
https://github.com/readest/readest.git
synced 2026-11-29 19:02:28 +00:00
Some checks are pending
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
CodeQL Advanced / Analyze (rust) (push) Waiting to run
Publish Docker image / build (linux/amd64, ubuntu-latest) (push) Waiting to run
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm) (push) Waiting to run
Publish Docker image / merge (push) Blocked by required conditions
Build with Nix / tests (push) Waiting to run
PR checks / test_web_app (2) (push) Waiting to run
PR checks / test_extensions (push) Waiting to run
PR checks / nix_flake_check (push) Waiting to run
PR checks / rust_lint (push) Waiting to run
PR checks / koplugin_rust_lint (push) Waiting to run
PR checks / build_web_app (push) Waiting to run
PR checks / test_web_app (1) (push) Waiting to run
PR checks / build_tauri_app (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Deploy to vercel on merge / build_and_deploy (push) Waiting to run
* feat(koplugin): sign in to Readest with a QR code (#7186) Readest > Log in Readest Account now offers "Sign in with a code", the device-code flow the CrossPoint plugin uses. KOReader shows a QR code, the /link URL and a code. A phone signed in to Readest scans it and approves the code on /link, and KOReader receives an ordinary Readest session that refreshes as a password login does. Nothing is typed on the e-reader, and accounts that sign in with Google or Apple, which have no password, can now use the plugin. - /api/crosspoint/device/token: with grant "session", an approved code returns the user's Supabase session instead of a CrossPoint key. The session comes from an admin magic link verified on the server; the link is never sent. If the session can't be started, the approval is put back for the next poll, as for keys. - Plugin: requestDeviceCode/pollDeviceToken in the sync API spec. Polling stops on approval, on expiry, or when the code message is dismissed. A network failure keeps it polling. The new session is written to disk at once, like a refreshed one. - /link says "Link a Reader", since KOReader uses it too. Verified: - crosspoint-routes.test.ts "signs KOReader in with a session of its own once the code is approved" fails on main (expected 500 to be 200) and passes. syncauth_spec.lua "SyncAuth:loginWithCode" (4 specs) covers approval, expiry, network failure and dismissal. - pnpm test: 1215 files, 14467 tests passed. pnpm lint: clean (tsc, Biome). pnpm test:lua: 495 successes. pnpm lint:lua: clean. - Kindle (KOReader), plugin built from this branch against a local dev server: Log in > Sign in with a code showed the QR code and code. The code was approved on web.readest.com/link, the next poll returned 200, the menu read "Log out as <user>", and opening Readest library synced (runCloudSync[both] done: success=true, 90 books pulled). After restarting KOReader it was still signed in. - The e2e run found two bugs, fixed here: the closed menu was refreshed after sign-in and came back under the next tap (it toggled Auto sync), and the session was lost on an exit that skipped saving. - Not run: Android KOReader, and the production host (the QR there encodes web.readest.com/link?code=…; the dev server's encoded 0.0.0.0:3010). Reported by misc-tw on GitHub. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(koplugin): keep showing the account while a refresh token can renew the session (#7186) Tools > Readest offered "Log in Readest Account" as soon as the access token expired, although the saved refresh token renews it on the next sync. A night in standby outlasts the token, so the plugin looked logged out every morning and the menu asked for the password again. needsLogin now asks for a login only while authentication is paused or no refresh token is saved. Verified: - New spec "SyncAuth:needsLogin stays signed in when only the access token expired (#7186)" fails on main (needsLogin returned true for a token expired 8 h ago) and passes with the fix. - pnpm test:lua: 493 successes, 0 failures. pnpm lint:lua: clean. - Kindle (KOReader, plugin 0.12.16) with an access token expired 8.5 days earlier and its refresh token saved: Tools > Readest showed "Log in Readest Account". With the fixed readest_syncauth.lua pushed and KOReader restarted, it showed "Log out as <user>"; Pull stats now then renewed the token (expires_at moved to now + 60457 s) with no login. - Not run: the reporter's BOOX Go 7 Color (Android). Reported by misc-tw on GitHub. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(koplugin): log out only this device, like the Readest app Readest > Log out called Supabase /logout without a scope. Supabase's default scope is global, so logging out of KOReader also ended every other session of the account (app, web, other readers) at their next token refresh. The app signs out with scope "local" (#6709); the plugin now sends the same. Verified: - New spec "SupabaseAuthClient:sign_out signs out only this device" fails on main (no params sent; expected { scope = "local" }) and passes with the fix. - The request KOReader's Spore library builds from the spec: main POST .../auth/v1/logout fix POST .../auth/v1/logout?scope=local - Kindle (KOReader): signed in, opened a second session of the same account from a shell, then Log out in the plugin. The menu went back to "Log in Readest Account" and the second session still refreshed (HTTP 200); it was then signed out with scope=local. - pnpm test:lua: 492 successes, 0 failures. pnpm lint:lua: clean. - Not run: the global-scope logout on main against a live account, to avoid signing the account out everywhere. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
213 lines
7.6 KiB
Lua
213 lines
7.6 KiB
Lua
local UIManager = require("ui/uimanager")
|
|
local logger = require("logger")
|
|
local socketutil = require("socketutil")
|
|
local sporeResponse = require("readest_sporeresponse")
|
|
|
|
-- Auth operation timeouts
|
|
local AUTH_TIMEOUTS = { 5, 10 }
|
|
-- Token refresh timeouts
|
|
local REFRESH_TIMEOUTS = { 3, 7 }
|
|
-- Hard limit for a token refresh. UI waiters are released much earlier; this
|
|
-- only ends a request that will never finish (e.g. a stalled DNS lookup), so
|
|
-- a later refresh can start. A rotation lost past it requires a new login.
|
|
local REFRESH_DEADLINE = 150
|
|
|
|
local SupabaseAuthClient = {
|
|
service_spec = nil,
|
|
api_key = nil,
|
|
REFRESH_DEADLINE = REFRESH_DEADLINE,
|
|
}
|
|
|
|
-- Normalize the outcome of a Spore call so every endpoint hands callers a
|
|
-- table with a message, including Spore's wrapped HTTP exceptions.
|
|
local function unwrap(name, expected_status, ok, res)
|
|
local response, reason = sporeResponse(res)
|
|
local status = response and response.status
|
|
local body = response and response.body
|
|
if ok and status == expected_status then return true, body or {}, status end
|
|
logger.dbg("SupabaseAuthClient:" .. name .. " failure:", reason or status or tostring(res))
|
|
if type(body) ~= "table" then
|
|
body = { msg = type(body) == "string" and body ~= "" and body
|
|
or reason or (status and ("HTTP " .. status)) or tostring(res) }
|
|
elseif not body.msg then
|
|
body.msg = body.message or body.error_description or body.error
|
|
or reason or (status and ("HTTP " .. status)) or "request failed"
|
|
end
|
|
return false, body, status
|
|
end
|
|
|
|
function SupabaseAuthClient:new(o)
|
|
if o == nil then o = {} end
|
|
setmetatable(o, self)
|
|
self.__index = self
|
|
if o.init then o:init() end
|
|
return o
|
|
end
|
|
|
|
function SupabaseAuthClient:init()
|
|
local Spore = require("Spore")
|
|
self.client = Spore.new_from_spec(self.service_spec)
|
|
|
|
-- Supabase API headers middleware
|
|
package.loaded["Spore.Middleware.SupabaseHeaders"] = {}
|
|
require("Spore.Middleware.SupabaseHeaders").call = function(args, req)
|
|
req.headers["apikey"] = args.api_key
|
|
req.headers["content-type"] = "application/json"
|
|
req.headers["accept"] = "application/json"
|
|
end
|
|
|
|
-- Supabase Bearer token auth middleware
|
|
package.loaded["Spore.Middleware.SupabaseAuth"] = {}
|
|
require("Spore.Middleware.SupabaseAuth").call = function(args, req)
|
|
if args.access_token then
|
|
req.headers["authorization"] = "Bearer " .. args.access_token
|
|
end
|
|
end
|
|
|
|
package.loaded["Spore.Middleware.AsyncHTTP"] = require("readest_asynchttp")
|
|
|
|
end
|
|
|
|
function SupabaseAuthClient:sign_in_password(email, password)
|
|
self.client:reset_middlewares()
|
|
self.client:enable("Format.JSON")
|
|
self.client:enable("SupabaseHeaders", {
|
|
api_key = self.api_key,
|
|
})
|
|
socketutil:set_timeout(AUTH_TIMEOUTS[1], AUTH_TIMEOUTS[2])
|
|
local ok, res = pcall(function()
|
|
return self.client:sign_in_password({
|
|
email = email,
|
|
password = password,
|
|
})
|
|
end)
|
|
socketutil:reset_timeout()
|
|
return unwrap("sign_in_password", 200, ok, res)
|
|
end
|
|
|
|
function SupabaseAuthClient:sign_in_otp(email, callback)
|
|
self.client:reset_middlewares()
|
|
self.client:enable("Format.JSON")
|
|
self.client:enable("SupabaseHeaders", {
|
|
api_key = self.api_key,
|
|
})
|
|
socketutil:set_timeout(AUTH_TIMEOUTS[1], AUTH_TIMEOUTS[2])
|
|
local co = coroutine.create(function()
|
|
local ok, res = pcall(function()
|
|
return self.client:sign_in_otp({
|
|
email = email,
|
|
})
|
|
end)
|
|
callback(unwrap("sign_in_otp", 200, ok, res))
|
|
end)
|
|
self.client:enable("AsyncHTTP", {thread = co, timeout = 15})
|
|
coroutine.resume(co)
|
|
if UIManager.looper then UIManager:setInputTimeout() end
|
|
socketutil:reset_timeout()
|
|
end
|
|
|
|
function SupabaseAuthClient:verify_otp(email, token, type)
|
|
self.client:reset_middlewares()
|
|
self.client:enable("Format.JSON")
|
|
self.client:enable("SupabaseHeaders", {
|
|
api_key = self.api_key,
|
|
})
|
|
socketutil:set_timeout(AUTH_TIMEOUTS[1], AUTH_TIMEOUTS[2])
|
|
local ok, res = pcall(function()
|
|
return self.client:verify_otp({
|
|
email = email,
|
|
token = token,
|
|
type = type or "email",
|
|
})
|
|
end)
|
|
socketutil:reset_timeout()
|
|
return unwrap("verify_otp", 200, ok, res)
|
|
end
|
|
|
|
function SupabaseAuthClient:refresh_token(refresh_token, callback)
|
|
-- DNS stalls must not block reader input. Keep the worker alive past the
|
|
-- caller's wait so a successful rotation can still be persisted.
|
|
if not UIManager.looper and require("ffi/util").runInSubProcess then
|
|
local worker = {
|
|
client = self.client,
|
|
_prepare = function()
|
|
self.client:reset_middlewares()
|
|
self.client:enable("Format.JSON")
|
|
self.client:enable("SupabaseHeaders", { api_key = self.api_key })
|
|
end,
|
|
}
|
|
require("readest_syncclient")._dispatchInSubprocess(worker, "refresh_token",
|
|
{ refresh_token = refresh_token }, REFRESH_TIMEOUTS,
|
|
function(ok, res) callback(unwrap("refresh_token", 200, ok, res)) end,
|
|
{ deadline = REFRESH_DEADLINE })
|
|
return
|
|
end
|
|
self.client:reset_middlewares()
|
|
self.client:enable("Format.JSON")
|
|
self.client:enable("SupabaseHeaders", {
|
|
api_key = self.api_key,
|
|
})
|
|
socketutil:set_timeout(REFRESH_TIMEOUTS[1], REFRESH_TIMEOUTS[2])
|
|
local co = coroutine.create(function()
|
|
local ok, res = pcall(function()
|
|
return self.client:refresh_token({
|
|
refresh_token = refresh_token,
|
|
})
|
|
end)
|
|
callback(unwrap("refresh_token", 200, ok, res))
|
|
end)
|
|
-- The coordinator's UI wait ends sooner; a late HTTP response must still
|
|
-- resume this coroutine unless the request outlives the hard limit.
|
|
self.client:enable("AsyncHTTP", {thread = co, timeout = REFRESH_DEADLINE})
|
|
coroutine.resume(co)
|
|
if UIManager.looper then UIManager:setInputTimeout() end
|
|
socketutil:reset_timeout()
|
|
end
|
|
|
|
function SupabaseAuthClient:sign_out(access_token, callback)
|
|
self.client:reset_middlewares()
|
|
self.client:enable("Format.JSON")
|
|
self.client:enable("SupabaseHeaders", {
|
|
api_key = self.api_key,
|
|
})
|
|
self.client:enable("SupabaseAuth", {
|
|
access_token = access_token,
|
|
})
|
|
socketutil:set_timeout(AUTH_TIMEOUTS[1], AUTH_TIMEOUTS[2])
|
|
local co = coroutine.create(function()
|
|
local ok, res = pcall(function()
|
|
-- Only this device, like the app: Supabase's default scope
|
|
-- (global) signs the account out everywhere.
|
|
return self.client:sign_out({ scope = "local" })
|
|
end)
|
|
callback(unwrap("sign_out", 204, ok, res))
|
|
end)
|
|
self.client:enable("AsyncHTTP", {thread = co, timeout = 15})
|
|
coroutine.resume(co)
|
|
if UIManager.looper then UIManager:setInputTimeout() end
|
|
socketutil:reset_timeout()
|
|
end
|
|
|
|
function SupabaseAuthClient:get_user(access_token, callback)
|
|
self.client:reset_middlewares()
|
|
self.client:enable("Format.JSON")
|
|
self.client:enable("SupabaseHeaders", {
|
|
api_key = self.api_key,
|
|
})
|
|
self.client:enable("SupabaseAuth", {
|
|
access_token = access_token,
|
|
})
|
|
socketutil:set_timeout(AUTH_TIMEOUTS[1], AUTH_TIMEOUTS[2])
|
|
local co = coroutine.create(function()
|
|
local ok, res = pcall(function()
|
|
return self.client:get_user()
|
|
end)
|
|
callback(unwrap("get_user", 200, ok, res))
|
|
end)
|
|
self.client:enable("AsyncHTTP", {thread = co, timeout = 15})
|
|
coroutine.resume(co)
|
|
if UIManager.looper then UIManager:setInputTimeout() end
|
|
socketutil:reset_timeout()
|
|
end
|
|
|
|
return SupabaseAuthClient
|