Skip to content

Commit 05c3bb5

Browse files
Merge pull request #33 from zippeurfou/fix/authentication-timing-issue
Fix/authentication timing issue
2 parents ebbbe1a + 7692663 commit 05c3bb5

File tree

1 file changed

+89
-18
lines changed

1 file changed

+89
-18
lines changed

linkedin_mcp_server/drivers/chrome.py

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -206,34 +206,105 @@ def login_with_cookie(driver: webdriver.Chrome, cookie: str) -> bool:
206206
Returns:
207207
bool: True if login was successful, False otherwise
208208
"""
209+
import time
210+
209211
try:
210212
from linkedin_scraper import actions # type: ignore
213+
from selenium.common.exceptions import TimeoutException
211214

212215
logger.info("Attempting cookie authentication...")
213216

214-
# Set timeout for cookie authentication - longer to handle LinkedIn's slow redirects
215-
driver.set_page_load_timeout(30)
217+
# Set longer timeout to handle slow LinkedIn loading
218+
# Invalid cookies cause indefinite loading, so timeout is our detection mechanism
219+
driver.set_page_load_timeout(45)
220+
221+
# Attempt login
222+
retry_count = 0
223+
max_retries = 1
224+
225+
while retry_count <= max_retries:
226+
try:
227+
actions.login(driver, cookie=cookie)
228+
# If we reach here without timeout, login attempt completed
229+
break
230+
except TimeoutException:
231+
# Timeout indicates invalid cookie (page loads forever)
232+
logger.warning(
233+
"Cookie authentication failed - page load timeout (likely invalid cookie)"
234+
)
235+
return False
236+
except Exception as e:
237+
# Handle InvalidCredentialsError from linkedin-scraper
238+
# This library sometimes incorrectly reports failure even when login succeeds
239+
if "InvalidCredentialsError" in str(
240+
type(e)
241+
) or "Cookie login failed" in str(e):
242+
logger.info(
243+
"LinkedIn-scraper reported InvalidCredentialsError - verifying actual authentication status..."
244+
)
245+
# Give LinkedIn time to complete redirect
246+
time.sleep(2)
247+
break
248+
else:
249+
logger.warning(f"Login attempt failed: {e}")
250+
if retry_count < max_retries:
251+
retry_count += 1
252+
logger.info(
253+
f"Retrying authentication (attempt {retry_count + 1}/{max_retries + 1})"
254+
)
255+
time.sleep(2)
256+
continue
257+
else:
258+
return False
259+
260+
# Check authentication status by examining the current URL
261+
try:
262+
current_url = driver.current_url
216263

217-
actions.login(driver, cookie=cookie)
264+
# Check if we're on login page (authentication failed)
265+
if "login" in current_url or "uas/login" in current_url:
266+
logger.warning(
267+
"Cookie authentication failed - redirected to login page"
268+
)
269+
return False
218270

219-
# Quick check - if we're on login page, cookie is invalid
220-
current_url = driver.current_url
221-
if "login" in current_url or "uas/login" in current_url:
222-
logger.warning("Cookie authentication failed - redirected to login page")
223-
return False
224-
elif (
225-
"feed" in current_url
226-
or "mynetwork" in current_url
227-
or "linkedin.com/in/" in current_url
228-
):
229-
logger.info("Cookie authentication successful")
230-
return True
231-
else:
232-
logger.warning("Cookie authentication failed - unexpected page")
271+
# Check if we're on authenticated pages (authentication succeeded)
272+
elif any(
273+
indicator in current_url
274+
for indicator in ["feed", "mynetwork", "linkedin.com/in/", "/feed/"]
275+
):
276+
logger.info("Cookie authentication successful")
277+
return True
278+
279+
# Unexpected page - wait briefly and check again
280+
else:
281+
logger.info(
282+
"Unexpected page after login, checking authentication status..."
283+
)
284+
time.sleep(2)
285+
286+
final_url = driver.current_url
287+
if "login" in final_url or "uas/login" in final_url:
288+
logger.warning("Cookie authentication failed - ended on login page")
289+
return False
290+
elif any(
291+
indicator in final_url
292+
for indicator in ["feed", "mynetwork", "linkedin.com/in/", "/feed/"]
293+
):
294+
logger.info("Cookie authentication successful after verification")
295+
return True
296+
else:
297+
logger.warning(
298+
f"Cookie authentication uncertain - unexpected final page: {final_url}"
299+
)
300+
return False
301+
302+
except Exception as e:
303+
logger.error(f"Error checking authentication status: {e}")
233304
return False
234305

235306
except Exception as e:
236-
logger.warning(f"Cookie authentication failed: {e}")
307+
logger.error(f"Cookie authentication failed with error: {e}")
237308
return False
238309
finally:
239310
# Restore normal timeout

0 commit comments

Comments
 (0)