Search code examples
pythonurllib2ntlm

How can I use Windows authentication in lieu of storing / passing a password?


I am looking for a way to avoid storing / entering a password when attempting to connect to a Windows SharePoint site. I am using urllib2

The script functions correctly when I pass "user" and "password" as strings in the code below. I would rather leverage Windows Authentication to avoid having to store a password within the script.

import os
import urllib2
from ntlm import HTTPNtlmAuthHandler


user = '%s\%s' % ( os.environ["USERDOMAIN"], os.environ["USERNAME"] )
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, server, user, password)

Is there something akin to using "Trusted Connection" in pyodbc:

cnxn = pyodbc.connect("""Driver={SQL Server};Server=Whatever;Database=Whatever;**Trusted_Connection=yes**""")

After lengthy searching, I have come up empty. I am grateful for any suggestions.


Solution

  • If storing a password works, then I'd recommend just storing the password. Try using Python's keyring module to do this. https://pypi.python.org/pypi/keyring

    This will keep the credentials secure on whatever platform the user is using to connect.