-- Desc: Authentication library -- Auth: Layla Manley -- Link: https://gitea.layla.gg/layla/computercraft settings.define("auth.token", { description = "Authentication token", type = "number", default = -1 }) settings.define("auth.server", { description = "Authentication server", type = "string", default = "auth.box" }) function login(username, password) local data = { ["action"] = "login", ["username"] = username, ["password"] = password } local auth_server_id = lns.lookup(settings.get("auth.server")) rednet.send(auth_server_id, data, "auth") local token = nil 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 password" then io.write("Invalid password\n") return false else token = msg break end end end settings.set("auth.username", username) settings.set("auth.token", token) settings.save() return true end function register(username, password) local data = { ["action"] = "register", ["username"] = username, ["password"] = password } 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 already exists" then io.write("User already exists\n") return false else io.write("Registered user " .. username .. "\n") return true end end end end function logout() settings.set("auth.token", -1) settings.save() io.write("Logged out\n") end function get_token() return settings.get("auth.token") end function check_user_in_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 false elseif msg == true then return true else return false end end end end function trueKeysToString(tbl) local result = "" local first = true for key, value in pairs(tbl) do if value == true then if not first then result = result .. ", " else first = false end result = result .. key end end return result 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