Skip to content

Instantly share code, notes, and snippets.

@flazz
Forked from kyleondata/auth.lua
Created August 23, 2016 23:08
Show Gist options
  • Save flazz/7f9588ee0ad18c690c04ba6884ab69d4 to your computer and use it in GitHub Desktop.
Save flazz/7f9588ee0ad18c690c04ba6884ab69d4 to your computer and use it in GitHub Desktop.
local count = 0
core.register_service("auth", "http", function (applet)
count = count + 1
-- Get X-Auth-Token --
local token = applet.headers['x-auth-token'][0]
local cmd = 'curl -H "X-Auth-Token:'..token..'"'
cmd = cmd..' -sL -w "%{http_code}\\n"'
cmd = cmd..' "https://staging.identity-internal.api.rackspacecloud.com/v2.0/tokens/'..token..'"'
cmd = cmd..' -o /dev/null'
print(os.time())
local output = io.popen(cmd)
local result = output:read()
output:close()
print(os.time())
print('----')
print(count)
if (result == 200) then
applet:set_status(200)
else
applet:set_status(403)
end
local response = ""
applet:add_header("content-length", string.len(response))
applet:add_header("content-type", "text/plain")
applet:start_response()
applet:send(response)
end)
core.register_fetches("auth3", function(txn)
core.Debug('FETCHER')
local token = txn.f:hdr("x-auth-token")
if token == nil then
core.Debug('token is nil')
return false
end
core.Debug('token exists')
local identity_endpoint = 'https://staging.identity-internal.api.rackspacecloud.com/v2.0/tokens/'
local t0 = socket.gettime()
local body, code, headers = http.request(identity_endpoint .. token)
local t1 = socket.gettime()
local elapsed_time = t1 - t0 -- TODO set haproxy stat?
core.Debug('call to identity took: ' .. elapsed_time .. 'ms')
if body == nil then
core.Debug('call to identity failed: ' .. code)
return false
end
if code ~= 200 then
core.Debug('token check failed: ' .. code)
core.Debug('token check failed: ' .. body)
pp(headers)
local obj, pos, err = json.decode(body, 1, nil)
print(obj['unauthorized']['message'])
return false
end
-- TODO get json data from inside token response
-- set
txn:Debug('token ok: ' .. body)
return true
end)
global
lua-load auth.lua
listen http_in
http-request use-service lua.auth
bind *:8080
mode http
timeout connect 5000
timeout client 50000
timeout server 50000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment