Advertisement
Guest User

Auth Key program Readability revision

a guest
May 13th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.85 KB | None | 0 0
  1. -- Changes
  2. -- * moved everything into the .authKey folder.
  3. --   the . makes the folder hidden
  4. --
  5. -- * added block to create .authKey folder if it
  6. --   does not exist
  7. --
  8. -- * cahnged backslashes in file path strings to
  9. --   forward slahes to prevent charecter escapes.
  10. --
  11. -- * renamed pass to passFile because having a
  12. --   pass and a password is confusing.
  13. --
  14. -- * changed password to a local variable so others
  15. --   cannot read it after program runs.
  16. --
  17. -- * added parenthesis to pass.close
  18. --
  19. -- * added return statement to first code block
  20. --   to get rid of the setup variable
  21. --
  22. -- * changed "password = pass.readAll()" to
  23. --   "password == pass.readAll()"
  24. --
  25. -- * Closed the passFile in password checking section.
  26. --
  27. -- * replaced the statement "goodPass = false" with
  28. --   a return statement. This eliminates the
  29. --   goodPass variable and removed a layer of
  30. --   indentation of the bulk of your program.
  31. --
  32. -- * Moved all the code that creates files if they
  33. --   dont exist into the beginning. Its good to put
  34. --   similair code together.
  35. --
  36. -- * Moved code into local functions to seperate the
  37. --   code that checks arguments and the code that
  38. --   actually does stuff. This also means all your
  39. --   code isn't in a gigantic if statement.
  40. --
  41. -- * renamed ranKey to randKey
  42. --
  43. -- * made pcID, user, and key local variables
  44. --
  45. -- * changed send functions to rednet.send functions
  46. --
  47. -- * changed "elseif fs.exists("authIDs\".. pcID) then"
  48. --   to "else". there is only to parts of your if
  49. --   and you dont have to check if the file exists twice.
  50. --
  51. -- * switched the sections in the " if not fs.exists("/.authKey/authIDs/".. pcID) then"
  52. --   statement. I like to avoid negative logic.
  53. --
  54. -- * moved code into a main function. I like to do this
  55. --   becuase all the main logic is up front and you
  56. --   dont have to worry about what order all your
  57. --   functions were declared. There is a main() at the
  58. --   bottom of the code.
  59. --
  60. -- Edited by: GhastTearz
  61. -- Original code at: www.pastebin.com/mGUecsv8
  62.  
  63. local args = { ... }
  64.  
  65. local function main()
  66.     -- Make sure all needed files exist
  67.     if not fs.exists("/.authKey") then
  68.         fs.makeDir("/.authKey")
  69.     end
  70.     if not fs.exists("/.authKey/authKeys") then
  71.         fs.makeDir("/.authKey/authKeys")
  72.     end
  73.     if not fs.exists("/.authKey/authIDs") then
  74.         fs.makeDir("/.authKey/authIDs")
  75.     end
  76.     if not fs.exists("/.authKey/Settings") then
  77.         fs.makeDir("/.authKey/Settings")
  78.         print("Please set an admin password:")
  79.         local password = read('*')
  80.         local passFile = fs.open("/.authKey/Settings/password", "w")
  81.         passFile.write(password)
  82.         passFile.close()
  83.         print("Thank you. You may now log in with that password in the future to make keys.")
  84.         print("Keys can only be made from this server, and nowhere else.")
  85.         return
  86.     end
  87.  
  88.     print("Please enter your password:")
  89.     local password = read('*')
  90.     local passFile = fs.open("/.authKey/Settings/password", "r")
  91.     local correctPassword = passFile.readAll()
  92.     passFile.close()
  93.     if password == correctPassword then
  94.         print("Password correct, logging you in.")
  95.     else
  96.         printError("Password incorrect. Please try again.")
  97.         return
  98.     end
  99.  
  100.     if args[1] == "createKey" then
  101.         if not args[2] then
  102.             printError("Useage: authKey createKey <Username>")
  103.         else
  104.             createKey(args[2])
  105.         end
  106.     elseif args[1] == "startListener" then
  107.         startListener()
  108.     elseif args[1] == "changePass" then
  109.         changePass()
  110.     else
  111.         printError("Usage:")
  112.         printError("Add new user:                    authKey createKey <Username>")
  113.         printError("Start listening for connections: authKey startListener")
  114.         printError("Change admin password:           authKey changePass")
  115.     end
  116. end
  117.  
  118.  
  119. local function createKey(userName)
  120.     math.randomseed(os.time())
  121.     randKey = math.random(10000000)
  122.     local keys = fs.open("/.authKey/authKeys/".. userName, "w")
  123.     keys.write(randKey)
  124.     keys.close()
  125.     print("Your key is: ".. ranKey)
  126.     print("User ".. userName.. " can log in with the client using their username and key.")
  127.     print("This key is valid for a one time use, and will never be usable again.")
  128. end
  129.  
  130.  
  131. local function startListener()
  132.     while true do
  133.         local pcID
  134.         local user
  135.         local key
  136.         pcID, user, key = rednet.receive()
  137.         if fs.exists("/.authKey/authIDs/".. pcID) then
  138.             userFile = fs.open("/.authKey/authIDs/".. pcID, "r")
  139.             user = userFile.readAll()
  140.             userFile.close()
  141.             rednet.send(pcID, "Welcome back, ".. user.. "!")
  142.         else
  143.             if fs.exists("/.authKey/authKeys/".. user) then
  144.                 keyFile = fs.open("/.authKey/authKeys/".. user, "r")
  145.                 if key == keyFile.readAll() then
  146.                     keyFile.close()
  147.                     rednet.send(pcID, "Success")
  148.                     userPcID = fs.open("/.authKey/authIDs/".. pcID, "w")
  149.                     userPcID.write(user)
  150.                     userPcID.close()
  151.                     fs.delete("/.authKey/authKeys/".. user)
  152.                 else
  153.                     keyFile.close()
  154.                     rednet.send(pcID, "Failure: Key does not match user.")
  155.                 end
  156.             else
  157.                 rednet.send(pcID, "Failire: User does not exist.")
  158.             end
  159.         end
  160.     end
  161. end
  162.  
  163.  
  164. local function changePass()
  165.     print("Please set a new admin password: ")
  166.     local password = read('*')
  167.     local passFile = fs.open("/.authFile/Settings/password", "w")
  168.     pass.write(password)
  169.     pass.close()
  170.     print("Thank you. You may now log in with that password in the future to make keys.")
  171.     print("Keys can only be made from this server, and nowhere else.")
  172. end
  173.  
  174.  
  175. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement