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

NTLM authentication #28

Open
abbazs opened this issue Apr 7, 2016 · 11 comments
Open

NTLM authentication #28

abbazs opened this issue Apr 7, 2016 · 11 comments

Comments

@abbazs
Copy link

abbazs commented Apr 7, 2016

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

@bandrel
Copy link

bandrel commented Apr 9, 2016

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

#14

@thomas-reg
Copy link

thomas-reg commented May 24, 2016

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
Copy link

pierrejochem commented Jul 28, 2016

#32 👀

@James-Dengel
Copy link

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

@gwsampso
Copy link

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
Copy link

thomas-reg commented Jan 23, 2019 via email

@nreith
Copy link

nreith commented Oct 10, 2019

Please do help!

@grahampugh
Copy link

@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
Copy link

thomas-reg commented Feb 19, 2020 via email

@grahampugh
Copy link

@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
Copy link

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')

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

No branches or pull requests

8 participants