iteration
This commit is contained in:
		
							
								
								
									
										109
									
								
								lib/auth.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								lib/auth.lua
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,109 @@
 | 
				
			|||||||
 | 
					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
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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_user_groups(username)
 | 
				
			||||||
 | 
					    local data = {
 | 
				
			||||||
 | 
					        ["action"] = "list_groups",
 | 
				
			||||||
 | 
					        ["username"] = username
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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 privileges" then
 | 
				
			||||||
 | 
					                io.write("Invalid privileges\n")
 | 
				
			||||||
 | 
					                return
 | 
				
			||||||
 | 
					            elseif msg == "ok" then
 | 
				
			||||||
 | 
					                return
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					                return
 | 
				
			||||||
 | 
					            end
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
@ -6,44 +6,10 @@ if args[1] == nil then
 | 
				
			|||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
local action = args[1]
 | 
					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")
 | 
					local server = settings.get("auth.server")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function lns_lookup(hostname)
 | 
					 | 
				
			||||||
    local data = {
 | 
					 | 
				
			||||||
        ["action"] = "lookup",
 | 
					 | 
				
			||||||
        ["hostname"] = hostname
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    lns_server_id = rednet.lookup("lns", lns_server)
 | 
					auth_server_id = lns.lookup(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)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
if action == "login" then
 | 
					if action == "login" then
 | 
				
			||||||
    io.write("Username: ")
 | 
					    io.write("Username: ")
 | 
				
			||||||
@ -78,20 +44,19 @@ if action == "login" then
 | 
				
			|||||||
        end
 | 
					        end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    settings.set("auth.username", username)
 | 
				
			||||||
    settings.set("auth.token", token)
 | 
					    settings.set("auth.token", token)
 | 
				
			||||||
    settings.save()
 | 
					    settings.save()
 | 
				
			||||||
    io.write("Logged in as " .. username .. "\n")
 | 
					    io.write("Logged in as " .. username .. "\n")
 | 
				
			||||||
    return
 | 
					    return
 | 
				
			||||||
end
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
if action == "logout" then
 | 
					elseif action == "logout" then
 | 
				
			||||||
    settings.set("auth.token", -1)
 | 
					    settings.set("auth.token", -1)
 | 
				
			||||||
    settings.save()
 | 
					    settings.save()
 | 
				
			||||||
    io.write("Logged out\n")
 | 
					    io.write("Logged out\n")
 | 
				
			||||||
    return
 | 
					    return
 | 
				
			||||||
end
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
if action == "register" then
 | 
					elseif action == "register" then
 | 
				
			||||||
    io.write("Username: ")
 | 
					    io.write("Username: ")
 | 
				
			||||||
    local username = io.read()
 | 
					    local username = io.read()
 | 
				
			||||||
    io.write("Password: ")
 | 
					    io.write("Password: ")
 | 
				
			||||||
@ -127,4 +92,24 @@ if action == "register" then
 | 
				
			|||||||
            end
 | 
					            end
 | 
				
			||||||
        end
 | 
					        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
 | 
					end
 | 
				
			||||||
@ -158,4 +158,63 @@ while true do
 | 
				
			|||||||
            rednet.send(client_id, "invalid token", "auth")
 | 
					            rednet.send(client_id, "invalid token", "auth")
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
    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
 | 
					end
 | 
				
			||||||
		Reference in New Issue
	
	Block a user