iteration
This commit is contained in:
130
packages/auth.lua
Normal file
130
packages/auth.lua
Normal file
@@ -0,0 +1,130 @@
|
||||
args = { ... }
|
||||
|
||||
if args[1] == nil then
|
||||
io.write("Usage: auth <action>\n")
|
||||
return
|
||||
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)
|
||||
|
||||
if action == "login" then
|
||||
io.write("Username: ")
|
||||
local username = io.read()
|
||||
io.write("Password: ")
|
||||
local password = read("*")
|
||||
|
||||
local data = {
|
||||
["action"] = "login",
|
||||
["username"] = username,
|
||||
["password"] = password
|
||||
}
|
||||
|
||||
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 password" then
|
||||
io.write("Invalid password\n")
|
||||
return
|
||||
else
|
||||
token = msg
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
settings.set("auth.token", token)
|
||||
settings.save()
|
||||
io.write("Logged in as " .. username .. "\n")
|
||||
return
|
||||
end
|
||||
|
||||
if action == "logout" then
|
||||
settings.set("auth.token", -1)
|
||||
settings.save()
|
||||
io.write("Logged out\n")
|
||||
return
|
||||
end
|
||||
|
||||
if action == "register" then
|
||||
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
|
||||
}
|
||||
|
||||
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 already exists" then
|
||||
io.write("User already exists\n")
|
||||
return
|
||||
else
|
||||
io.write("Registered user " .. username .. "\n")
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user