| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- #!/usr/bin/env python2
-
- import uuid
- import hashlib
- from xmlrpc.server import SimpleXMLRPCServer
- from xmlrpc.server import SimpleXMLRPCRequestHandler
-
- # Restrict to a particular path.
- class RequestHandler(SimpleXMLRPCRequestHandler):
- rpc_paths = ('/RPC2',)
-
- # Create server
- server = SimpleXMLRPCServer(("localhost", 8000),
- requestHandler=RequestHandler)
- server.register_introspection_functions()
-
-
- # Register an instance; all the methods of the instance are
- # published as XML-RPC methods
- class CHAP:
- test_password = 'Test123'
- # initializes class-instance and instance variables
- def __init__(self):
- self.key = '';
- self.authenticated = False;
-
- # tells the server to start the autentification process
- # and send the generated random salt
- def init(self):
- self.key = str( uuid.uuid4() )
- return self.key
-
- # checks if send hash is same as internally generated to validate if the correct
- # password was used
- def auth(self, password_hash):
- combined = CHAP.test_password + self.key
- passhash = hashlib.sha256( combined.encode( 'utf-8' ) ).hexdigest()
- print( passhash )
- print( password_hash )
- self.authenticated = passhash == password_hash
- return self.authenticated
-
- # a little method that refuses to say hi, if you
- # are not authenticated
- def hello(self):
- if ( self.authenticated ):
- return 'Hi, you are authenticated'
- else:
- return 'Sorry, please authenticate first'
- server.register_instance(CHAP())
- # Run the server's main loop
- server.serve_forever()
|