Skip to content Skip to sidebar Skip to footer

Changing Userpassword In Openldap Using Ldap3 Library

I can't seem to change a users password using the ldap3 python module against an OpenLDAP server. A similar question has been asked before but that's specific to Active Directory.

Solution 1:

Changing the password seems to work as described in the docs and shown in the edit of my question above. For future reference, this code seems to work:

from ldap3 import (
    HASHED_SALTED_SHA, MODIFY_REPLACE
)
from ldap3.utils.hashed import hashed

defmodify_user_password(self, user, password):
    dn = user.entry_get_dn()
    hashed_password = hashed(HASHED_SALTED_SHA, password)
    changes = {
        'userPassword': [(MODIFY_REPLACE, [hashed_password])]
    }
    success = self.connection.modify(dn, changes=changes)
    ifnot success:
        print('Unable to change password for %s' % dn)
        print(self.connection.result)
        raise ValueError('Unable to change password')

To clarify a few things:

  1. This is connecting to an OpenLDAP server (with multiple databases)
  2. There is NO SSL here. We plan on implementing SSL but this works without it.

Post a Comment for "Changing Userpassword In Openldap Using Ldap3 Library"