From 3af78e03116a0a7f8eba4d8b2909411bd37de1b8 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Sat, 24 Apr 2021 18:21:31 -0500 Subject: [PATCH] Finished login procedure. --- .gitignore | 1 + panera_sync/__init__.py | 0 panera_sync/fill_form.py | 9 +++++++++ panera_sync/ids.py | 0 panera_sync/login.py | 22 ++++++++++++++++++++++ panera_sync/main_loop.py | 19 +++++++++++++++++++ panera_sync/saml_check.py | 25 +++++++++++++++++++++++++ panera_sync/shifts.py | 0 panera_sync/user.py | 0 9 files changed, 76 insertions(+) create mode 100644 .gitignore create mode 100644 panera_sync/__init__.py create mode 100644 panera_sync/fill_form.py create mode 100644 panera_sync/ids.py create mode 100644 panera_sync/login.py create mode 100644 panera_sync/main_loop.py create mode 100644 panera_sync/saml_check.py create mode 100644 panera_sync/shifts.py create mode 100644 panera_sync/user.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/panera_sync/__init__.py b/panera_sync/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/panera_sync/fill_form.py b/panera_sync/fill_form.py new file mode 100644 index 0000000..3b51c04 --- /dev/null +++ b/panera_sync/fill_form.py @@ -0,0 +1,9 @@ +def get_form_fields(form): + form_action = form.action + data = dict() + inputs = form.xpath("//input") + for _input in inputs: + name = _input.name + if name is not None: + data[name] = _input.value + return form_action, data diff --git a/panera_sync/ids.py b/panera_sync/ids.py new file mode 100644 index 0000000..e69de29 diff --git a/panera_sync/login.py b/panera_sync/login.py new file mode 100644 index 0000000..3411279 --- /dev/null +++ b/panera_sync/login.py @@ -0,0 +1,22 @@ +from lxml.html import fromstring as lxml_from_string +from urllib.parse import urljoin + +from panera_sync.saml_check import saml_check +from panera_sync.fill_form import get_form_fields + + +def login_user(session, response, username, password): + parsed = lxml_from_string(response.text) + form = parsed.xpath("//form")[0] + post_url, to_send = get_form_fields(form) + to_send["username"] = username + to_send["password"] = password + print(to_send) + post_url = urljoin(response.url, post_url) + print(post_url) + headers = { + "Host": "iso3.panerabread.com", + "Referer": "https://pantry.panerabread.com/", + } + r = session.post(post_url, data=to_send, headers=headers) + return saml_check(session, r) diff --git a/panera_sync/main_loop.py b/panera_sync/main_loop.py new file mode 100644 index 0000000..72097e6 --- /dev/null +++ b/panera_sync/main_loop.py @@ -0,0 +1,19 @@ +import lxml +import requests + +from panera_sync.saml_check import saml_check +from panera_sync.login import login_user + +PANERA_URL = "https://pantry.panerabread.com" + + +def get_login_page(session: requests.Session): + landing = session.get(PANERA_URL) + return saml_check(session, landing) + + +def login(username, password): + session = requests.Session() + login_page = get_login_page(session) + login_user(session, login_page, username, password) + return session diff --git a/panera_sync/saml_check.py b/panera_sync/saml_check.py new file mode 100644 index 0000000..f26bc16 --- /dev/null +++ b/panera_sync/saml_check.py @@ -0,0 +1,25 @@ +from urllib.parse import urlparse +from lxml.html import fromstring as lxml_from_string + +from panera_sync.fill_form import get_form_fields + + +def saml_submit(session, response): + origin_url = response.url + url_parse_result = urlparse(origin_url) + headers = { + "Origin": f"{url_parse_result.scheme}://{url_parse_result.netloc}", + "Referer": origin_url, + } + parsed = lxml_from_string(response.text) + form = parsed.xpath("//form")[0] + post_url, to_submit = get_form_fields(form) + return session.post(post_url, data=to_submit, headers=headers) + + +def saml_check(session, response, response_text=None): + parsed = lxml_from_string(response.text) + witness = parsed.xpath('//form//input[contains(@name,"SAML")]') + print(witness) + if len(witness) != 0: + return saml_submit(session, response) diff --git a/panera_sync/shifts.py b/panera_sync/shifts.py new file mode 100644 index 0000000..e69de29 diff --git a/panera_sync/user.py b/panera_sync/user.py new file mode 100644 index 0000000..e69de29