settings.define("auth.token", { description = "Authentication token", type = "number", default = -1 }) settings.define("auth.server", { description = "Authentication server", type = "string", default = "auth.box" }) function get_token() return settings.get("auth.token") end function check_user_group(username, group) local data = { ["action"] = "check_group", ["username"] = username, ["group"] = group } local auth_server_id = lns.lookup(settings.get("auth.server")) rednet.send(auth_server_id, data, "auth") while true do id, msg = rednet.receive("auth") if id == auth_server_id then if msg == "invalid request" then io.write("Invalid request\n") return elseif msg == "user not found" then io.write("User not found\n") return elseif msg == "invalid token" then io.write("Invalid token\n") return elseif msg == "invalid group" then io.write("Invalid group\n") return elseif msg == "ok" then return true else return false end end end end function list_to_string(list) local str = "" for i, v in ipairs(list) do str = str .. v .. ", " end return str:sub(1, -3) end function list_user_groups(username) local data = { ["action"] = "list_groups", ["username"] = username } local auth_server_id = lns.lookup(settings.get("auth.server")) rednet.send(auth_server_id, data, "auth") while true do id, msg = rednet.receive("auth") if id == auth_server_id then if msg == "invalid request" then io.write("Invalid request\n") return elseif msg == "user not found" then io.write("User not found\n") return elseif msg == "invalid token" then io.write("Invalid token\n") return else return msg end end end end function add_user_to_group(username, group) local data = { ["action"] = "add_group", ["username"] = username, ["group"] = group, ["token"] = get_token() } local auth_server_id = lns.lookup(settings.get("auth.server")) rednet.send(auth_server_id, data, "auth") while true do id, msg = rednet.receive("auth") if id == auth_server_id then if msg == "invalid request" then io.write("Invalid request\n") return false elseif msg == "user not found" then io.write("User not found\n") return false elseif msg == "invalid token" then io.write("Invalid token\n") return false elseif msg == "invalid privileges" then io.write("Invalid privileges\n") return false elseif msg == "ok" then return true else return false end end end end