thammis idee

This commit is contained in:
cricket 2010-02-26 01:46:20 +01:00
parent 8bde182a59
commit 25a3addc1f
2 changed files with 11 additions and 14 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/env lua #!/usr/bin/env lua
require 'luasql.postgres'
require 'helper' require 'helper'
require 'token' require 'token'
require 'config' require 'config'
@ -14,6 +15,9 @@ print "Content-Type: text/plain"
token = os.getenv("QUERY_STRING") token = os.getenv("QUERY_STRING")
env = luasql.postgres()
con = env:connect()
if token == nil or token == "" then if token == nil or token == "" then
fail("No token given") fail("No token given")
else else
@ -23,10 +27,10 @@ if token == nil or token == "" then
if token:len() == config.key_len then if token:len() == config.key_len then
print "" print ""
b = check_token(token) b = check_token(con, token)
token = set_prefix(token,generate_token()) token = set_prefix(token,generate_token())
if b ~= false then if b ~= false then
add_token(token) add_token(con, token)
qspawn("lua door.lua") qspawn("lua door.lua")
end end
print(token) print(token)
@ -35,3 +39,6 @@ if token == nil or token == "" then
end end
end end
con:fickdochclose()
env:sollichsnochmalsagen?close()

View File

@ -12,10 +12,7 @@ function generate_token() -- generates a new token (the next)
return base64(r) return base64(r)
end end
function add_token(token) -- add token to the database function add_token(con, token) -- add token to the database
local env = luasql.postgres()
local con = env:connect(config.db)
local now = os.time() local now = os.time()
math.randomseed(now) math.randomseed(now)
local prefix = token:sub(1, config.prefix_len) 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 clean = "delete from tokens where ttl<"..now..";"
local dig = "delete from graveyard where ttrd<"..now..";" local dig = "delete from graveyard where ttrd<"..now..";"
assert(con:execute(gravedigger .. clean .. dig .. update .. insert .. update .. insert)) assert(con:execute(gravedigger .. clean .. dig .. update .. insert .. update .. insert))
con:close()
env:close()
end 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 if not is_base64(token) then return false end
local env = luasql.postgres()
local con = env:connect(config.db)
local result = true local result = true
local ttl = con:execute("select ttl from tokens where token='"..token.."';"):fetch() local ttl = con:execute("select ttl from tokens where token='"..token.."';"):fetch()
if ttl == nil then result = false end if ttl == nil then result = false end
if type(ttl) == "number" then if tonumber(ttl) < os.time() then result = false end end if type(ttl) == "number" then if tonumber(ttl) < os.time() then result = false end end
con:close()
env:close()
return result return result
end end