From 627dc1ac68709d040a1ddf882b76635a34cc67ea Mon Sep 17 00:00:00 2001 From: Layla Manley Date: Sun, 8 Oct 2023 00:19:55 -0400 Subject: [PATCH] iteration --- auth_server.lua | 135 ++++++++++++++++++++++++++++++++++++++++++++++++ lupdate.lua | 6 --- utils/lsh.lua | 3 ++ 3 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 auth_server.lua create mode 100644 utils/lsh.lua diff --git a/auth_server.lua b/auth_server.lua new file mode 100644 index 0000000..617daa8 --- /dev/null +++ b/auth_server.lua @@ -0,0 +1,135 @@ + + +data = { + users = { + ["admin"] = { + password = "admin" + } + } +} + +function save_data() + db = fs.open("auth.db", "w") + db.write(textutils.serialize(data)) + db.close() +end + +function load_data() + if fs.exists("auth.db") then + db = fs.open("auth.db", "r") + db_contents = db.readAll() + db.close() + + data = textutils.unserialize(db_contents) + end +end + +function generate_token(user) + local token = textutils.sha256(user .. os.time() .. math.random()) + data.users[user].token = token + + return token +end + +function convert_password(password) + return textutils.sha256(password + "3m&LmNm7") +end + +load_data() +math.randomseed(os.time()) + +while true do + client_id, msg = rednet.receive() + request = msg + + if request.action == nil then + rednet.send(client_id, "invalid request") + end + + if request.action == "register" then + if request.username == nil or request.password == nil then + rednet.send(client_id, "invalid request") + end + + if data.users[request.username] ~= nil then + rednet.send(client_id, "user already exists") + end + + data.users[request.username] = { + password = convert_password(request.password) + } + + save_data() + + rednet.send(client_id, "ok") + end + + if request.action == "login" then + if request.username == nil or request.password == nil then + rednet.send(client_id, "invalid request") + end + + if data.users[request.username] == nil then + rednet.send(client_id, "user not found") + end + + if convert_password(data.users[request.username].password) == request.password then + local token = generate_token(request.username) + + rednet.send(client_id, token) + else + rednet.send(client_id, "invalid password") + end + + end + + if request.action == "token" then + if request.token == nil then + rednet.send(client_id, "invalid request") + end + + for user, userdata in pairs(data.users) do + if userdata.token == request.token then + rednet.send(client_id, user) + end + end + + rednet.send(client_id, "invalid token") + end + + if request.action == "profile" then + if request.username == nil then + rednet.send(client_id, "invalid request") + end + + local profile = {} + + if data.users[request.username].profile_data ~= nil then + profile = data.users[request.username].profile_data + else + profile.display_name = request.username + end + + profile.username = request.username + + rednet.send(client_id, data.users[request.username]) + end + + if request.action == "update_profile" then + if request.username == nil or request.token == nil then + rednet.send(client_id, "invalid request") + end + + if data.users[request.username].token == request.token then + if request.profile_data ~= nil then + data.users[request.username].profile_data = request.profile_data + end + + save_data() + + rednet.send(client_id, "ok") + else + rednet.send(client_id, "invalid token") + end + end +end \ No newline at end of file diff --git a/lupdate.lua b/lupdate.lua index 1cd1372..0aea7f2 100644 --- a/lupdate.lua +++ b/lupdate.lua @@ -19,12 +19,6 @@ if response.getResponseCode() == 200 then file.write(response.readAll()) file.close() - -- check if program is in path - if not shell.resolveProgram(program_name) then - io.write("Program not in path, adding to path\n") - shell.setPath(shell.path() .. ":" .. bin_path) - end - io.write("Updated " .. program_name .. ".lua\n") else io.write("Failed to update " .. program_name .. ".lua\n") diff --git a/utils/lsh.lua b/utils/lsh.lua new file mode 100644 index 0000000..fe23f5f --- /dev/null +++ b/utils/lsh.lua @@ -0,0 +1,3 @@ + +local bin_path = "/bin/" +shell.setPath(shell.path() .. ":" .. bin_path) \ No newline at end of file