11from requests import Session
2+ from requests .exceptions import JSONDecodeError
23from bs4 import BeautifulSoup , Tag
34import re
45from typing import Any
@@ -37,6 +38,8 @@ def login_with_drexel_connect(session: Session) -> Session:
3738 soup = BeautifulSoup (response .text , "html.parser" )
3839 data = parse_initial_mfa_page (soup )
3940
41+ # the intial MFA page does not have the 'verification code' form field
42+ # the following two requests are sent to fetch the html page with the 'verification code' form field
4043 response = send_request (
4144 session ,
4245 config .drexel_connect_base_url + data ["url" ],
@@ -46,7 +49,15 @@ def login_with_drexel_connect(session: Session) -> Session:
4649 assert (
4750 response .status_code == 200
4851 ), "Failed to request MFA code page from Drexel Connect"
49- json_response = response .json ()
52+
53+ try :
54+ json_response = response .json ()
55+ except JSONDecodeError :
56+ raise Exception (
57+ "Failed to decode JSON response from Drexel Connect. Response: {}" .format (
58+ response .text
59+ )
60+ )
5061
5162 data = {
5263 json_response ["csrfN" ]: json_response ["csrfV" ],
@@ -59,6 +70,7 @@ def login_with_drexel_connect(session: Session) -> Session:
5970 data = data ,
6071 method = "POST" ,
6172 )
73+ # the response should be in HTML format that contains the 'verification code' form field
6274 assert (
6375 response .status_code == 200
6476 ), "Failed to receive MFA code page from Drexel Connect"
@@ -74,6 +86,7 @@ def login_with_drexel_connect(session: Session) -> Session:
7486 "j_mfaToken" : totp_code ,
7587 }
7688
89+ # this request sends the MFA code to Drexel Connect
7790 response = send_request (
7891 session ,
7992 config .drexel_connect_base_url + parsed_data ["url" ],
@@ -84,6 +97,7 @@ def login_with_drexel_connect(session: Session) -> Session:
8497 response .status_code == 200
8598 ), "Failed to send MFA code to Drexel Connect (final step)"
8699
100+ # the session should now have the required cookies to access the TMS website
87101 return session
88102
89103
0 commit comments