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 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.keys = {}
  20. self.authenticated = {}
  21. # tells the server to start the autentification process
  22. # and send the generated random salt
  23. def init( self ):
  24. session = str( uuid.uuid4() )
  25. key = str( uuid.uuid4() )
  26. while session in self.keys:
  27. session = str( uuid.uuid4() )
  28. self.keys[ session ] = key
  29. return [ session, key ]
  30. # checks if send hash is same as internally generated to validate if the correct
  31. # password was used
  32. def auth( self, password_hash ):
  33. combined = CHAP.test_password + self.key
  34. passhash = hashlib.sha256( combined.encode( 'utf-8' ) ).hexdigest()
  35. self.authenticated = passhash == password_hash
  36. return self.authenticated
  37. # adds functionality for users to log them selfes off, but also need the
  38. # the password_hash to ensure, that nobody else logs you off
  39. def logout( self, session, password_hash ):
  40. if session in self.authenticated:
  41. if self.auth( password_hash ):
  42. self.authenticated[ session ] = False
  43. return self.authenticated.get( session )
  44. # a little method that refuses to say hi, if you
  45. # are not authenticated
  46. def hello( self ):
  47. if ( self.authenticated ):
  48. return 'Hi, you are authenticated'
  49. else:
  50. return 'Sorry, please authenticate first'
  51. server.register_instance(CHAP())
  52. # Run the server's main loop
  53. server.serve_forever()