使用ldap3库在OpenLDAP中更改用户密码

2024-05-15 00:55:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我似乎无法在OpenLDAP服务器上使用ldap3python模块更改用户密码。有人问过类似的问题before,但这是特定于activedirectory的。在

我所做的:

from ldap3.extend.standard.modifyPassword import ModifyPassword
from ldap3.utils.hashed import hashed
password = hashed(HASHED_SALTED_SHA, password)
# or..
password = '{SASL}theuser@domain.com'
modify = ModifyPassword(
    connection, user.entry_get_dn(), new_password=password)
resp = modify.send()
print(modify.result)
{'referrals': None, 'result': 0, 'description': 'success', 'type': 'extendedResp', 'message': '', 'responseName': None, 'new_password': None, 'dn': '', 'responseValue': None}

描述说成功,但密码实际上没有更改。在

我还试图发送一个modify replace消息:

^{pr2}$

该连接不是SSL连接。AD问题的答案要求连接通过SSL。这也是OpenLDAP的一个要求吗?在

编辑:

{{{cd2}之后的时间似乎变成了工作。在今天再次运行这些测试之后,它现在似乎一直在工作。我要把这归因于没有在我的目录浏览器中查看新的数据。在


Tags: fromimport服务器none密码sslnewpassword
1条回答
网友
1楼 · 发布于 2024-05-15 00:55:25

更改密码似乎可以按照文档中的描述和上面我的问题编辑中所显示的那样工作。为了将来参考,此代码似乎可以工作:

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

def modify_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)
    if not success:
        print('Unable to change password for %s' % dn)
        print(self.connection.result)
        raise ValueError('Unable to change password')

澄清几件事:

  1. 这是连接到OpenLDAP服务器(具有多个数据库)
  2. 这里有没有SSL。我们计划实现SSL,但是没有SSL就可以了。在

相关问题 更多 >

    热门问题