Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connecting to Sharepoint #334

Open
ahulist opened this issue Mar 26, 2021 · 6 comments
Open

Connecting to Sharepoint #334

ahulist opened this issue Mar 26, 2021 · 6 comments
Labels

Comments

@ahulist
Copy link

ahulist commented Mar 26, 2021

I would like to CRUD files on Sharepoint. To do this I'm trying to follow this basic example:

from office365.sharepoint.client_context import ClientContext

settings = {
    'url': 'https://*****.sharepoint.com/sites/*****',
    'user_credentials': {
        'username': '*****@gmail.com',
        'password': '*****',
    },
}

ctx = ClientContext(settings["url"]).with_user_credentials(settings.get('user_credentials').get('username'),
                                                           settings.get('user_credentials').get('password'))

web = ctx.web.get().execute_query()
print(web.properties["Url"])

which gives me an Error:

IndexError                                Traceback (most recent call last)
<ipython-input-23-c5907526ff22> in <module>
     12                                                            settings.get('user_credentials').get('password'))
     13 
---> 14 web = ctx.web.get().execute_query()
     15 print(web.properties["Url"])

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\client_object.py in execute_query(self)
     31 
     32     def execute_query(self):
---> 33         self.context.execute_query()
     34         return self
     35 

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\client_runtime_context.py in execute_query(self)
    136     def execute_query(self):
    137         if self.has_pending_request:
--> 138             self.pending_request().execute_query()
    139 
    140     def add_query(self, query, to_begin=False):

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\client_request.py in execute_query(self)
     72                 request = self.build_request()
     73                 self.beforeExecute.notify(request)
---> 74                 response = self.execute_request_direct(request)
     75                 response.raise_for_status()
     76                 self.process_response(response)

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\odata\odata_request.py in execute_request_direct(self, request)
     32     def execute_request_direct(self, request):
     33         self.ensure_media_type(request)
---> 34         return super(ODataRequest, self).execute_request_direct(request)
     35 
     36     def build_request(self):

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\client_request.py in execute_request_direct(self, request_options)
     84         :type request_options: office365.runtime.http.request_options.RequestOptions
     85         """
---> 86         self.context.authenticate_request(request_options)
     87         if request_options.method == HttpMethod.Post:
     88             if request_options.is_bytes or request_options.is_file:

D:\Anaconda\envs\hplc\lib\site-packages\office365\sharepoint\client_context.py in authenticate_request(self, request)
    151 
    152     def authenticate_request(self, request):
--> 153         self._auth_context.authenticate_request(request)
    154 
    155     def _build_modification_query(self, request):

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\auth\authentication_context.py in authenticate_request(self, request)
     82         """Authenticate request
     83         :type request: RequestOptions"""
---> 84         self._provider.authenticate_request(request)

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py in authenticate_request(self, request)
     71         """
     72         logger = self.logger(self.authenticate_request.__name__)
---> 73         self.ensure_authentication_cookie()
     74         logger.debug_secrets(self._cached_auth_cookies)
     75         cookie_header_value = "; ".join(["=".join([key, str(val)]) for key, val in self._cached_auth_cookies.items()])

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py in ensure_authentication_cookie(self)
     78     def ensure_authentication_cookie(self):
     79         if self._cached_auth_cookies is None:
---> 80             self._cached_auth_cookies = self.get_authentication_cookie()
     81         return True
     82 

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py in get_authentication_cookie(self)
     91             user_realm = self._get_user_realm()
     92             if user_realm.IsFederated:
---> 93                 token = self._acquire_service_token_from_adfs(user_realm.STSAuthUrl)
     94             else:
     95                 token = self._acquire_service_token()

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py in _acquire_service_token_from_adfs(self, adfs_url)
    134                                  headers={'Content-Type': 'application/soap+xml; charset=utf-8'})
    135         dom = minidom.parseString(response.content.decode())
--> 136         assertion_node = dom.getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion", 'Assertion')[0].toxml()
    137 
    138         try:

IndexError: list index out of range

Any ideas? It doesn't feel like an authentication problem.

@vgrem vgrem added the question label Mar 30, 2021
@nsmcan
Copy link
Contributor

nsmcan commented Apr 14, 2021

@ahulist Logging the response.content.decode() from line 135 of saml_token_provider.py, and reading it might help you to understand, why Assertion was not provided

@nsmcan
Copy link
Contributor

nsmcan commented Apr 14, 2021

@vgrem, though you guys got this code with the minidom module from my suggestion, I ended up using the simple regex match there. It must be the most effective, and don't require minidom, which you don't use anywhere else:

            match = re.search(r'<saml:Assertion.+</saml:Assertion>', response.content.decode())
            if match is None:
                self.error = 'Cannot get security assertion for user {0} from {1}'.format(self.__username, adfs_url)
                logger.error(self.error)
                return None
            assertion_node = match.group()

This chunk is from an older 2.2.1.1 version

@vgrem
Copy link
Owner

vgrem commented Apr 14, 2021

@nsmcan, i believe we did #297 :)

@vgrem
Copy link
Owner

vgrem commented Apr 14, 2021

@nsmcan, although this change seems unfortunately impacted to broken auth (for federated signin with ADFS), related issues reported here:

@nsmcan
Copy link
Contributor

nsmcan commented Apr 14, 2021

@vgrem, we need to double-check code against a reference implementation found on Microsoft. I will ask our SharePoint admin (@Zerg00s) to help me installing and configuring a couple of test instances, where it could be properly tested

@AkechiShiro
Copy link

AkechiShiro commented Feb 22, 2023

Hey @nsmcan, @vgrem @Zerg00s, was this issue abandoned/forgotten ?
has this been solved in any way yet (I believe it is not fixed at the moment) or considered a won't fix issue ?
Is there any temporary workaround for this issue ? Reverting back to an old commit ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants