Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Get Intial CSRF
- cred_a = self.do_request(self.module, self.endpoint, method="GET")
- cookie_a = cred_a.headers.get('Set-Cookie').split(';')
- init_csrftoken = None
- csrftoken = None
- for c in cookie_a:
- if "csrftoken" in c:
- init_csrftoken = c.replace("csrftoken=", "")
- init_csrftoken = init_csrftoken.rstrip("\r\n")
- break
- # Make Header Dictionary with initial CSRF
- header = {'csrftoken': init_csrftoken, 'X-CSRFToken': init_csrftoken,
- 'Content-type': 'application/x-www-form-urlencoded'}
- # Endpoint to get final authentication header
- login_endpoint = self.endpoint + "/login"
- # Raw data with username and password
- dat = 'USERNAME={}&PASSWORD={}'.format(module.params['stacki_user'], module.params['stacki_password'])
- # Get Final CSRF and Session ID
- login_req = self.do_request(self.module, login_endpoint, headers=header,
- payload=dat, method="POST")
- cookie_f = login_req.headers.get('Set-Cookie').split(';')
- for c in cookie_f:
- if "csrftoken" in c:
- csrftoken = c.replace("csrftoken=", "")
- csrftoken = init_csrftoken.rstrip("\r\n")
- if "sessionid" in c:
- sessionid = c.replace("sessionid=", "")
- sessionid = sessionid.rstrip("\r\n")
- auth_creds.update(CSRFTOKEN=csrftoken, SESSIONID=sessionid)
- self.header = {'csrftoken': auth_creds['CSRFTOKEN'],
- "library/stackime.py" 366L, 12059C written 159,8 41%
- header = {'csrftoken': init_csrftoken, 'X-CSRFToken': init_csrftoken,
- 'Content-type': 'application/x-www-form-urlencoded'}
- # Endpoint to get final authentication header
- login_endpoint = self.endpoint + "/login"
- # Raw data with username and password
- dat = 'USERNAME={}&PASSWORD={}'.format(module.params['stacki_user'], module.params['stacki_password'])
- dat = self.module.jsonify(auth_creds)
- # Get Final CSRF and Session ID
- login_req = self.do_request(self.module, login_endpoint, headers=header,
- payload=dat, method="POST")
- cookie_f = login_req.headers.get('Set-Cookie').split(';')
- for c in cookie_f:
- if "csrftoken" in c:
- csrftoken = c.replace("csrftoken=", "")
- csrftoken = init_csrftoken.rstrip("\r\n")
- if "sessionid" in c:
- sessionid = c.replace("sessionid=", "")
- sessionid = sessionid.rstrip("\r\n")
- auth_creds.update(CSRFTOKEN=csrftoken, SESSIONID=sessionid)
- self.header = {'csrftoken': auth_creds['CSRFTOKEN'],
- 'X-CSRFToken': auth_creds['CSRFTOKEN'],
- 'sessionid': auth_creds['SESSIONID'],
- 'Content-type': 'application/json'}
- def do_request(self, module, url, payload=None, headers=None, method=None):
- res, info = fetch_url(module, url, data=payload, headers=headers, method=method)
- if info['status'] != 200:
- print(info)
- exit()
- self.module.fail_json(changed=False, result=info['msg'])
- return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement