diff --git a/auth.lua b/auth.lua index aff73fc..4d3d4ad 100644 --- a/auth.lua +++ b/auth.lua @@ -6,7 +6,20 @@ if args[1] == nil then end local action = args[1] -local server = "auth.layla.mc" + +settings.define("auth.token", { + description = "Authentication token", + type = "string", + default = "" +}) + +settings.define("auth.server", { + description = "Authentication server", + type = "string", + default = "auth.box" +}) + +local server = settings.get("auth.server") function lns_lookup(hostname) local data = { @@ -65,8 +78,8 @@ if action == "login" then end end + settings.set("auth.token", token) io.write("Logged in as " .. username .. "\n") - io.write("Token: " .. token .. "\n") return end diff --git a/lns_server.lua b/lns_server.lua index 8d37edd..02f9903 100644 --- a/lns_server.lua +++ b/lns_server.lua @@ -1,8 +1,31 @@ -modem_location = "right" +settings.define("lns_server.modem_location", { + description = "Modem location", + type = "string", + default = "right" +}) +modem_location = settings.get("lns_server.modem_location") +settings.define("lns_server.rednet_hostname", { + description = "Rednet hostname", + type = "string", + default = "test" +}) -rednet.host("lns", "default") +settings.define("lns_server.require_auth", { + description = "Require authentication", + type = "boolean", + default = false +}) +require_auth = settings.get("lns_server.require_auth") + +settings.define("lns_server.auth_server", { + description = "Authentication server", + type = "string", + default = "auth.box" +}) + +rednet.host("lns", settings.get("lns_server.rednet_hostname")) rednet.open(modem_location) local data = {} @@ -68,9 +91,37 @@ while true do end if request.action == "register" then - data[request.hostname] = client_id - log("Registered " .. request.hostname .. " as " .. client_id) - save_data() - rednet.send(client_id, "ok") + + local auth_passed = false + if require_auth then + local authID = data[auth_server] + if authID ~= nil then + rednet.send(authID, { + action = "token", + token = request.token + }) + + local auth_response = rednet.receive() + local errorPos, errorEnd = string.find(auth_response, "invalid") + if errorPos then + log("Error: " .. auth_response) + else + auth_passed = true + end + else + log("Error: Auth server not found to " .. client_id) + end + else + auth_passed = true + end + + if auth_passed then + data[request.hostname] = client_id + log("Registered " .. request.hostname .. " as " .. client_id) + save_data() + rednet.send(client_id, "ok") + else + rednet.send(client_id, "auth required") + end end end \ No newline at end of file diff --git a/register_lns.lua b/register_lns.lua index d9bdc22..3c9448f 100644 --- a/register_lns.lua +++ b/register_lns.lua @@ -21,7 +21,8 @@ current_time = os.time() data = { ["action"] = "register", - ["hostname"] = hostname + ["hostname"] = hostname, + ["token"] = settings.get("auth.token", nil) } rednet.send(receiverID, data)