Commit 21514c83 authored by abuddenberg's avatar abuddenberg
Browse files

Added some extra goodness to spoof Drupal's login. Removed application/json...

Added some extra goodness to spoof Drupal's login. Removed application/json from login headers since Drupal doesn't send cookies with it
parent 1bde1223
...@@ -3,6 +3,7 @@ __author__ = 'abuddenberg' ...@@ -3,6 +3,7 @@ __author__ = 'abuddenberg'
import requests import requests
import pickle import pickle
import json import json
from bs4 import BeautifulSoup
def http_resp(fn): def http_resp(fn):
def wrapped(*args, **kwargs): def wrapped(*args, **kwargs):
...@@ -19,7 +20,7 @@ class Nca3Client(object): ...@@ -19,7 +20,7 @@ class Nca3Client(object):
self.base_url = url self.base_url = url
self.s = requests.Session() self.s = requests.Session()
self.s.auth = (http_basic_user, http_basic_pass) self.s.auth = (http_basic_user, http_basic_pass)
self.s.headers = {'content-type': 'application/json'} # self.s.headers = {'content-type': 'application/json'}
self.drupal_user = username self.drupal_user = username
self.drupal_pass = password self.drupal_pass = password
...@@ -31,16 +32,21 @@ class Nca3Client(object): ...@@ -31,16 +32,21 @@ class Nca3Client(object):
def do_login(self): def do_login(self):
url = '{b}/user'.format(b=self.base_url) url = '{b}/user'.format(b=self.base_url)
form = BeautifulSoup(self.s.get(url).text).find('form', id='user-login')
form_build_id = form.find_all('input', attrs={'name': 'form_build_id'})
resp = self.s.post( resp = self.s.post(
url, url,
data={ data={
'name': self.drupal_user, 'name': self.drupal_user,
'pass': self.drupal_pass, 'pass': self.drupal_pass,
'form_id': 'user_login', 'form_id': 'user_login',
'form_build_id': form_build_id,
'op': 'Log in' 'op': 'Log in'
}, },
allow_redirects=False allow_redirects=False
) )
pickle.dump(self.s.cookies, open(self.cookie_jar, 'wb')) pickle.dump(self.s.cookies, open(self.cookie_jar, 'wb'))
return resp return resp
...@@ -49,18 +55,19 @@ class Nca3Client(object): ...@@ -49,18 +55,19 @@ class Nca3Client(object):
def get_all_captions(self): def get_all_captions(self):
url = '{b}/gcis/figure-table-captions'.format(b=self.base_url) url = '{b}/gcis/figure-table-captions'.format(b=self.base_url)
resp = self.s.get(url, verify=False, cookies=self.s.cookies) resp = self.s.get(url, verify=False, headers={'content-type': 'application/json'}, cookies=self.s.cookies)
return resp return resp
def get_figure(self, nid): def get_figure(self, nid):
url = '{b}/gcis/node/{nid}'.format(b=self.base_url, nid=nid) url = '{b}/gcis/node/{nid}'.format(b=self.base_url, nid=nid)
return self.s.get(url, verify=False, cookies=self.s.cookies).json() return self.s.get(url, verify=False, headers={'content-type': 'application/json'}, cookies=self.s.cookies).json()
def update_figure(self, nid, figure_frag): def update_figure(self, nid, figure_frag):
url = '{b}/gcis/node/{nid}'.format(b=self.base_url, nid=nid) url = '{b}/gcis/node/{nid}'.format(b=self.base_url, nid=nid)
token_url = '{b}/services/session/token'.format(b=self.base_url) token_url = '{b}/services/session/token'.format(b=self.base_url)
token = self.s.get(token_url, verify=False, cookies=self.s.cookies).text token = self.s.get(token_url, verify=False, cookies=self.s.cookies).text
return self.s.put(url, data=json.dumps(figure_frag), verify=False, cookies=self.s.cookies, headers={'X-CSRF-Token': token}) return self.s.put(url, data=json.dumps(figure_frag), verify=False, cookies=self.s.cookies, headers={'X-CSRF-Token': token, 'content-type': 'application/json'})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment