ox-it / python-sharepoint

A Python library for getting data out of SharePoint
BSD 3-Clause "New" or "Revised" License
126 stars 45 forks source link

NTLM authentication #28

Open abbazs opened 8 years ago

abbazs commented 8 years ago

Doest this library provides NTLM authentication? The site in which I'm trying to use has NTLM authentication!

bandrel commented 8 years ago

Checkout this fork. works great for me. https://github.com/cgc17/python-sharepoint/tree/master There is a pull request but its pretty old.

https://github.com/ox-it/python-sharepoint/pull/14

thomas-reg commented 8 years ago

Here is a solution I pieced together:

import urllib2
from ntlm import HTTPNtlmAuthHandler
from sharepoint import SharePointSite

url = 'https://site.sharepoint.com'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)

auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
opener = urllib2.build_opener(auth_NTLM)
site = SharePointSite(url, opener)

for sp_list in site.lists:
    print sp_list.id, sp_list.meta['Title']

Hope that helps!

pierrejochem commented 8 years ago

https://github.com/ox-it/python-sharepoint/pull/32 :eyes:

James-Dengel commented 7 years ago

The above helped me as well when having issues with NTLM authentication.

gwsampso commented 5 years ago

Just coming across this thread... I’ve inherited a SharePoint system... yuck, gosh I have something similar and thought I had built it correctly in python 3.7.... is this thread still alive? If it is I’ll post my code. I can connect via session.auth but really struggling with build_opener

thomas-reg commented 5 years ago

I maybe able to able help. The build_handler function was very difficult for me to finally get working. There is little to no documentation on it.

Thomas

On Jan 23, 2019, at 3:59 AM, gwsampso notifications@github.com wrote:

Just coming across this thread... I’ve inherited a SharePoint system... yuck, gosh I have something similar and thought I had built it correctly in python 3.7.... is this thread still alive? If it is I’ll post my code. I can connect via session.auth but really struggling with build_opener

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

nreith commented 4 years ago

Please do help!

grahampugh commented 4 years ago

@thomas-reg did you get a version of your above solution working in python3? We've been using that, but now we need to update to python3, and python-ntlm does not exist for py3. There is an python-ntlm3 module but it fails.

The only other sharepoint connection methods that use NTLM I found use requests (e.g. https://sathua.wordpress.com/2017/05/30/extracting-data-from-sharepoint-using-python-and-rest-api/), but I haven't figured out how to bring requests and sharepoint together (specifically, to make the SharePointSite object).

thomas-reg commented 4 years ago

I did get it working with Python3 using the python-ntlm3 module. But soon thereafter we moved to Sharepoint in the Cloud and I had to switch to using OAUTH2 which was more difficult at first and now is easier. I will see if I can pull up my old code and send some snippets, but can you tell me what exactly are the error(s) you are encountering? Can you send a piece of code I can review?

Thomas Register

On Feb 18, 2020, at 3:19 PM, Graham Pugh notifications@github.com wrote:

@thomas-reg https://github.com/thomas-reg did you get a version of your above solution working in python3? We've been using that, but now we need to update to python3, and python-ntlm does not exist for py3. There is an python-ntlm3 module but it fails.

The only other sharepoint connection methods that use NTLM I found use requests (e.g. https://sathua.wordpress.com/2017/05/30/extracting-data-from-sharepoint-using-python-and-rest-api/ https://sathua.wordpress.com/2017/05/30/extracting-data-from-sharepoint-using-python-and-rest-api/), but I haven't figured out how to bring requests and sharepoint together (specifically, to make the SharePointSite object).

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ox-it/python-sharepoint/issues/28?email_source=notifications&email_token=AEOSMVZY6ECMN75HFJA5DETRDRGFTA5CNFSM4CAH2HG2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMFCG2Q#issuecomment-587866986, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEOSMV2CGNVLGMTWZAMR74LRDRGFTANCNFSM4CAH2HGQ.

grahampugh commented 4 years ago

@thomas-reg This is my current function for connecting (with the relevant imports shown):

from six.moves import urllib
from ntlm3.HTTPNtlmAuthHandler import HTTPNtlmAuthHandler
from sharepoint import SharePointSite
    def connect_sharepoint(url, user, password):
        """make a connection to SharePoint"""
        pass_man = urllib.request.HTTPPasswordMgrWithDefaultRealm()
        pass_man.add_password(None, url, user, password)
        auth_ntlm = HTTPNtlmAuthHandler(pass_man)
        opener = urllib.request.build_opener(auth_ntlm)
        urllib.request.install_opener(opener)
        site = SharePointSite(url, opener)
        return site

I get the following error, at the point of running the line sp_list = site.lists['list name'], which appears (at a guess) to be due to improper encoding/decoding of the parts of the authentication request, but I cannot figure out exactly where:

Traceback (most recent call last):
  File "/Library/AutoPkg/autopkglib/__init__.py", line 673, in process
    self.env = processor.process()
  File "/Library/AutoPkg/autopkglib/__init__.py", line 480, in process
    self.main()
  File "/Users/gpugh/Library/AutoPkg/RecipeRepos/ch.ethz.gitlab.id-cd-mac.id-mac-autopkg-recipes/_CommonProcessors/CheckSharepointToStage.py", line 167, in main
    if (self.check_content_test(site, untested_policy_name)
  File "/Users/gpugh/Library/AutoPkg/RecipeRepos/ch.ethz.gitlab.id-cd-mac.id-mac-autopkg-recipes/_CommonProcessors/CheckSharepointToStage.py", line 87, in check_content_test
    self.output(site.as_xml(sp_test_listname))
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/site-packages/sharepoint/site.py", line 50, in as_xml
    xml.append(self.lists.as_xml(**kwargs))
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/site-packages/sharepoint/lists/__init__.py", line 113, in as_xml
    return OUT.lists(*[l.as_xml(**kwargs) for l in lists])
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/site-packages/sharepoint/lists/__init__.py", line 80, in __iter__
    return iter(self.all_lists)
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/site-packages/sharepoint/lists/__init__.py", line 36, in all_lists
    result = self.opener.post_soap(LIST_WEBSERVICE, xml)
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/site-packages/sharepoint/site.py", line 32, in post_soap
    response = self.opener.open(request, timeout=self.timeout)
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/urllib/request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/urllib/request.py", line 563, in error
    result = self._call_chain(*args)
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/site-packages/ntlm3/HTTPNtlmAuthHandler.py", line 135, in http_error_401
    return self.http_error_authentication_required('www-authenticate', req, fp, headers)
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/site-packages/ntlm3/HTTPNtlmAuthHandler.py", line 40, in http_error_authentication_required
    return self.retry_using_http_NTLM_auth(req, auth_header_field, None, headers)
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/site-packages/ntlm3/HTTPNtlmAuthHandler.py", line 106, in retry_using_http_NTLM_auth
    (ServerChallenge, NegotiateFlags) = ntlm.parse_NTLM_CHALLENGE_MESSAGE(auth_header_value[5:])
  File "/Library/AutoPkg/Python3/Python.framework/Versions/Current/lib/python3.7/site-packages/ntlm3/ntlm.py", line 189, in parse_NTLM_CHALLENGE_MESSAGE
    msg_type = struct.unpack("<I", msg2[8:12])[0]
struct.error: unpack requires a buffer of 4 bytes
  File "/Library/AutoPkg/autopkglib/__init__.py", line 673, in process
    self.env = processor.process()
unpack requires a buffer of 4 bytes
grahampugh commented 4 years ago

Update: I managed to solve it by editing two lines in python-ntlm3 - HTTPNtlmAuthHandler.py:

Lines 57 and 107 require .decode('utf-8') at their end:

Line 57:

auth = 'NTLM %s' % ntlm.create_NTLM_NEGOTIATE_MESSAGE(user, type1_flags).decode('utf-8')

Line 107-108:

auth = 'NTLM %s' % ntlm.create_NTLM_AUTHENTICATE_MESSAGE(
    ServerChallenge, UserName, DomainName, pw, NegotiateFlags).decode('utf-8')