1
1
from requests import Session
2
+ from requests .exceptions import JSONDecodeError
2
3
from bs4 import BeautifulSoup , Tag
3
4
import re
4
5
from typing import Any
@@ -37,6 +38,8 @@ def login_with_drexel_connect(session: Session) -> Session:
37
38
soup = BeautifulSoup (response .text , "html.parser" )
38
39
data = parse_initial_mfa_page (soup )
39
40
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
40
43
response = send_request (
41
44
session ,
42
45
config .drexel_connect_base_url + data ["url" ],
@@ -46,7 +49,15 @@ def login_with_drexel_connect(session: Session) -> Session:
46
49
assert (
47
50
response .status_code == 200
48
51
), "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
+ )
50
61
51
62
data = {
52
63
json_response ["csrfN" ]: json_response ["csrfV" ],
@@ -59,6 +70,7 @@ def login_with_drexel_connect(session: Session) -> Session:
59
70
data = data ,
60
71
method = "POST" ,
61
72
)
73
+ # the response should be in HTML format that contains the 'verification code' form field
62
74
assert (
63
75
response .status_code == 200
64
76
), "Failed to receive MFA code page from Drexel Connect"
@@ -74,6 +86,7 @@ def login_with_drexel_connect(session: Session) -> Session:
74
86
"j_mfaToken" : totp_code ,
75
87
}
76
88
89
+ # this request sends the MFA code to Drexel Connect
77
90
response = send_request (
78
91
session ,
79
92
config .drexel_connect_base_url + parsed_data ["url" ],
@@ -84,6 +97,7 @@ def login_with_drexel_connect(session: Session) -> Session:
84
97
response .status_code == 200
85
98
), "Failed to send MFA code to Drexel Connect (final step)"
86
99
100
+ # the session should now have the required cookies to access the TMS website
87
101
return session
88
102
89
103
0 commit comments