iteration

This commit is contained in:
Layla 2023-10-08 00:19:55 -04:00
parent 7fcae7b276
commit 627dc1ac68
3 changed files with 138 additions and 6 deletions

135
auth_server.lua Normal file
View File

@ -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

View File

@ -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")

3
utils/lsh.lua Normal file
View File

@ -0,0 +1,3 @@
local bin_path = "/bin/"
shell.setPath(shell.path() .. ":" .. bin_path)