I wrote a python script to change passwords in AD, which works well from the command line. Now I am migrating this script to Django to create a page with password changes. Everything seems to work, except for one: for some reason, I keep getting TypeError in Django, which assumes that a string should be passed to AD, not a unicode object. Of course, when I do this, AD protests because the password is no longer in Unicode. I suppose I'm missing something simple, but I can't figure that out.
Below is the views.py code, which I think should work (and this works on the command line version). I added unicode () to passwords to indicate that they are actually unicode, although they are definitely unicode before these lines.
import ldap def changePassword(request): t = ldap.initialize(server) . . . t.simple_bind_s('**********', '****') . . . mod_attrs = [(ldap.MOD_DELETE, 'unicodePwd', [unicode(oldPassword)]), (ldap.MOD_ADD, 'unicodePwd', [unicode(newPassword)])] t.modify_s(dn, mod_attrs)
Error:
('expected a string in the list', u'pass!word') Request Method: POST Request URL: http://127.0.0.1:8000/index/ Django Version: 1.4.2 Exception Type: TypeError Exception Value: ('expected a string in the list', u'pass!word') Exception Location: /usr/lib/python2.7/dist-packages/ldap/ldapobject.py in _ldap_call, line 96 Python Executable: /usr/bin/python Python Version: 2.7.3
The modified version does not raise an exception, but also does not change the password (since the password is not transmitted in Unicode):
mod_attrs = [(ldap.MOD_DELETE, 'unicodePwd', [str(oldPassword)]), (ldap.MOD_ADD, 'unicodePwd', [str(newPassword)])] t.modify_s(dn, mod_attrs)
Does anyone know what could lead to this issue in Unicode? If I could prevent the exception in Django and save unicode, I think this code will work.
Thanks in advance for any information you could offer.