Advertisement
tobast

auth_checker

Jun 20th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1. --[[
  2.     Auth_checker by Théophile Bastian
  3.     Vérifie une identité auprès du serveur d'authentification.
  4.  
  5.     Dépendances
  6.     - sha256
  7.     - slot_sig
  8. --]]
  9.  
  10. ------------- PARAMETERS --------------
  11. ID_READER_SIDE = 'left'
  12. MODEM_SIDE='top'
  13.  
  14. function onAllowed()
  15.     redstone.setOutput('bottom',true)
  16.     sleep(3)
  17.     redstone.setOutput('bottom',false)
  18. end
  19. ------------- END PARAMS --------------
  20.  
  21. os.loadAPI('/lib/sha256')
  22. os.loadAPI('/lib/slot_sig')
  23.  
  24. local allowed = { users = {}, groups = {}}
  25. local server = -1
  26. local nextTimer = nil
  27.  
  28. function getHash()
  29.     path = '/'..disk.getMountPath(ID_READER_SIDE)..'/key'
  30.  
  31.     handle = fs.open(path,'r')
  32.     if(handle == nil) then
  33.         return ""
  34.     end
  35.  
  36.     key = handle.readAll()
  37.     handle.close()
  38.  
  39.     hash = sha256.sha256(key)
  40.     return hash
  41. end
  42.  
  43. function onRednet(data)
  44.     sender = data[1]
  45.  
  46.     mess=nil
  47.     if(type(data[2]) == 'string') then
  48.         mess = textutils.unserialize(mess)
  49.     else
  50.         mess = data[2]
  51.     end
  52.  
  53.     if(mess.type == 'authreply') then
  54.         if(mess.user == nil) then
  55.             return
  56.         end
  57.  
  58.         if(allowed.users[mess.user]) then
  59.             onAllowed()
  60.             return
  61.         end
  62.  
  63.         for gid=1,#(mess.groups) do
  64.             if(allowed.groups[mess.groups[gid]]) then
  65.                 onAllowed()
  66.                 return
  67.             end
  68.         end
  69.     end
  70. end
  71.  
  72. function requestAuth(hash)
  73.     if(server == nil) then -- server still not found
  74.         onAllowed() -- Allow login, to allow anyone in
  75.     end
  76.  
  77.     mess = {}
  78.     mess['reqtype'] = 'auth'
  79.     mess['hash'] = hash
  80.     rednet.send(server, textutils.serialize(mess))
  81. end
  82.  
  83. function onDiskInserted(data)
  84.     side=data[1]
  85.     if side == ID_READER_SIDE then
  86.         hash = getHash()
  87.         disk.eject(ID_READER_SIDE)
  88.         requestAuth(hash)
  89.     end
  90. end
  91.  
  92. function onTimer(data)
  93.     if(data[1] == nextTimer and server == nil) then
  94.         server = rednet.lookup('auth','server')
  95.         if(server == nil) then
  96.             nextTimer = os.startTimer(5)
  97.         end
  98.     end
  99. end
  100.  
  101. function main()
  102.     rednet.open(MODEM_SIDE)
  103.     server = rednet.lookup('auth','server')
  104.  
  105.     if(server == nil) then
  106.         nextTimer = os.startTimer(5)
  107.     end
  108.    
  109.     slot_sig.connectSlot('disk', onDiskInserted)
  110.     slot_sig.connectSlot('rednet_message', onRednet)
  111.     slot_sig.connectSlot('timer',onTimer)
  112.  
  113.     slot_sig.run()
  114. end
  115. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement