connect.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. """Openconnect-Microsoft-login connection helpers."""
  2. import logging
  3. import time
  4. from dataclasses import dataclass
  5. from typing import Optional
  6. from urllib.parse import urlparse
  7. from otppy import OTP
  8. from selenium import webdriver
  9. from selenium.common.exceptions import ElementClickInterceptedException
  10. from selenium.common.exceptions import ElementNotInteractableException
  11. from selenium.common.exceptions import NoSuchElementException
  12. from selenium.common.exceptions import StaleElementReferenceException
  13. from selenium.common.exceptions import TimeoutException
  14. from selenium.webdriver.common.by import By
  15. from selenium.webdriver.firefox.options import Options as FirefoxOptions
  16. from selenium.webdriver.support import expected_conditions as EC
  17. from selenium.webdriver.support.ui import WebDriverWait
  18. USERNAME_INPUT_NAME = "loginfmt"
  19. PASSWORD_INPUT_NAME = "passwd"
  20. MFA_INPUT_NAME = "otc"
  21. CONTINUE_BUTTON_ID = "idSIButton9"
  22. MFA_CONTINUE_BUTTON_ID = "idSubmit_SAOTCC_Continue"
  23. MFA_ERROR_TEXT_ID = "idSpan_SAOTCC_Error_OTC"
  24. VPN_INSTALL_PAGE_EXCLUSIVE_ELEMENT_ID = "provisioning_action_label"
  25. MAX_RETRY_DOMAIN_CHECK = 16
  26. MFA_MAX_RETRY_COUNT = 3
  27. ELEMENT_CHECK_DELAY = 0.5
  28. DOMAIN_CHECK_DELAY = 0.5
  29. LOGGER = logging.getLogger("OCMA")
  30. LOGGER.setLevel(logging.INFO)
  31. @dataclass
  32. class VPNCookie:
  33. """VPN cookie data container."""
  34. domain: str
  35. cookie: str
  36. def login(
  37. username: str,
  38. password: str,
  39. mfa_secret: Optional[str] = None,
  40. vpn_site: str = "https://vpn.fhnw.ch",
  41. headless: bool = True,
  42. log_messages: bool = False,
  43. ) -> VPNCookie:
  44. """
  45. Log in to the vpn.
  46. Parameters
  47. ----------
  48. username : str
  49. Microsoft account username.
  50. password : str
  51. Microsoft account password.
  52. mfa_secret : Optional[str], optional
  53. Multifactor secret, by default None
  54. vpn_site : _type_, optional
  55. VPN site to log into, by default "https://vpn.fhnw.ch"
  56. headless : bool, optional
  57. If the browser should be run in headless mode, by default True
  58. log_messages : bool, optional
  59. If messages should be logged to the console, by default False
  60. Returns
  61. -------
  62. VPNCookie
  63. Cookie to log into the openconnect VPN.
  64. Raises
  65. ------
  66. ValueError
  67. If anything went sideways during the login.
  68. """
  69. if log_messages:
  70. formatter = logging.Formatter(
  71. "%(asctime)s: %(levelname)s - %(name)s - %(message)s"
  72. )
  73. fh_s = logging.StreamHandler()
  74. fh_s.setLevel(logging.INFO)
  75. fh_s.setFormatter(formatter)
  76. LOGGER.addHandler(fh_s)
  77. LOGGER.info("Starting")
  78. options = FirefoxOptions()
  79. if headless:
  80. LOGGER.info("Running in headless mode")
  81. options.add_argument("--headless")
  82. driver = webdriver.Firefox(options=options)
  83. driver.get(vpn_site)
  84. retries = MAX_RETRY_DOMAIN_CHECK
  85. while not _is_on_ms_login_page(driver) and retries > 0:
  86. retries -= 1
  87. time.sleep(DOMAIN_CHECK_DELAY)
  88. if retries < 0:
  89. raise ValueError(
  90. f"We never reached the MS login page! Currently on {driver.current_url}"
  91. )
  92. _fill_login(driver, username, password)
  93. _fill_mfa(driver, mfa_secret)
  94. _confirm_stay_signed_in(driver)
  95. _is_on_install_page(driver)
  96. return _get_webvpn_cookie(driver)
  97. def _fill_login(driver: webdriver.Firefox, username: str, password: str) -> None:
  98. LOGGER.info("Filling in login information")
  99. _check_on_ms_login_page(driver)
  100. if not _find_element(driver, By.NAME, USERNAME_INPUT_NAME):
  101. raise ValueError("Could not find login page!")
  102. if not _element_interactable(driver, By.NAME, USERNAME_INPUT_NAME):
  103. raise ValueError("Could not find username field!")
  104. driver.find_element(By.NAME, USERNAME_INPUT_NAME).send_keys(username)
  105. click_continue(driver)
  106. if not _element_interactable(driver, By.NAME, PASSWORD_INPUT_NAME):
  107. raise ValueError("Could not find password input!")
  108. driver.find_element(By.NAME, PASSWORD_INPUT_NAME).send_keys(password)
  109. click_continue(driver)
  110. retries = MAX_RETRY_DOMAIN_CHECK
  111. while on_login_form(driver) and retries > 0:
  112. LOGGER.info(
  113. "Login information may not have yet been confirmed. Checking again (Try %s of %s)",
  114. MAX_RETRY_DOMAIN_CHECK - retries + 1,
  115. MAX_RETRY_DOMAIN_CHECK,
  116. )
  117. time.sleep(ELEMENT_CHECK_DELAY)
  118. if on_login_form(driver):
  119. LOGGER.info("Login information has not yet been confirmed. Trying again")
  120. click_continue(driver) # Confirm password
  121. retries -= 1
  122. if retries < 0:
  123. raise ValueError("Failed to confirm login form!")
  124. LOGGER.info("Login information filled out")
  125. def on_login_form(driver: webdriver.Firefox) -> bool:
  126. """
  127. Check if we are on the login form.
  128. Parameters
  129. ----------
  130. driver : webdriver.Firefox
  131. The web driver to check on.
  132. Returns
  133. -------
  134. bool
  135. True if we are on the login form, else false.
  136. """
  137. return "saml2" in driver.current_url
  138. def _fill_mfa(driver: webdriver.Firefox, mfa_secret: Optional[str]) -> None:
  139. LOGGER.info("Checking if we can fill in MFA information")
  140. _check_on_ms_login_page(driver)
  141. # Check if we have the MFA page
  142. if driver.current_url.endswith("/login") and mfa_secret is None:
  143. raise ValueError("You need to supply your MFA secret to log in!")
  144. _check_approval_screen(driver)
  145. # Check if we have an MFA code to enter
  146. if mfa_secret is None:
  147. LOGGER.info("Filling in login information")
  148. return
  149. for i in range(MFA_MAX_RETRY_COUNT):
  150. LOGGER.info(
  151. "Filling in MFA information (Try %s of %s)", i + 1, MFA_MAX_RETRY_COUNT
  152. )
  153. if not _find_element(driver, By.NAME, MFA_INPUT_NAME):
  154. time.sleep(ELEMENT_CHECK_DELAY)
  155. continue
  156. mfa_code = get_mfa_code(mfa_secret)
  157. driver.find_element(By.NAME, MFA_INPUT_NAME).send_keys(mfa_code)
  158. click_continue(driver, MFA_CONTINUE_BUTTON_ID) # Confirm otp
  159. LOGGER.info("Confirmed MFA code")
  160. if not driver.current_url.endswith("/login"):
  161. # We have moved on
  162. LOGGER.info("We have moved away from the MFA page")
  163. break
  164. # Check for any errors
  165. if not _find_element(driver, By.ID, MFA_ERROR_TEXT_ID, 2):
  166. # Did not find an error
  167. LOGGER.info("MFA seems to have been accepted, no errors")
  168. break
  169. def _check_approval_screen(driver: webdriver.Firefox) -> None:
  170. if not _find_element(driver, By.ID, "idDiv_SAOTCAS_Title"):
  171. return
  172. found_sign_in_other_way = False
  173. for _ in range(16):
  174. try:
  175. driver.find_element(By.ID, "signInAnotherWay").click()
  176. found_sign_in_other_way = True
  177. break
  178. except (ElementClickInterceptedException, StaleElementReferenceException):
  179. pass
  180. except NoSuchElementException:
  181. pass
  182. time.sleep(ELEMENT_CHECK_DELAY)
  183. if not found_sign_in_other_way:
  184. raise ValueError("Failed to find the option to sing in another way!")
  185. for _ in range(16):
  186. try:
  187. driver.find_element(By.XPATH, "//div[@data-value='PhoneAppOTP']").click()
  188. break
  189. except (ElementClickInterceptedException, StaleElementReferenceException):
  190. pass
  191. except NoSuchElementException:
  192. pass
  193. time.sleep(ELEMENT_CHECK_DELAY)
  194. return
  195. def _confirm_stay_signed_in(driver: webdriver.Firefox) -> bool:
  196. LOGGER.info("Checking if we should confirm if we should stay signed in")
  197. if not _is_on_ms_login_page(driver):
  198. LOGGER.info("Already logged in, can't confirm 'stay signed in'")
  199. return False
  200. if not driver.current_url.endswith("/common/SAS/ProcessAuth"):
  201. LOGGER.info(
  202. "We have reached a dead end. We have completed the login, but not on the 'stay signed in' page"
  203. )
  204. return False
  205. click_continue(driver) # Confirm stay signed in
  206. LOGGER.info("Confirmed to 'stay signed in'")
  207. return True
  208. def _find_element(driver: webdriver.Firefox, by: str, item: str, wait: int = 8) -> bool:
  209. try:
  210. w = WebDriverWait(driver, wait)
  211. w.until(EC.presence_of_element_located((by, item)))
  212. return True
  213. except TimeoutException:
  214. return False
  215. def _element_interactable(
  216. driver: webdriver.Firefox, by: str, item: str, wait: int = 8
  217. ) -> bool:
  218. try:
  219. w = WebDriverWait(driver, wait)
  220. w.until(EC.element_to_be_clickable((by, item)))
  221. return True
  222. except ElementNotInteractableException:
  223. return False
  224. def _is_on_install_page(driver: webdriver.Firefox) -> None:
  225. if not _find_element(driver, By.ID, VPN_INSTALL_PAGE_EXCLUSIVE_ELEMENT_ID):
  226. raise ValueError("Could not find install page!")
  227. def _get_webvpn_cookie(driver: webdriver.Firefox) -> VPNCookie:
  228. webvpn_cookie = driver.get_cookie("webvpn")
  229. driver.close()
  230. if webvpn_cookie is None:
  231. raise ValueError(
  232. "Failed to find the webvpn cookie. Maybe the authentication has failed?"
  233. )
  234. webvpn_domain = webvpn_cookie["domain"]
  235. webvpn_value = webvpn_cookie["value"]
  236. return VPNCookie(domain=webvpn_domain, cookie=webvpn_value)
  237. def _check_on_ms_login_page(driver: webdriver.Firefox) -> None:
  238. if not _is_on_ms_login_page(driver):
  239. raise ValueError("We should still be on the MS login page but we aren't!")
  240. def _is_on_ms_login_page(driver: webdriver.Firefox) -> bool:
  241. return urlparse(driver.current_url).netloc == "login.microsoftonline.com"
  242. def click_continue(driver: webdriver.Firefox, btn_id: str = CONTINUE_BUTTON_ID) -> bool:
  243. """
  244. Click the continue button.
  245. Parameters
  246. ----------
  247. driver : webdriver.Firefox
  248. The driver for which to click the continue button.
  249. btn_id : str, optional
  250. The buttons ID to be clicked, by default CONTINUE_BUTTON_ID
  251. Returns
  252. -------
  253. bool
  254. True if still on login page, false if not.
  255. """
  256. for _ in range(16):
  257. try:
  258. driver.find_element(By.ID, btn_id).click()
  259. return True
  260. except (ElementClickInterceptedException, StaleElementReferenceException):
  261. pass
  262. except NoSuchElementException:
  263. pass
  264. time.sleep(ELEMENT_CHECK_DELAY)
  265. return False
  266. def get_mfa_code(secret: str) -> str:
  267. """
  268. Get the multifactor authentication code for the given secret.
  269. Parameters
  270. ----------
  271. secret : str
  272. MFA secret
  273. Returns
  274. -------
  275. str
  276. MFA code
  277. """
  278. otp = OTP.fromb32(secret)
  279. code: str = otp.TOTP()[0]
  280. return code