use request uri and regex to choose callbacks
This commit is contained in:
parent
766b1d2aff
commit
eefdecb1e9
42
main.lua
42
main.lua
|
@ -12,33 +12,45 @@ function log(msg)
|
|||
end
|
||||
|
||||
local WANTED_SCRIPTS = {
|
||||
'scripts.webfinger_allowlist'
|
||||
['scripts.webfinger_allowlist'] = {accounts = {"example@example.com"}}
|
||||
}
|
||||
|
||||
local compiled_chain = {}
|
||||
|
||||
for _, module_name in pairs(WANTED_SCRIPTS) do
|
||||
log('load module', module_name)
|
||||
mod = require(module_name)
|
||||
log('load module', mod)
|
||||
table.insert(compiled_chain, mod)
|
||||
for module_name, module_config in pairs(WANTED_SCRIPTS) do
|
||||
module = require(module_name)
|
||||
table.insert(compiled_chain, {module, module_config})
|
||||
end
|
||||
|
||||
local function onRequest()
|
||||
local request_uri = ngx.var.uri
|
||||
|
||||
-- find out which modules to call based on their regexes
|
||||
local callbacks_to_call = {}
|
||||
for _, filter in ipairs(compiled_chain) do
|
||||
local module, module_config = filter[1], filter[2]
|
||||
|
||||
for callback_regex, callback_function in pairs(module.callbacks) do
|
||||
local match, error = ngx.re.match(request_uri, callback_regex)
|
||||
if match then
|
||||
table.insert(callbacks_to_call, {callback_function, module_config})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
log('AWOOOOGA')
|
||||
|
||||
for _,mod in ipairs(compiled_chain) do
|
||||
log(mod)
|
||||
local mod_config = {accounts = {"a@a.com"}}
|
||||
local result, body = mod.callback(mod_config)
|
||||
log(result)
|
||||
log(body)
|
||||
if not result then
|
||||
for _,tuple in ipairs(callbacks_to_call) do
|
||||
local callback_function, config = tuple[1], tuple[2]
|
||||
local result, body = callback_function(config)
|
||||
log(result)
|
||||
log(body)
|
||||
if not result then
|
||||
ngx.status = 400
|
||||
ngx.say(body or "request denied")
|
||||
ngx.exit(400)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return onRequest
|
||||
|
|
|
@ -6,6 +6,11 @@ function webfingerCallback(cfg)
|
|||
|
||||
local resource = args['resource']
|
||||
if resource ~= nil then
|
||||
-- TODO this is O(n) but we can make it O(1) by doing a funny and making
|
||||
-- cfg.accounts be transformed into keys in a table
|
||||
--
|
||||
-- this would require us to do some setup() callback as well as
|
||||
-- the request() callback
|
||||
for _, account in ipairs(cfg.accounts) do
|
||||
if resource == account then
|
||||
return true
|
||||
|
@ -27,7 +32,6 @@ return {
|
|||
Useful for small instances.
|
||||
]],
|
||||
apiVersion=1,
|
||||
callback=webfingerCallback,
|
||||
callbacks = {
|
||||
['/.well-known/webfinger'] = webfingerCallback
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue