2023-10-08 06:30:50 +02:00
|
|
|
args = { ... }
|
|
|
|
|
|
|
|
if args[1] == nil then
|
|
|
|
io.write("Usage: auth <action>\n")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local action = args[1]
|
2023-10-08 08:18:41 +02:00
|
|
|
local server = settings.get("auth.server")
|
2023-10-08 06:30:50 +02:00
|
|
|
|
|
|
|
|
2023-10-08 10:44:58 +02:00
|
|
|
auth_server_id = lns.lookup(server)
|
2023-10-08 06:30:50 +02:00
|
|
|
|
|
|
|
if action == "login" then
|
|
|
|
io.write("Username: ")
|
|
|
|
local username = io.read()
|
|
|
|
io.write("Password: ")
|
2023-10-08 06:33:38 +02:00
|
|
|
local password = read("*")
|
2023-10-08 06:30:50 +02:00
|
|
|
|
|
|
|
local data = {
|
|
|
|
["action"] = "login",
|
|
|
|
["username"] = username,
|
|
|
|
["password"] = password
|
|
|
|
}
|
|
|
|
|
2023-10-08 08:44:20 +02:00
|
|
|
rednet.send(auth_server_id, data, "auth")
|
2023-10-08 06:30:50 +02:00
|
|
|
|
|
|
|
while true do
|
2023-10-08 08:44:20 +02:00
|
|
|
id, msg = rednet.receive("auth")
|
2023-10-08 06:30:50 +02:00
|
|
|
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 password" then
|
|
|
|
io.write("Invalid password\n")
|
|
|
|
return
|
|
|
|
else
|
|
|
|
token = msg
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-08 10:44:58 +02:00
|
|
|
settings.set("auth.username", username)
|
2023-10-08 08:18:41 +02:00
|
|
|
settings.set("auth.token", token)
|
2023-10-08 08:36:02 +02:00
|
|
|
settings.save()
|
2023-10-08 06:30:50 +02:00
|
|
|
io.write("Logged in as " .. username .. "\n")
|
|
|
|
return
|
|
|
|
|
2023-10-08 10:44:58 +02:00
|
|
|
elseif action == "logout" then
|
2023-10-08 08:57:56 +02:00
|
|
|
settings.set("auth.token", -1)
|
2023-10-08 08:55:56 +02:00
|
|
|
settings.save()
|
|
|
|
io.write("Logged out\n")
|
|
|
|
return
|
|
|
|
|
2023-10-08 10:44:58 +02:00
|
|
|
elseif action == "register" then
|
2023-10-08 06:41:44 +02:00
|
|
|
io.write("Username: ")
|
|
|
|
local username = io.read()
|
|
|
|
io.write("Password: ")
|
|
|
|
local password = read("*")
|
|
|
|
io.write("Confirm Password: ")
|
|
|
|
local confirm_password = read("*")
|
|
|
|
|
|
|
|
if password ~= confirm_password then
|
|
|
|
io.write("Passwords do not match\n")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local data = {
|
|
|
|
["action"] = "register",
|
|
|
|
["username"] = username,
|
|
|
|
["password"] = password
|
|
|
|
}
|
|
|
|
|
2023-10-08 08:44:20 +02:00
|
|
|
rednet.send(auth_server_id, data, "auth")
|
2023-10-08 06:41:44 +02:00
|
|
|
|
|
|
|
while true do
|
2023-10-08 08:44:20 +02:00
|
|
|
id, msg = rednet.receive("auth")
|
2023-10-08 06:41:44 +02:00
|
|
|
if id == auth_server_id then
|
|
|
|
if msg == "invalid request" then
|
|
|
|
io.write("Invalid request\n")
|
|
|
|
return
|
|
|
|
elseif msg == "user already exists" then
|
|
|
|
io.write("User already exists\n")
|
|
|
|
return
|
|
|
|
else
|
|
|
|
io.write("Registered user " .. username .. "\n")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-10-08 10:44:58 +02:00
|
|
|
elseif action == "group" then
|
|
|
|
local subaction = args[2]
|
|
|
|
if subaction == nil then
|
|
|
|
io.write("Usage: auth group <list:check:add>\n")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if subaction == "list" then
|
|
|
|
local result = auth.list_user_groups(settings.get("auth.username"))
|
|
|
|
io.write("Groups: " .. result .. "\n")
|
|
|
|
elseif subaction == "add" then
|
|
|
|
local target_user = args[3]
|
|
|
|
local group = args[4]
|
|
|
|
if target_user == nil or group == nil then
|
|
|
|
io.write("Usage: auth group add <user> <group>\n")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local result = auth.add_user_group(target_user, group)
|
|
|
|
end
|
2023-10-08 06:41:44 +02:00
|
|
|
end
|