Advertisement
DOGGYWOOF

Domain controller

Nov 27th, 2024 (edited)
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. local USERS_DB = "/domain/users.db" -- Path to user database
  2.  
  3. -- Simulated database: You can replace this with a more advanced implementation
  4. local users = {
  5. ["user1"] = {password = "hashedPassword123", roles = {"admin"}},
  6. ["user2"] = {password = "hashedPassword456", roles = {"user"}},
  7. }
  8.  
  9. -- Save the database to a file
  10. local function saveDatabase()
  11. local file = fs.open(USERS_DB, "w")
  12. file.write(textutils.serialize(users))
  13. file.close()
  14. end
  15.  
  16. -- Load the database from a file
  17. local function loadDatabase()
  18. if fs.exists(USERS_DB) then
  19. local file = fs.open(USERS_DB, "r")
  20. users = textutils.unserialize(file.readAll())
  21. file.close()
  22. else
  23. saveDatabase()
  24. end
  25. end
  26.  
  27. -- Validate user credentials
  28. local function validateUser(username, password)
  29. local user = users[username]
  30. if user and user.password == password then -- Replace with hashing for real security
  31. return true, user.roles
  32. else
  33. return false
  34. end
  35. end
  36.  
  37. -- Server loop
  38. local function startDomainController()
  39. print("Domain Controller is running...")
  40.  
  41. while true do
  42. print("Waiting for client requests...")
  43. local event, side, message, replyChannel = os.pullEvent("modem_message")
  44. local request = textutils.unserialize(message)
  45.  
  46. if request and request.type == "authenticate" then
  47. local success, roles = validateUser(request.username, request.password)
  48. local response = {success = success, roles = roles}
  49. rednet.send(replyChannel, textutils.serialize(response))
  50. elseif request and request.type == "fetch_users" then
  51. local userData = {}
  52. for username, data in pairs(users) do
  53. userData[username] = {roles = data.roles} -- Exclude passwords
  54. end
  55. rednet.send(replyChannel, textutils.serialize(userData))
  56. end
  57. end
  58. end
  59.  
  60. loadDatabase()
  61. startDomainController()
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement