Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local USERS_DB = "/domain/users.db" -- Path to user database
- -- Simulated database: You can replace this with a more advanced implementation
- local users = {
- ["user1"] = {password = "hashedPassword123", roles = {"admin"}},
- ["user2"] = {password = "hashedPassword456", roles = {"user"}},
- }
- -- Save the database to a file
- local function saveDatabase()
- local file = fs.open(USERS_DB, "w")
- file.write(textutils.serialize(users))
- file.close()
- end
- -- Load the database from a file
- local function loadDatabase()
- if fs.exists(USERS_DB) then
- local file = fs.open(USERS_DB, "r")
- users = textutils.unserialize(file.readAll())
- file.close()
- else
- saveDatabase()
- end
- end
- -- Validate user credentials
- local function validateUser(username, password)
- local user = users[username]
- if user and user.password == password then -- Replace with hashing for real security
- return true, user.roles
- else
- return false
- end
- end
- -- Server loop
- local function startDomainController()
- print("Domain Controller is running...")
- while true do
- print("Waiting for client requests...")
- local event, side, message, replyChannel = os.pullEvent("modem_message")
- local request = textutils.unserialize(message)
- if request and request.type == "authenticate" then
- local success, roles = validateUser(request.username, request.password)
- local response = {success = success, roles = roles}
- rednet.send(replyChannel, textutils.serialize(response))
- elseif request and request.type == "fetch_users" then
- local userData = {}
- for username, data in pairs(users) do
- userData[username] = {roles = data.roles} -- Exclude passwords
- end
- rednet.send(replyChannel, textutils.serialize(userData))
- end
- end
- end
- loadDatabase()
- startDomainController()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement