toolbelt
toolbelt copied to clipboard
Extending Guess Auth with NTLM
Hello,
I'm trying to extend the GuessAuth class to add NTLM as well as other forms of security.
Here is my code:
def handle_401(self, r, **kwargs):
"""Resends a request with auth headers, if needed."""
www_authenticate = r.headers.get('www-authenticate', '').lower()
if 'basic' in www_authenticate:
return self._handle_basic_auth_401(r, kwargs)
if 'digest' in www_authenticate:
return self._handle_digest_auth_401(r, kwargs)
if www_authenticate.find('ntlm') > -1:
return self._handle_ntlm_auth_401(r, kwargs)
def _handle_ntlm_auth_401(self, r, kwargs):
if self.pos is not None:
r.request.body.seek(self.pos)
# Consume content and release the original connection
# to allow our new request to reuse the same one.
r.content
r.raw.release_conn()
prep = r.request.copy()
if not hasattr(prep, '_cookies'):
prep._cookies = cookies.RequestsCookieJar()
cookies.extract_cookies_to_jar(prep._cookies, r.request, r.raw)
prep.prepare_cookies(prep._cookies)
self.auth = HttpNtlmAuth(self.username, self.password)
prep = self.auth(prep)
_r = r.connection.send(prep, **kwargs)
_r.history.append(r)
_r.request = prep
return _r
I still am getting a 401 error on the retry.
When I do this:
from requests_ntlm import HttpNtlmAuth
session = requests.Session()
session.verify = False
session.auth = HttpNtlmAuth('domain\\user','awesomepw')
print(session.get('https://mysite.domain.com/wa/sharing/rest/info?f=json').json())
it works.
Any ideas what I'm missing?
Thanks