Advertisement
Guest User

auth

a guest
Dec 14th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. -- cs authentication
  2. -- prompt for login on startup
  3.  
  4. -- specify the authentication server details
  5. authServer = "none"
  6. authProtocol = "auth"
  7.  
  8. -- specify the local credentials
  9. localuser = "admin"
  10. localpass = "admin"
  11.  
  12. -- used to validate credentials with server
  13. function validateCredentialsRemote(id, user, pass)
  14.   -- detect and open modems
  15.   for k, v in pairs(redstone.getSides()) do
  16.     if peripheral.getType(v) == "modem" then
  17.       rednet.open(v)
  18.     end
  19.   end    
  20.   -- send the credentials to the authentication server
  21.   local credentials = {user, pass}
  22.   rednet.send(id, credentials, authProtocol)
  23.   -- receive a message from the server
  24.   local source, message = rednet.receive(authProtocol, 10)
  25.   if source == id and message == "SUCCESS" then
  26.     return true    
  27.   elseif source == id and message == "FAILURE" then
  28.     return false, "Invalid credentials."
  29.   else
  30.     return false, "Could not connect to the server."
  31.   end  
  32. end
  33.  
  34. -- used to validate credentials locally
  35. function validateCredentialsLocal(user, pass)
  36.   if user == localuser and pass == localpass then
  37.     return true
  38.   else
  39.     return false, "Invalid credentials."
  40.   end
  41. end
  42.  
  43. -- continuously check for logins
  44. while true do
  45.   -- clear the terminal
  46.   term.clear()
  47.   term.setCursorPos(1, 1)
  48.   -- print a welcome message
  49.   print("PROPERTY OF CS FACTION\n")
  50.  
  51.   -- prompt the user for a username and password
  52.   write("username> ")
  53.   local user = read()
  54.   write("password> ")
  55.   local pass = read()    
  56.  
  57.   -- validate with a remote server or locally
  58.   local valid, e
  59.  
  60.   -- use the authentication server if specified  
  61.   if authServer ~= "none" then
  62.     -- lookup the id of the authentication server
  63.     local id = rednet.lookup(authProtocol, authServer)
  64.     -- validate using the authentication server
  65.     if id ~= nil then
  66.       print("\nConnecting to authentication server...")
  67.       valid, e = validateCredentialsRemote(id, user, pass_
  68.     else  
  69.       print("\nAuthentication server not found. Using local credentials...")
  70.       valid, e = validateCredentialsLocal(user, pass)
  71.     end
  72.  
  73.   -- use local credentials if an authentication server is not specified
  74.   else
  75.     valid, e = validateCredentialsLocal(user, pass)
  76.   end    
  77.  
  78.   -- launch a shell if the credentials are valid
  79.   if valid == true then
  80.     term.clear()
  81.     term.setCursorPos(1, 1)
  82.     shell.run("/rom/programs/shell")
  83.   else
  84.     print(e.." Press any key to continue...")
  85.     read()
  86.   end
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement