iteration
This commit is contained in:
		@ -6,44 +6,10 @@ if args[1] == nil then
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local action = args[1]
 | 
			
		||||
 | 
			
		||||
settings.define("auth.token", {
 | 
			
		||||
    description = "Authentication token",
 | 
			
		||||
    type = "number",
 | 
			
		||||
    default = -1
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
settings.define("auth.server", {
 | 
			
		||||
    description = "Authentication server",
 | 
			
		||||
    type = "string",
 | 
			
		||||
    default = "auth.box"
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
local server = settings.get("auth.server")
 | 
			
		||||
 | 
			
		||||
function lns_lookup(hostname)
 | 
			
		||||
    local data = {
 | 
			
		||||
        ["action"] = "lookup",
 | 
			
		||||
        ["hostname"] = hostname
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    lns_server_id = rednet.lookup("lns", lns_server)
 | 
			
		||||
    rednet.send(lns_server_id, data, "lns")
 | 
			
		||||
 | 
			
		||||
    while true do
 | 
			
		||||
        id, msg = rednet.receive("lns")
 | 
			
		||||
        if id == lns_server_id then
 | 
			
		||||
            if msg == nil then
 | 
			
		||||
                return nil
 | 
			
		||||
            else
 | 
			
		||||
                return msg
 | 
			
		||||
            end
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
auth_server_id = lns_lookup(server)
 | 
			
		||||
auth_server_id = lns.lookup(server)
 | 
			
		||||
 | 
			
		||||
if action == "login" then
 | 
			
		||||
    io.write("Username: ")
 | 
			
		||||
@ -78,20 +44,19 @@ if action == "login" then
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    settings.set("auth.username", username)
 | 
			
		||||
    settings.set("auth.token", token)
 | 
			
		||||
    settings.save()
 | 
			
		||||
    io.write("Logged in as " .. username .. "\n")
 | 
			
		||||
    return
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
if action == "logout" then
 | 
			
		||||
elseif action == "logout" then
 | 
			
		||||
    settings.set("auth.token", -1)
 | 
			
		||||
    settings.save()
 | 
			
		||||
    io.write("Logged out\n")
 | 
			
		||||
    return
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
if action == "register" then
 | 
			
		||||
elseif action == "register" then
 | 
			
		||||
    io.write("Username: ")
 | 
			
		||||
    local username = io.read()
 | 
			
		||||
    io.write("Password: ")
 | 
			
		||||
@ -127,4 +92,24 @@ if action == "register" then
 | 
			
		||||
            end
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
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
 | 
			
		||||
end
 | 
			
		||||
@ -158,4 +158,63 @@ while true do
 | 
			
		||||
            rednet.send(client_id, "invalid token", "auth")
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    if request.action == "check_group" then
 | 
			
		||||
        if request.username == nil or request.group == nil then
 | 
			
		||||
            rednet.send(client_id, "invalid request", "auth")
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        if data.users[request.username].groups == nil then
 | 
			
		||||
            rednet.send(client_id, false, "auth")
 | 
			
		||||
        else
 | 
			
		||||
            rednet.send(client_id, data.users[request.username].groups[request.group] ~= nil, "auth")
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    if request.action == "list_groups" then
 | 
			
		||||
        if request.username == nil then
 | 
			
		||||
            rednet.send(client_id, "invalid request", "auth")
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        if data.users[request.username].groups == nil then
 | 
			
		||||
            rednet.send(client_id, {}, "auth")
 | 
			
		||||
        else
 | 
			
		||||
            rednet.send(client_id, data.users[request.username].groups, "auth")
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    -- Only if in group admin
 | 
			
		||||
    if request.action == "add_group" then
 | 
			
		||||
        if request.username == nil or request.group == nil or request.token == nil then
 | 
			
		||||
            rednet.send(client_id, "invalid request", "auth")
 | 
			
		||||
        else
 | 
			
		||||
            local found = false
 | 
			
		||||
            local acting_user = nil
 | 
			
		||||
            for user, userdata in pairs(data.users) do
 | 
			
		||||
                if userdata.token == request.token then
 | 
			
		||||
                    acting_user = user
 | 
			
		||||
                    found = true
 | 
			
		||||
                end
 | 
			
		||||
            end
 | 
			
		||||
 | 
			
		||||
            if not found then
 | 
			
		||||
                rednet.send(client_id, "invalid token", "auth")
 | 
			
		||||
            elseif data.users[acting_user].groups["admin"] == nil then
 | 
			
		||||
                rednet.send(client_id, "invalid privileges", "auth")
 | 
			
		||||
            else
 | 
			
		||||
                if data.users[request.username].groups == nil then
 | 
			
		||||
                    data.users[request.username].groups = {}
 | 
			
		||||
                end
 | 
			
		||||
 | 
			
		||||
                data.users[request.username].groups[request.group] = true
 | 
			
		||||
 | 
			
		||||
                save_data()
 | 
			
		||||
 | 
			
		||||
                rednet.send(client_id, "ok", "auth")
 | 
			
		||||
                log(request.username .. " added to group " .. request.group, "auth")
 | 
			
		||||
            else
 | 
			
		||||
                rednet.send(client_id, "invalid token", "auth")
 | 
			
		||||
            end
 | 
			
		||||
        end
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user