computercraft/packages/auth.lua

75 lines
2.0 KiB
Lua
Raw Normal View History

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]
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
2023-10-09 12:54:35 +02:00
if login(username, password) then
io.write("Logged in as " .. username .. "\n")
2023-10-08 06:30:50 +02:00
end
2023-10-08 10:44:58 +02:00
elseif action == "logout" then
2023-10-09 12:54:35 +02:00
auth.logout()
2023-10-08 08:55:56 +02:00
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
2023-10-09 12:54:35 +02:00
auth.register(username, password)
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"))
2023-10-08 11:19:07 +02:00
io.write("Groups: " .. auth.trueKeysToString(result) .. "\n")
2023-10-08 10:44:58 +02:00
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
2023-10-08 10:54:18 +02:00
local result = auth.add_user_to_group(target_user, group)
2023-10-08 11:25:17 +02:00
elseif subaction == "check" then
local target_user = args[3]
local group = args[4]
if target_user == nil or group == nil then
io.write("Usage: auth group check <user> <group>\n")
return
end
local result = auth.check_user_in_group(target_user, group)
if result then
io.write("User " .. target_user .. " is in group " .. group .. "\n")
else
io.write("User " .. target_user .. " is not in group " .. group .. "\n")
end
else
io.write("Usage: auth group <list:check:add>\n")
return
2023-10-08 10:44:58 +02:00
end
2023-10-08 06:41:44 +02:00
end