Some python scrips for demonstrating chap protocol
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

chap-server.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. self.authenticated = passhash == password_hash
  32. return self.authenticated
  33. # a little method that refuses to say hi, if you
  34. # are not authenticated
  35. def hello(self):
  36. if ( self.authenticated ):
  37. return 'Hi, you are authenticated'
  38. else:
  39. return 'Sorry, please authenticate first'
  40. server.register_instance(CHAP())
  41. # Run the server's main loop
  42. server.serve_forever()