Some python scrips for demonstrating chap protocol
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

chap-server.py 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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, session, password_hash ):
  33. if session in self.keys:
  34. combined = CHAP.test_password + self.keys[ session ]
  35. passhash = hashlib.sha256( combined.encode( 'utf-8' ) ).hexdigest()
  36. self.authenticated[ session ] = passhash == password_hash
  37. return self.authenticated[ session ] == True
  38. else:
  39. return False
  40. # adds functionality for users to log them selfes off, but also need the
  41. # the password_hash to ensure, that nobody else logs you off
  42. def logout( self, session, password_hash ):
  43. if self.auth( session, password_hash ):
  44. del self.authenticated[ session ]
  45. del self.keys[ session ]
  46. return self.authenticated.get( session ) != True
  47. # a little method that refuses to say hi, if you
  48. # are not authenticated
  49. def hello( self, session ):
  50. if ( self.authenticated.get( session ) == True ):
  51. return 'Hi, you are authenticated'
  52. else:
  53. return 'Sorry, please authenticate first'
  54. server.register_instance(CHAP())
  55. # Run the server's main loop
  56. server.serve_forever()