Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. # Get Intial CSRF
  2. cred_a = self.do_request(self.module, self.endpoint, method="GET")
  3. cookie_a = cred_a.headers.get('Set-Cookie').split(';')
  4. init_csrftoken = None
  5. csrftoken = None
  6. for c in cookie_a:
  7. if "csrftoken" in c:
  8. init_csrftoken = c.replace("csrftoken=", "")
  9. init_csrftoken = init_csrftoken.rstrip("\r\n")
  10. break
  11.  
  12. # Make Header Dictionary with initial CSRF
  13. header = {'csrftoken': init_csrftoken, 'X-CSRFToken': init_csrftoken,
  14. 'Content-type': 'application/x-www-form-urlencoded'}
  15.  
  16. # Endpoint to get final authentication header
  17. login_endpoint = self.endpoint + "/login"
  18.  
  19. # Raw data with username and password
  20. dat = 'USERNAME={}&PASSWORD={}'.format(module.params['stacki_user'], module.params['stacki_password'])
  21.  
  22. # Get Final CSRF and Session ID
  23. login_req = self.do_request(self.module, login_endpoint, headers=header,
  24. payload=dat, method="POST")
  25.  
  26. cookie_f = login_req.headers.get('Set-Cookie').split(';')
  27.  
  28. for c in cookie_f:
  29. if "csrftoken" in c:
  30. csrftoken = c.replace("csrftoken=", "")
  31. csrftoken = init_csrftoken.rstrip("\r\n")
  32. if "sessionid" in c:
  33. sessionid = c.replace("sessionid=", "")
  34. sessionid = sessionid.rstrip("\r\n")
  35.  
  36. auth_creds.update(CSRFTOKEN=csrftoken, SESSIONID=sessionid)
  37.  
  38. self.header = {'csrftoken': auth_creds['CSRFTOKEN'],
  39. "library/stackime.py" 366L, 12059C written 159,8 41%
  40. header = {'csrftoken': init_csrftoken, 'X-CSRFToken': init_csrftoken,
  41. 'Content-type': 'application/x-www-form-urlencoded'}
  42.  
  43. # Endpoint to get final authentication header
  44. login_endpoint = self.endpoint + "/login"
  45.  
  46. # Raw data with username and password
  47. dat = 'USERNAME={}&PASSWORD={}'.format(module.params['stacki_user'], module.params['stacki_password'])
  48. dat = self.module.jsonify(auth_creds)
  49. # Get Final CSRF and Session ID
  50. login_req = self.do_request(self.module, login_endpoint, headers=header,
  51. payload=dat, method="POST")
  52.  
  53. cookie_f = login_req.headers.get('Set-Cookie').split(';')
  54.  
  55. for c in cookie_f:
  56. if "csrftoken" in c:
  57. csrftoken = c.replace("csrftoken=", "")
  58. csrftoken = init_csrftoken.rstrip("\r\n")
  59. if "sessionid" in c:
  60. sessionid = c.replace("sessionid=", "")
  61. sessionid = sessionid.rstrip("\r\n")
  62.  
  63. auth_creds.update(CSRFTOKEN=csrftoken, SESSIONID=sessionid)
  64.  
  65. self.header = {'csrftoken': auth_creds['CSRFTOKEN'],
  66. 'X-CSRFToken': auth_creds['CSRFTOKEN'],
  67. 'sessionid': auth_creds['SESSIONID'],
  68. 'Content-type': 'application/json'}
  69.  
  70.  
  71. def do_request(self, module, url, payload=None, headers=None, method=None):
  72. res, info = fetch_url(module, url, data=payload, headers=headers, method=method)
  73.  
  74. if info['status'] != 200:
  75. print(info)
  76. exit()
  77. self.module.fail_json(changed=False, result=info['msg'])
  78.  
  79. return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement