@@ -200,34 +200,39 @@ def authenticate_icourse(
200200
201201 # Follow redirect chain to reach IDP login page and extract lck.
202202 # The CAS gateway occasionally returns 200 + an HTML interstitial
203- # instead of a 302 chain — a short sleep gives it time to settle.
203+ # instead of a 302 chain — retry a few times before giving up so
204+ # we don't force a full WebVPN re-login for a transient hiccup.
204205 import time as _time
205- _time .sleep (1 )
206-
207- resp = self .session .get (vpn_url , allow_redirects = False , timeout = 60 )
208206 lck = None
209- for _ in range (15 ):
210- location = resp .headers .get ("Location" , "" )
211- if resp .status_code not in (301 , 302 , 303 , 307 ) or not location :
212- break
213- lck_match = re .search (r'lck=([^&#"]+)' , location )
214- if lck_match :
215- lck = lck_match .group (1 )
207+ for cas_attempt in range (3 ):
208+ if cas_attempt > 0 :
209+ _time .sleep (2 )
210+ print (f" CAS lck extract retry { cas_attempt } /2..." )
211+ resp = self .session .get (vpn_url , allow_redirects = False , timeout = 60 )
212+ for _ in range (15 ):
213+ location = resp .headers .get ("Location" , "" )
214+ if resp .status_code not in (301 , 302 , 303 , 307 ) or not location :
215+ break
216+ lck_match = re .search (r'lck=([^&#"]+)' , location )
217+ if lck_match :
218+ lck = lck_match .group (1 )
219+ break
220+ if not location .startswith ("http" ):
221+ location = urljoin (resp .url , location )
222+ resp = self .session .get (
223+ location , allow_redirects = False , timeout = 60
224+ )
225+ if lck :
216226 break
217- if not location .startswith ("http" ):
218- location = urljoin (resp .url , location )
219- resp = self .session .get (
220- location , allow_redirects = False , timeout = 60
221- )
222-
223- if not lck :
224- # Check final response URL, body, AND any redirect history
227+ # Check final response for lck embedded in HTML
225228 for source in [resp .url , resp .text [:5000 ],
226229 str (getattr (resp , 'history' , []))]:
227230 m = re .search (r'lck=([^&#"]+)' , source )
228231 if m :
229232 lck = m .group (1 )
230233 break
234+ if lck :
235+ break
231236 if not lck :
232237 raise RuntimeError (
233238 f"Failed to extract lck from CAS redirect chain (status={ resp .status_code } )"
0 commit comments