Some python scrips for demonstrating chap protocol
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

chap-server.py 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python2
  2. import uuid
  3. import hashlib
  4. from xmlrpc.server import SimpleXMLRPCServer
  5. from xmlrpc.server import SimpleXMLRPCRequestHandler
  6. # Restrict to a particular path.
  7. class RequestHandler(SimpleXMLRPCRequestHandler):
  8. rpc_paths = ('/RPC2',)
  9. # Create server
  10. server = SimpleXMLRPCServer(("localhost", 8000),
  11. requestHandler=RequestHandler)
  12. server.register_introspection_functions()
  13. # Register an instance; all the methods of the instance are
  14. # published as XML-RPC methods
  15. class CHAP:
  16. test_password = 'Test123'
  17. # initializes class-instance and instance variables
  18. def __init__(self):
  19. self.key = '';
  20. self.authenticated = False;
  21. # tells the server to start the autentification process
  22. # and send the generated random salt
  23. def init(self):
  24. self.key = str( uuid.uuid4() )
  25. return self.key
  26. # checks if send hash is same as internally generated to validate if the correct
  27. # password was used
  28. def auth(self, password_hash):
  29. combined = CHAP.test_password + self.key
  30. passhash = hashlib.sha256( combined.encode( 'utf-8' ) ).hexdigest()
  31. print( passhash )
  32. print( password_hash )
  33. self.authenticated = passhash == password_hash
  34. return self.authenticated
  35. # a little method that refuses to say hi, if you
  36. # are not authenticated
  37. def hello(self):
  38. if ( self.authenticated ):
  39. return 'Hi, you are authenticated'
  40. else:
  41. return 'Sorry, please authenticate first'
  42. server.register_instance(CHAP())
  43. # Run the server's main loop
  44. server.serve_forever()