Skip to content

Authentication failing when performed after a long(ish) wait #950

@AlessandroZanatta

Description

@AlessandroZanatta

Hi,

the issue I'm observing is the following: when performing authentication to SharePoint normally (see below for the code used for authentication), everything works fine. I'm able to authenticate just fine, and to perform various actions.

However, due to the nature of what I'm trying to achieve, I have some scheduled jobs that run at predetermined time instants using the schedule library. When a job is run through the scheduler soon after the script is started, it works perfectly fine. However, when the same exact job is run after a long(ish) wait (30 minutes appear to be enough), it fails to authenticate. Below you can also find the related stack trace.

I know very little about SharePoint and similar MS products, but this appears to me as a bug in the library, but I may very well be wrong.

Thanks in advance!

Authentication function:

def get_ctx():
    config = get_config()
    ctx_auth = AuthenticationContext(url=config.SP_SITE_URL)
    if not ctx_auth.acquire_token_for_user(config.SP_USERNAME, config.SP_PASSWORD):
        logger.critical("Failed to authorize!!")
        exit(1)

    return ClientContext(config.SP_SITE_URL, ctx_auth)

Example function that fails:

def get_folder(name: str, ctx: ClientContext = get_ctx()) -> Folder:
    logger.debug("Getting folder '{}'", name)
    return ctx.web.get_folder_by_server_relative_url(name).get().execute_query()

Stack trace:

Traceback (most recent call last):
  File "/app/src/main.py", line 94, in <module>
    main()
    ~~~~^^
  File "/app/src/main.py", line 89, in main
    schedule.run_pending()
    ~~~~~~~~~~~~~~~~~~~~^^
  File "/app/.venv/lib/python3.13/site-packages/schedule/__init__.py", line 854, in run_pending
    default_scheduler.run_pending()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/app/.venv/lib/python3.13/site-packages/schedule/__init__.py", line 101, in run_pending
    self._run_job(job)
    ~~~~~~~~~~~~~^^^^^
  File "/app/.venv/lib/python3.13/site-packages/schedule/__init__.py", line 173, in _run_job
    ret = job.run()
  File "/app/.venv/lib/python3.13/site-packages/schedule/__init__.py", line 691, in run
    ret = self.job_func()
  File "/app/src/main.py", line 47, in cleanup
    files, _ = list_folder(config.PATH)
               ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/app/src/sharepoint.py", line 41, in list_folder
    folder = get_folder(name, ctx=ctx)
  File "/app/src/sharepoint.py", line 35, in get_folder
    return ctx.web.get_folder_by_server_relative_url(name).get().execute_query()
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/app/.venv/lib/python3.13/site-packages/office365/runtime/client_object.py", line 55, in execute_query
    self.context.execute_query()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/app/.venv/lib/python3.13/site-packages/office365/runtime/client_runtime_context.py", line 173, in execute_query
    self.pending_request().execute_query(qry)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/app/.venv/lib/python3.13/site-packages/office365/runtime/client_request.py", line 37, in execute_query
    response = self.execute_request_direct(request)
  File "/app/.venv/lib/python3.13/site-packages/office365/runtime/client_request.py", line 46, in execute_request_direct
    self.beforeExecute.notify(request)
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/office365/runtime/types/event_handler.py", line 41, in notify
    listener(*args, **kwargs)
    ~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/office365/sharepoint/client_context.py", line 284, in _authenticate_request
    self.authentication_context.authenticate_request(request)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/office365/runtime/auth/authentication_context.py", line 249, in authenticate_request
    self._authenticate(request)
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/office365/runtime/auth/authentication_context.py", line 222, in _authenticate
    provider.authenticate_request(request)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 85, in authenticate_request
    self.ensure_authentication_cookie()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/app/.venv/lib/python3.13/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 97, in ensure_authentication_cookie
    self._cached_auth_cookies = self.get_authentication_cookie()
                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/app/.venv/lib/python3.13/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 109, in get_authentication_cookie
    token = self._acquire_service_token_from_adfs(user_realm.STSAuthUrl)
  File "/app/.venv/lib/python3.13/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 160, in _acquire_service_token_from_adfs
    assertion_node = dom.getElementsByTagNameNS(
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
        "urn:oasis:names:tc:SAML:1.0:assertion", "Assertion"
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    )[0].toxml()
    ~^^^
IndexError: list index out of range

``

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions