Add nonce support.

This commit is contained in:
redxef 2023-11-14 15:40:53 +01:00
parent 1e2137cd90
commit 437ad585ac
Signed by: redxef
GPG key ID: 7DAC3AA211CBD921
2 changed files with 12 additions and 0 deletions

View file

@ -8,6 +8,7 @@ import datetime
import logging import logging
from schema import Use, Schema from schema import Use, Schema
from bs4 import BeautifulSoup
from . import utils from . import utils
from .abc import Sink, Adapter from .abc import Sink, Adapter
@ -151,7 +152,16 @@ class Wordpress(Sink, Adapter):
) )
return final_dict return final_dict
def get_nonce(self):
r = self.session.get(
f'{self.base_url}/wp-admin/admin.php?page=wpbs-calendars&subpage=edit-calendar&calendar_id=1',
)
soup = BeautifulSoup(r.text, 'html.parser')
nonce = soup.find_all('input', {'id': 'wpbs_token'})[0]
return nonce['value']
def post_events(self, events, start: datetime.datetime | None=None, until: datetime.timedelta | None=None): def post_events(self, events, start: datetime.datetime | None=None, until: datetime.timedelta | None=None):
metadata = self.calendar_metadata.to_dict() metadata = self.calendar_metadata.to_dict()
data = self._generate_data(events, start=start, until=until) data = self._generate_data(events, start=start, until=until)
update_request = self.session.post( update_request = self.session.post(
@ -161,6 +171,7 @@ class Wordpress(Sink, Adapter):
'action': 'wpbs_save_calendar_data', 'action': 'wpbs_save_calendar_data',
'form_data': urllib.parse.urlencode(metadata), 'form_data': urllib.parse.urlencode(metadata),
'calendar_data': json.dumps(data), 'calendar_data': json.dumps(data),
'wpbs_token': self.get_nonce(),
}, },
) )
r = 'wpbs_message=calendar_update_success' in update_request.text r = 'wpbs_message=calendar_update_success' in update_request.text

View file

@ -4,3 +4,4 @@ google-api-python-client
google-auth-httplib2 google-auth-httplib2
google-auth-oauthlib google-auth-oauthlib
click click
bs4