SHARE
TWEET

Untitled

a guest Dec 31st, 2018 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class HTTPAuthValidator(object):
  2.     MESSAGE = "Validating user: %s Password: %s for %s"
  3.  
  4.     def __init__(self, auth=None):
  5.         self.session = Session()
  6.         self.auth = auth or HTTPBasicAuth
  7.         self.url = ""
  8.  
  9.     def validate(self, url, credentials):
  10.         self.url = url
  11.         p = Pool(cpu_count())
  12.         sc = p.map(self, credentials)
  13.         return [result for result in sc]
  14.  
  15.     def __call__(self, auth):
  16.         username, password = auth
  17.         info(self.MESSAGE % (username, password, self.url))
  18.         result = self.session.get(self.url, auth=self.auth(username, password))
  19.         if result.status_code == 200:
  20.             return username, password
  21.  
  22.  
  23. auth_validators = (
  24.     ("Tomcat", HTTPAuthValidator),
  25. )
  26.  
  27.  
  28. def validator_factory(module):
  29.     """
  30.     Validator lookup
  31.     :param module:
  32.     :return: Instance of the validator
  33.     """
  34.     found = next(
  35.         validator[1] for validator in auth_validators if validator[0] == module)
  36.     return found()
  37.  
  38.  
  39.  
  40. class TestValidators(unittest.TestCase):
  41.  
  42.     @patch.object(Session, "send")
  43.     def test_auth_validator(self, mock_get):
  44.         expected_text = "FAIL - Unknown command "
  45.         mock_response = Mock()
  46.         mock_response.text = expected_text
  47.         mock_response.headers = [{"content-encoding": "text/plain"}]
  48.         mock_response.status_code = 200
  49.         mock_get.return_value = mock_response
  50.         url = "http://127.0.0.1:8080/manager/"
  51.  
  52.         expected_tokens = "Basic %s" % b64encode(
  53.             "%s:%s" % ("tomcat", "tomcat"))
  54.  
  55.         tomcat_validator = validator_factory("Tomcat")
  56.         creds = [
  57.             ("tomcat", "tomcat"),
  58.             ("admin", "admin"),
  59.             ("tomcat", "s3cret"),
  60.         ]
  61.  
  62.         results = tomcat_validator.validate(url, creds)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top