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