177 lines
5.5 KiB
Lua
177 lines
5.5 KiB
Lua
--[[--
|
|
拷贝漫画账号会话:按所选站点的 API 登录,持久化 Token 与账号密码。
|
|
|
|
密码要落盘:令牌被挤掉(HTTP 401)时 `client` 用本地账号自动重登;
|
|
只存 Token 会在过期后只能弹「重新登录」,整书缓存也会跟着挂。
|
|
|
|
登录规则与 kComics / 官方 App 一致:
|
|
salt = 6 位随机整数
|
|
password = base64(明文密码 .. "-" .. salt)
|
|
POST /api/v3/login (x-www-form-urlencoded)
|
|
|
|
@module koplugin.book.moonsource_copymanga.auth
|
|
--]]
|
|
|
|
local Client = require("moonsource_copymanga.client")
|
|
local Text = require("utils.text")
|
|
local _ = require("l10n").get("moonsource_copymanga")
|
|
|
|
local Auth = {}
|
|
local SOURCE_ID = "copymanga"
|
|
|
|
---@return table
|
|
local function cfg()
|
|
return require("utils.settings").getSource(SOURCE_ID)
|
|
end
|
|
|
|
---@param patch table
|
|
local function save(patch)
|
|
local settings = require("utils.settings")
|
|
local current = settings.getSource(SOURCE_ID)
|
|
for key, value in pairs(patch) do
|
|
current[key] = value
|
|
end
|
|
settings.saveSource(SOURCE_ID, current)
|
|
end
|
|
|
|
--- 站点前端同款密码编码。
|
|
---@param password string
|
|
---@param salt string|number
|
|
---@return string
|
|
function Auth.encodePassword(password, salt)
|
|
return Text.base64Encode(tostring(password) .. "-" .. tostring(salt))
|
|
end
|
|
|
|
---@return string
|
|
local function newSalt()
|
|
return tostring(math.random(100000, 999999))
|
|
end
|
|
|
|
---@return boolean
|
|
function Auth.hasSession()
|
|
local token = cfg().token
|
|
return type(token) == "string" and token ~= ""
|
|
end
|
|
|
|
---@return string|nil
|
|
function Auth.userLabel()
|
|
local current = cfg()
|
|
if type(current.nickname) == "string" and current.nickname ~= "" then
|
|
return current.nickname
|
|
end
|
|
if type(current.username) == "string" and current.username ~= "" then
|
|
return current.username
|
|
end
|
|
return nil
|
|
end
|
|
|
|
--- 本地保存的账号密码;缺任一端则没有可自动重登的凭据。
|
|
---@return string|nil, string|nil
|
|
function Auth.credentials()
|
|
local current = cfg()
|
|
local username = current.username
|
|
local password = current.password
|
|
if type(username) == "string" and username ~= ""
|
|
and type(password) == "string" and password ~= ""
|
|
then
|
|
return username, password
|
|
end
|
|
return nil
|
|
end
|
|
|
|
--- 清除登录态,保留站点地址。
|
|
function Auth.clearSession()
|
|
save({
|
|
token = "",
|
|
user_id = "",
|
|
username = "",
|
|
nickname = "",
|
|
password = "",
|
|
})
|
|
end
|
|
|
|
--- 账号密码登录;成功后写入 token / 用户信息。
|
|
---@param username string
|
|
---@param password string
|
|
---@param cb fun(ok: boolean|nil, err: string|nil, rejected: boolean|nil) rejected = 服务端拒绝(非网络错误)
|
|
---@return { cancel: fun() }
|
|
function Auth.loginAsync(username, password, cb)
|
|
username = Text.stripWhitespace(username)
|
|
password = tostring(password or "")
|
|
if username == "" or password == "" then
|
|
return require("source.base").defer(cb, nil, _("请输入账号和密码"))
|
|
end
|
|
|
|
local current = cfg()
|
|
local base_url = Client.normalizeBaseUrl(current.base_url)
|
|
local old_token, old_user_id = current.token, current.user_id
|
|
local cancelled = false
|
|
local salt = newSalt()
|
|
local body = Text.formEncode({
|
|
username = username,
|
|
password = Auth.encodePassword(password, salt),
|
|
salt = salt,
|
|
})
|
|
local headers = Client.headers()
|
|
headers["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
|
|
|
|
local request = require("http.request").request({
|
|
url = base_url .. "/api/v3/login",
|
|
method = "POST",
|
|
body = body,
|
|
headers = headers,
|
|
cache_ttl = 0,
|
|
allow_redirects = false,
|
|
timeout = require("http.timeout").net(1),
|
|
connect_timeout = require("http.timeout").net(1),
|
|
}, function(res, err)
|
|
if cancelled then return end
|
|
local wire, decode_err = Client.decode(res and res.body, err)
|
|
if not wire then
|
|
-- 第三个参数:服务端回了包并拒绝(密码错 / 风控),区别于网络错误。
|
|
cb(nil, decode_err, err == nil and res ~= nil)
|
|
return
|
|
end
|
|
local results = type(wire.results) == "table" and wire.results or {}
|
|
local token = results.token
|
|
if type(token) ~= "string" or token == "" then
|
|
cb(nil, _("登录失败:未返回令牌"), true)
|
|
return
|
|
end
|
|
if cfg().token ~= old_token or Client.normalizeBaseUrl(cfg().base_url) ~= base_url then
|
|
cb(nil, _("登录状态已改变,请重试"))
|
|
return
|
|
end
|
|
local user_id = tostring(results.user_id or "")
|
|
if user_id == "" then
|
|
cb(nil, _("登录失败:未返回账号标识"), true)
|
|
return
|
|
end
|
|
local function commit()
|
|
save({
|
|
token = token,
|
|
user_id = user_id,
|
|
username = tostring(results.username or username),
|
|
nickname = tostring(results.nickname or results.username or username),
|
|
-- 明文密码供 401 自动重登(#42);退出登录时 clearSession 清掉。
|
|
password = password,
|
|
})
|
|
end
|
|
if old_user_id ~= user_id then
|
|
if not require("source.logout").replaceLibrary(SOURCE_ID, commit) then
|
|
cb(nil, _("准备本地同步失败"))
|
|
return
|
|
end
|
|
else
|
|
commit()
|
|
end
|
|
cb(true)
|
|
end)
|
|
return { cancel = function()
|
|
cancelled = true
|
|
request.cancel()
|
|
end }
|
|
end
|
|
|
|
return Auth
|