2018-09-12 18:31:25 +02:00
|
|
|
require("sqlite")
|
2021-10-04 22:10:14 +02:00
|
|
|
local TUNNEL_KEYCHAIN = "/opt/www/tmp/channels/antunnel_keychain"
|
2018-09-12 18:31:25 +02:00
|
|
|
function fail(msg)
|
2021-05-06 20:39:10 +02:00
|
|
|
std.custom_header("Connection","close")
|
2018-09-12 18:31:25 +02:00
|
|
|
std.json()
|
|
|
|
std.t(JSON.encode({error=msg}))
|
|
|
|
end
|
|
|
|
|
|
|
|
function result(obj)
|
2021-05-06 20:39:10 +02:00
|
|
|
std.custom_header("Connection","close")
|
2018-09-12 18:31:25 +02:00
|
|
|
std.json()
|
|
|
|
std.t(JSON.encode({result=obj, error=false}))
|
|
|
|
end
|
|
|
|
|
|
|
|
function die (msg)
|
|
|
|
fail(msg)
|
|
|
|
debug.traceback=nil
|
|
|
|
error("Permission denied")
|
2020-06-26 15:20:17 +02:00
|
|
|
return false
|
2018-09-12 18:31:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- check if the sysdb is create, otherwise create the table
|
|
|
|
function sysdb()
|
|
|
|
local meta = {}
|
|
|
|
meta.sessionid = ""
|
|
|
|
meta.username = ""
|
|
|
|
meta.stamp = 0
|
|
|
|
return require("dbmodel").get("sysdb", "sessions", meta)
|
|
|
|
end
|
|
|
|
|
|
|
|
function is_auth()
|
2021-02-09 01:30:08 +01:00
|
|
|
local sessionid = nil
|
|
|
|
if SESSION.sessionid and SESSION.sessionid ~= '0' then
|
|
|
|
sessionid = SESSION.sessionid
|
|
|
|
-- should be used only by API call
|
|
|
|
elseif REQUEST.sessionid and REQUEST.sessionid ~= '0' then
|
|
|
|
sessionid = REQUEST.sessionid
|
|
|
|
end
|
|
|
|
if sessionid == nil then
|
|
|
|
return false
|
|
|
|
end
|
2018-09-12 18:31:25 +02:00
|
|
|
-- query session id from database
|
|
|
|
local db = sysdb()
|
|
|
|
if db == nil then return false end
|
2021-02-09 01:30:08 +01:00
|
|
|
local cond = {exp= {["="] = { sessionid = sessionid }}}
|
2018-09-12 18:31:25 +02:00
|
|
|
local data = db:find(cond)
|
|
|
|
--print(JSON.encode(data))
|
|
|
|
db:close()
|
2020-06-26 15:20:17 +02:00
|
|
|
if data == nil or data[1] == nil then return die("No user data found") end
|
2018-09-12 18:31:25 +02:00
|
|
|
-- next time check the stamp
|
|
|
|
SESSION.user = data[1].username
|
2021-10-04 22:10:14 +02:00
|
|
|
local f = io.open(TUNNEL_KEYCHAIN, "w")
|
|
|
|
if f then
|
2021-10-27 15:42:30 +02:00
|
|
|
f:write(sessionid..SESSION.user)
|
2021-10-04 22:33:16 +02:00
|
|
|
f:close()
|
2021-10-04 22:10:14 +02:00
|
|
|
end
|
2018-09-12 18:31:25 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function auth_or_die(msg)
|
|
|
|
if(is_auth() == false) then
|
|
|
|
die(msg)
|
|
|
|
end
|
2021-10-04 22:33:16 +02:00
|
|
|
end
|