Advertisement
Doug4347

authKey

May 12th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. local args = { ... }
  2.  
  3. setup = false
  4.  
  5. if not fs.exists("authKeySettings") then
  6.     setup = true
  7.     fs.makeDir("authKeySettings")
  8.     print("Please set an admin password: ")
  9.     password = read('*')
  10.     local pass = fs.open("authKeySettings\password", "w")
  11.     pass.write(password)
  12.     pass.close
  13.     print("Thank you. You may now log in with that password in the future to make keys.")
  14.     print("Keys can only be made from this server, and nowhere else.")
  15. end
  16. if not setup then
  17.     print("Please enter your password:")
  18.     password = read('*')
  19.     local pass = fs.open("authKeySettings\password", "r")
  20.     if password = pass.readAll() then
  21.         goodPass = true
  22.         print("Password correct, logging you in.")
  23.     else
  24.         goodPass = false
  25.         printError("Password incorrect. Please try again.")
  26.     end
  27. end
  28.  
  29. if not setup and goodPass then
  30.     if not fs.exists("authKeys") then
  31.         fs.makeDir("authKeys")
  32.     end
  33.     if not fs.exists("authIDs") then
  34.         fs.makeDir("authIDs")
  35.     end
  36.     if args[1] == "createKey" then
  37.         if not args[2] then
  38.             printError("Useage: authKey createKey <Username>")
  39.         else
  40.             math.randomseed(os.time())
  41.             ranKey = math.random(10000000)
  42.             local keys = fs.open("authKeys/".. args[2], "w")
  43.             keys.write(ranKey)
  44.             keys.close()
  45.             print("Your key is: ".. ranKey)
  46.             print("User ".. args[2].. " can log in with the client using their username and key.")
  47.             print("This key is valid for a one time use, and will never be usable again.")
  48.         end
  49.     elseif args[1] == "startListener" then
  50.         while true do
  51.             pcID, user, key = rednet.receive()
  52.             if not fs.exists("authIDs\".. pcID) then
  53.                 if fs.exists("authKeys\".. user) then
  54.                     keyFile = fs.open("authKeys\".. user, "r")
  55.                     if key == keyFile.readAll) then
  56.                         keyFile.close()
  57.                         send(pcID, "Success")
  58.                         userPcID = fs.open("authIDs\".. pcID, "w")
  59.                         userPcID.write(user)
  60.                         userPcID.close()
  61.                         fs.delete("authKeys\".. user)
  62.                     else
  63.                         keyFile.close()
  64.                         send(pcID, "Failure: Key does not match user.")
  65.                     end
  66.                 else
  67.                     send(pcID, "Failire: User does not exist.")
  68.                 end
  69.             elseif fs.exists("authIDs\".. pcID) then
  70.                 userFile = fs.open("authIDs\".. pcID, "r")
  71.                 user = userFile.readAll()
  72.                 userFile.close()
  73.                 rednet.send(pcID, "Welcome back, ".. user.. "!")
  74.             end
  75.         end
  76.     elseif args[1] == "changePass" then
  77.         print("Please set a new admin password: ")
  78.         password = read('*')
  79.         local pass = fs.open("authKeySettings\password", "w")
  80.         pass.write(password)
  81.         pass.close
  82.         print("Thank you. You may now log in with that password in the future to make keys.")
  83.         print("Keys can only be made from this server, and nowhere else.")
  84.     else
  85.         printError("Usage:")
  86.         printError("Add new user:                    authKey createKey <Username>")
  87.         printError("Start listening for connections: authKey startListener")
  88.         printError("Change admin password:           authKey changePass")
  89.     end
  90. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement