Skip to content

Commit

Permalink
Merge branch 'master' of github.com:foundpatterns/lighttouch-base int…
Browse files Browse the repository at this point in the history
…o contentdb-rename
  • Loading branch information
sh-zam committed Jan 29, 2019
2 parents 50805d7 + c8c22ef commit c91b9e9
Show file tree
Hide file tree
Showing 59 changed files with 265 additions and 148 deletions.
6 changes: 3 additions & 3 deletions base.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
_log.debug("[loading] libraries")
log.trace("[loading] libraries")

math.randomseed(os.time())
lua_math.randomseed(os.time())

tera.instance = tera.new(torchbear.settings.templates_path or "templates/**/*")

Expand All @@ -20,6 +20,7 @@ if not home_store then
end
file:write(home_store)
file:close()
fs.create_dir("content/" .. home_store, true)
end
contentdb.home = home_store

Expand Down Expand Up @@ -50,5 +51,4 @@ outgoing_response_event:addAction(response_process_action)
outgoing_response_event:setActionPriority(response_process_action, 100)

log.info("[loaded] LightTouch")

events["lighttouch_loaded"]:trigger()
25 changes: 25 additions & 0 deletions helpers/get_place.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

function _G.get_place ()
--[[
-- Doesn't work for https stuff
local response = send_request({
method = "GET",
uri = "https://ifconfig.me/",
headers = {
["accept"] = "text/plain",
},
body = ""
})
local place = response.body
]]

-- Ideally, this would be a raw request, like above
local cmd = "curl ifconfig.me"
local file = assert(io.popen(cmd, 'r'))
local place = assert(file:read('*a'))
file:close()

log.info("Current place: " .. place)

return place
end
3 changes: 3 additions & 0 deletions helpers/render.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function _G.render (file, data)
return tera.instance:render(file, data)
end
15 changes: 15 additions & 0 deletions helpers/send_request.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function _G.send_request (request)

if type(request) == "string" then
request = {uri = request}
end

request.uuid = uuid.v4()
events["outgoing_request_about_to_be_sent"]:trigger({ request = request })

local response = client_request.send(request)
response.uuid = uuid.v4()
events["incoming_response_received"]:trigger({ response = response })

return response
end
32 changes: 8 additions & 24 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,14 @@

-- this config must be before requires
local address = torchbear.settings.address or "localhost"
local host = torchbear.settings.host or "3000"
_log.info("starting web server on " .. address .. ":" .. host)
local port = torchbear.settings.port or "3000"
package.path = package.path..";lighttouch-base/?.lua;"
--

require "mod"
require "base"

function _G.render (file, data)
return tera.instance:render(file, data)
end


function _G.send_request (request)

if type(request) == "string" then
request = {uri = request}
end

request.uuid = uuid.v4()
events["outgoing_request_about_to_be_sent"]:trigger({ request = request })

local response = client_request.send(request)
response.uuid = uuid.v4()
events["incoming_response_received"]:trigger({ response = response })

return response
end


log.info("[starting] web server on " .. address .. ":" .. port)

-- Handler function
return function (request)
Expand All @@ -44,5 +22,11 @@ return function (request)
v.rule(request)
end

if lighttouch_response then
events["outgoing_response_about_to_be_sent"]:trigger({
response = lighttouch_response
})
end

return lighttouch_response
end
36 changes: 36 additions & 0 deletions keys/connect_profile.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function keys.connect_profile (data)

local result = {}

local profile_uuid = data.uuid
result.profile = profile_uuid

content.write_file(profile_uuid, profile_uuid, {
type = "profile",
name = data.name
})

if data.sign_public_key then
local sign_pub_id = uuid.v4()

content.write_file(profile_uuid, uuid.v4(), {
type = "key",
kind = "sign_public",
}, data.sign_public_key)

result.sign_public_key = sign_pub_id
end

if data.place then
local place_id = uuid.v4()

content.write_file(profile_uuid, uuid.v4(), {
type = "place",
host = data.place,
})

result.place = place_id
end

return result
end
2 changes: 1 addition & 1 deletion keys/get_private_key.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function keys.get_private_key ()
local priv_key = content.walk_documents("home",
local priv_key = content.walk_documents(content.home,
function (file_uuid, header, body)
if header.model == "key"
and header.kind == "sign_private"
Expand Down
4 changes: 2 additions & 2 deletions keys/get_profile.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function keys.get_profile ()
return content.walk_documents("home",
function keys.get_profile (profile)
return content.walk_documents(profile or content.home,
function (file_uuid, header, body)
if header.model == "profile" then
return file_uuid, header.name
Expand Down
24 changes: 24 additions & 0 deletions keys/get_profile_data.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function keys.get_profile_data ()

local data = {}
local uuid, name = keys.get_profile()
log.debug("UUID: " .. tostring(uuid))

data.sign_public_key = content.walk_documents(uuid,
function (file_uuid, header, body)
if header.model == "key"
and header.kind == "sign_private"
then return body end
end
)

data.place = content.walk_documents(uuid,
function (file_uuid, header, body)
if header.model == "place" then
return header.host
end
end
)

return data
end
3 changes: 3 additions & 0 deletions keys/mod.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ require "keys.get_private_key"
require "keys.verify_http_signature"
require "keys.sign_http_message"
require "keys.witness_document"
require "keys.connect_profile"
require "keys.get_profile_data"

return keys
2 changes: 1 addition & 1 deletion keys/sign_http_message.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function keys.sign_http_message (message)
local profile_uuid = get_profile()
local profile_uuid = keys.get_profile()

if not profile_uuid then
return false, "No home profile found"
Expand Down
14 changes: 9 additions & 5 deletions keys/verify_http_signature.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ function keys.verify_http_signature (message)
-- order, not specifically keyId before signature

local header = message.headers["signature"]
log.trace("signature header", header)
if not header then
log.info("No signature header")
return false
end
log.trace("signature header " .. header)

if not header then log.info("Unsigned Request") return end

local keyId, signature = header:match('keyId="([^"]+)".+signature="([^"]+)"')
log.debug("keyId", keyId)
log.debug("signature", signature)
log.debug("keyId " .. keyId)
log.debug("signature" .. signature)

local pub_key = content.walk_documents(keyId,
function (file_uuid, header, body)
Expand All @@ -30,10 +34,10 @@ function keys.verify_http_signature (message)

pub_key = crypto.sign.load_public(pub_key)

log.trace("public key", pub_key)
log.trace("public key " .. tostring(pub_key))

local signature_string = "date: " .. message.headers.date .. "\n" .. message.body_raw
log.trace("signature string", signature_string)
log.trace("signature string " .. signature_string)

local is_valid = pub_key:verify_detached(signature_string, signature)

Expand Down
2 changes: 1 addition & 1 deletion loaders/actions/base.lua → loaders/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ package.searchers[2] = actions_loader.custom_require
-- runs the function against all packages in packages_path
each(fs.directory_list(_G.packages_path), actions_loader.create_actions)

return actions_loader
return actions_loader
13 changes: 7 additions & 6 deletions loaders/actions/assign_action_to_event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ function actions_loader.assign_action_to_event(action, file_name)
table.insert( _G.events_actions[v], action )
local event_action = event:addAction(
function(input_parameters)
log.debug("[running] action " .. ansicolors('%{underline}' .. file_name) .. " with priority " .. action.priority )
-- TODO: figure out what to do if more than one responses are returned
if _G.lighttouch_response == nil then
_G.lighttouch_response = action.action(input_parameters)
log.trace("[running] action " .. ansicolors('%{underline}' .. file_name) .. " with priority " .. action.priority )
local response = action.action(input_parameters)
-- Only the first generated response is sent
if response and _G.lighttouch_response == nil then
_G.lighttouch_response = response
end
log.debug("[completed] action " .. ansicolors('%{underline}' .. file_name) )
log.trace("[completed] action " .. ansicolors('%{underline}' .. file_name) )
end
)
event:setActionPriority(event_action, action.priority)
Expand All @@ -21,4 +22,4 @@ function actions_loader.assign_action_to_event(action, file_name)
log.error("event " .. v .. " doesn't exist")
end
end
end
end
1 change: 0 additions & 1 deletion loaders/actions/custom_require.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ function actions_loader.custom_require(name)
return default_package_searchers2(name)
end
end

2 changes: 1 addition & 1 deletion loaders/actions/extract_header.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ function actions_loader.extract_header (modulepath)
end

return scl.to_table(header) -- decode scl string into a lua table
end
end
8 changes: 4 additions & 4 deletions loaders/actions/loop_over_actions.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function actions_loader.loop_over_actions(action_files, package_name)
log.trace("[loading] " .. ansicolors('%{underline}' .. package_name) .. " package's actions")
for _, file_name in ipairs(action_files) do
log.trace("[patching] action " .. ansicolors('%{underline}' .. file_name))

local action_require_name = "packages." .. package_name .. ".actions." .. string.sub( file_name, 0, string.len( file_name ) - 4 )
local action_require = require(action_require_name)
actions_loader.assign_action_to_event(action_require, file_name)

log.trace("[loaded] action " .. ansicolors('%{underline}' .. file_name))
end
log.trace("[patched] actions for package " .. ansicolors('%{underline}' .. package_name))
end
end
2 changes: 1 addition & 1 deletion loaders/actions/set_priority.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function actions_loader.set_priority (created_file, header)
created_file:write("\nlocal priority = " .. header.priority .. " \n\n")
end
end
2 changes: 1 addition & 1 deletion loaders/actions/write_events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ function actions_loader.write_events (created_file, header)
end
end
created_file:write(" }")
end
end
1 change: 0 additions & 1 deletion loaders/actions/write_function.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ function actions_loader.write_function (created_file, header, modulepath)
end
created_file:write("\nend")
end

2 changes: 1 addition & 1 deletion loaders/actions/write_input_parameters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ function actions_loader.write_input_parameters (created_file, header)
end
created_file:write("}\n")
end
end
end
2 changes: 1 addition & 1 deletion loaders/actions/write_return.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function actions_loader.write_return (created_file, header)
created_file:write("\n\nreturn{\n\tevent = event,\n\taction = action,\n\tpriority = priority,\n\tinput_parameters = input_parameters\n}") -- ending return
end
end
53 changes: 0 additions & 53 deletions loaders/classes.lua

This file was deleted.

File renamed without changes.
Loading

0 comments on commit c91b9e9

Please sign in to comment.