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
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()

View File

@ -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