From a624475349bf71ada1f2410eb8cd05fb0f3bface Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Thu, 11 May 2023 19:47:49 +0200 Subject: [PATCH] Add files via upload --- example_show_accessable_pages.py | 42 +++++++++++++++++++ example_surf_to_pages.py | 47 +++++++++++++++++++++ get_tree/get_layout_url.py | 18 ++++++++ get_tree/get_tree.py | 41 ++++++++++++++++++ login/close_login_popup.json | 3 ++ login/close_login_popup.py | 26 ++++++++++++ login/login.json | 7 ++++ login/login.py | 72 ++++++++++++++++++++++++++++++++ username.json | 3 ++ 9 files changed, 259 insertions(+) create mode 100644 example_show_accessable_pages.py create mode 100644 example_surf_to_pages.py create mode 100644 get_tree/get_layout_url.py create mode 100644 get_tree/get_tree.py create mode 100644 login/close_login_popup.json create mode 100644 login/close_login_popup.py create mode 100644 login/login.json create mode 100644 login/login.py create mode 100644 username.json diff --git a/example_show_accessable_pages.py b/example_show_accessable_pages.py new file mode 100644 index 0000000..9f2f626 --- /dev/null +++ b/example_show_accessable_pages.py @@ -0,0 +1,42 @@ +import json +from login.login import login +from login.close_login_popup import close_login_popup + +from get_tree.get_tree import get_tree + +from getpass import getpass + +from selenium import webdriver +from selenium.webdriver.firefox.service import Service as FirefoxService +from webdriver_manager.firefox import GeckoDriverManager + +list_of_page_ids: list[int] = [59451, 59246] + +base_url: str = "https://www.uni-bremen.de" + +zfn_password: str = getpass() + +with open("username.json", "r") as file: + json_dict = json.load(file) +zfn_user: str = json_dict["zfn_user"] + +driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install())) + +# Login +if login(driver, base_url, zfn_user, zfn_password) is False: + print("Login failed") + exit(1) + +# Get rid of the popup +close_login_popup(driver) + +# Get the protected URL to the pages +page_list = get_tree(driver, base_url) + +for page_entry in page_list: + print(page_entry) + print("---") + +print("Close shop") +driver.close() +driver.quit() diff --git a/example_surf_to_pages.py b/example_surf_to_pages.py new file mode 100644 index 0000000..0bfa5e1 --- /dev/null +++ b/example_surf_to_pages.py @@ -0,0 +1,47 @@ +import json +from login.login import login +from login.close_login_popup import close_login_popup + +from get_tree.get_layout_url import get_layout_url + +from getpass import getpass + +from selenium import webdriver +from selenium.webdriver.firefox.service import Service as FirefoxService +from webdriver_manager.firefox import GeckoDriverManager + +import time + +list_of_page_ids: list[int] = [59451, 59246] + +base_url: str = "https://www.uni-bremen.de" + +zfn_password: str = getpass() + +with open("username.json", "r") as file: + json_dict = json.load(file) +zfn_user: str = json_dict["zfn_user"] + +driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install())) + +# Login +if login(driver, base_url, zfn_user, zfn_password) is False: + print("Login failed") + exit(1) + +# Get rid of the popup +close_login_popup(driver) + +# Get the protected URL to the pages +page_url_base = get_layout_url(driver, base_url) + +for page_id in list_of_page_ids: + print(f"Go to page {int(page_id)}") + page_url: str = page_url_base + f"&id={int(page_id)}" + driver.get(page_url) + print("Wait for 5 sec") + time.sleep(5) + +print("Close shop") +driver.close() +driver.quit() diff --git a/get_tree/get_layout_url.py b/get_tree/get_layout_url.py new file mode 100644 index 0000000..a2fc675 --- /dev/null +++ b/get_tree/get_layout_url.py @@ -0,0 +1,18 @@ +from selenium import webdriver + + +def get_layout_url( + driver: webdriver.firefox.webdriver.WebDriver, + base_url: str, +) -> str: + data_elements = str(driver.page_source).split('"') + + lost_and_found = [] + for element in data_elements: + if str(element).find("index.php?route=%2Fweb%2Flayout%2F&token=") != -1: + lost_and_found.append(element) + + assert len(lost_and_found) > 0 + page_url = base_url + str(lost_and_found[0]).replace("\\", "") + + return page_url diff --git a/get_tree/get_tree.py b/get_tree/get_tree.py new file mode 100644 index 0000000..afc2175 --- /dev/null +++ b/get_tree/get_tree.py @@ -0,0 +1,41 @@ +import json +from selenium import webdriver + + +def get_tree( + driver: webdriver.firefox.webdriver.WebDriver, + base_url: str, +) -> list: + data_elements = str(driver.page_source).split('"') + + lost_and_found = [] + for element in data_elements: + if ( + str(element).find( + "index.php?route=%2Fajax%2Fpage%2Ftree%2FfetchData&token=" + ) + != -1 + ): + lost_and_found.append(element) + + assert len(lost_and_found) == 1 + ajax_url = base_url + str(lost_and_found[0]).replace("\\", "") + + backup_url = driver.current_url + driver.get(ajax_url) + + data = str(driver.page_source) + data_lines = data.split('
') + + assert len(data_lines) == 2 + data = data_lines[1] + + data_lines = data.split("
") + assert len(data_lines) > 0 + data = data_lines[0] + + page_dict = json.loads(data) + + driver.get(backup_url) + + return page_dict diff --git a/login/close_login_popup.json b/login/close_login_popup.json new file mode 100644 index 0000000..4f577a1 --- /dev/null +++ b/login/close_login_popup.json @@ -0,0 +1,3 @@ +{ + "button_class": "t3js-modal-close close" +} \ No newline at end of file diff --git a/login/close_login_popup.py b/login/close_login_popup.py new file mode 100644 index 0000000..78367fa --- /dev/null +++ b/login/close_login_popup.py @@ -0,0 +1,26 @@ +import json +import os +import time + +from selenium import webdriver +from selenium.webdriver.common.by import By + + +def close_login_popup(driver: webdriver.firefox.webdriver.WebDriver): + string_json_path: str = os.path.join("login", "close_login_popup.json") + + time.sleep(1) + + with open(string_json_path, "r") as file: + string_dict = json.load(file) + + evil_element_found: bool = True + while evil_element_found is True: + found_element_list = driver.find_elements(By.TAG_NAME, "button") + evil_element_found = False + for found_element in found_element_list: + if found_element.get_dom_attribute("class") == string_dict["button_class"]: + evil_element_found = True + found_element.click() + time.sleep(1) + break diff --git a/login/login.json b/login/login.json new file mode 100644 index 0000000..ac4331e --- /dev/null +++ b/login/login.json @@ -0,0 +1,7 @@ +{ + "login_url": "/typo3/index.php?route=%2Flogin", + "user_name_id": "t3-username", + "user_password_id": "t3-password", + "user_button_id": "t3-login-submit", + "bad_url": "/typo3/index.php?loginProvider=" +} \ No newline at end of file diff --git a/login/login.py b/login/login.py new file mode 100644 index 0000000..21ce293 --- /dev/null +++ b/login/login.py @@ -0,0 +1,72 @@ +import json +import os +import time + +from selenium import webdriver +from selenium.webdriver.common.by import By + + +def login( + driver: webdriver.firefox.webdriver.WebDriver, + base_url: str, + zfn_user: str, + zfn_password: str, +) -> bool: + assert len(base_url) > 0 + assert len(zfn_user) > 0 + assert len(zfn_password) > 0 + + string_json_path: str = os.path.join("login", "login.json") + + with open(string_json_path, "r") as file: + string_dict = json.load(file) + + login_url: str = base_url + string_dict["login_url"] + + # Login + driver.get(login_url) + print_new_line: bool = False + while driver.current_url != login_url: + print(".", end="") + time.sleep(1) + print_new_line = True + + if print_new_line is True: + print() + + login_name_list = driver.find_elements(By.ID, string_dict["user_name_id"]) + login_password_list = driver.find_elements(By.ID, string_dict["user_password_id"]) + login_button_list = driver.find_elements(By.ID, string_dict["user_button_id"]) + + if ( + (len(login_name_list) != 1) + and (len(login_password_list) != 1) + and (len(login_button_list) != 1) + ): + return False + + login_name = login_name_list[0] + login_password = login_password_list[0] + login_button = login_button_list[0] + + while (login_name.get_attribute("value") != zfn_user) or ( + login_password.get_attribute("value") != zfn_password + ): + login_name.clear() + login_password.clear() + login_name.send_keys(zfn_user) + login_password.send_keys(zfn_password) + + login_button.click() + while driver.current_url == login_url: + print(".", end="") + time.sleep(1) + print_new_line = True + + if print_new_line is True: + print() + + if str(driver.current_url).startswith(base_url + string_dict["bad_url"]) is True: + return False + + return True diff --git a/username.json b/username.json new file mode 100644 index 0000000..d71e34e --- /dev/null +++ b/username.json @@ -0,0 +1,3 @@ +{ + "zfn_user": "myusername" +} \ No newline at end of file