Wednesday, January 7, 2009

Python SUDS with Windows Authentication (SOAP)

While intergrating Plone with CRM4, I stumbled upon Suds. However, it didn't support NTLM. Then I found python-ntlm, and merged the two libs.

Below is the result.

You can add it at the bottom of suds/transport.py



[code]
class WindowsHttpAuthenticated(HttpAuthenticated):
"""
Provides Windows (NTLM) http authentication.
@ivar pm: The password manager.
@ivar handler: The authentication handler.
@author: Christopher Bess
"""

def __init__(self, options):
# try to import ntlm support
try:
from ntlm import HTTPNtlmAuthHandler
except ImportError:
raise Exception("Cannot import python-ntlm module")
return

HttpTransport.__init__(self, options)
self.pm = u2.HTTPPasswordMgrWithDefaultRealm()
self.handler = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(self.pm)
self.urlopener = u2.build_opener(self.handler)
u2.install_opener(self.urlopener)
pass # end class

[/code]


[code]
from suds import transport
#>>>
from suds import client
#>>>
from suds.options import Options
#>>>
options = Options(username='DOMAIN\username',
password='password')
#>>>
c = client.Client(url='http://0.0.0.0/mscrmservices/2007/CrmServiceWsdl.aspx',
username='DOMAIN\username',
password='password',
transport=transport.WindowsHttpAuthenticated(options))
#>>>
c
#---

[/code]