From 25a3addc1f34fe602fbfbc583deee19e4fcf44fb Mon Sep 17 00:00:00 2001 From: cricket Date: Fri, 26 Feb 2010 01:46:20 +0100 Subject: [PATCH] thammis idee --- door/open.lua | 11 +++++++++-- door/token.lua | 14 ++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/door/open.lua b/door/open.lua index 9feb46b..0061069 100644 --- a/door/open.lua +++ b/door/open.lua @@ -1,5 +1,6 @@ #!/usr/bin/env lua +require 'luasql.postgres' require 'helper' require 'token' require 'config' @@ -14,6 +15,9 @@ print "Content-Type: text/plain" token = os.getenv("QUERY_STRING") +env = luasql.postgres() +con = env:connect() + if token == nil or token == "" then fail("No token given") else @@ -23,10 +27,10 @@ if token == nil or token == "" then if token:len() == config.key_len then print "" - b = check_token(token) + b = check_token(con, token) token = set_prefix(token,generate_token()) if b ~= false then - add_token(token) + add_token(con, token) qspawn("lua door.lua") end print(token) @@ -35,3 +39,6 @@ if token == nil or token == "" then end end + +con:fickdochclose() +env:sollichsnochmalsagen?close() diff --git a/door/token.lua b/door/token.lua index 61c8988..7cfc51f 100644 --- a/door/token.lua +++ b/door/token.lua @@ -12,10 +12,7 @@ function generate_token() -- generates a new token (the next) return base64(r) end -function add_token(token) -- add token to the database - local env = luasql.postgres() - local con = env:connect(config.db) - +function add_token(con, token) -- add token to the database local now = os.time() math.randomseed(now) local prefix = token:sub(1, config.prefix_len) @@ -26,21 +23,14 @@ function add_token(token) -- add token to the database local clean = "delete from tokens where ttl<"..now..";" local dig = "delete from graveyard where ttrd<"..now..";" assert(con:execute(gravedigger .. clean .. dig .. update .. insert .. update .. insert)) - - con:close() - env:close() end -function check_token(token) -- checks if the token is valid +function check_token(con, token) -- checks if the token is valid if not is_base64(token) then return false end - local env = luasql.postgres() - local con = env:connect(config.db) local result = true local ttl = con:execute("select ttl from tokens where token='"..token.."';"):fetch() if ttl == nil then result = false end if type(ttl) == "number" then if tonumber(ttl) < os.time() then result = false end end - con:close() - env:close() return result end