python3以安全的方式检查ldap用户名和密码

问题描述 投票:0回答:1

我如何在python3中检查用户名和密码是否有效?

我已经要求用户提供用户名和密码:

import getpass
import sys

sys.stdout.write("- Enter your (LDAP) username : ")
    username = input().lower()
password=getpass.getpass("- Enter your (LDAP) password : ")

我知道我可以使用ldapwhoami检查有效性,例如:

import subprocess

subprocess.run(['ldapwhoami', '-h', 'ldap-server', '-D', '{}@domain'.format(username)', '-x', 
                '-w', password], check=True)

但随后会生成一个过程,在该过程中将看到密码。那么,我如何安全地检查这些凭据?使用python库还是类似的方法隐藏密码?

python-3.x ldap python-ldap
1个回答
0
投票

您可以使用python-ldap库获取此文件,因此不会产生单独的进程。

import ldap
try:
    # build a client
    ldap_client = ldap.initialize("ldap://ldap-server.domain")
    # perform a synchronous bind
    ldap_client.set_option(ldap.OPT_REFERRALS, 0)
    ldap_client.simple_bind_s("{}@domain".format(username), password)
    print("LDAP credentials were good!")
except ldap.INVALID_CREDENTIALS:
    ldap_client.unbind()
    print("LDAP credentials incorrect!")
© www.soinside.com 2019 - 2024. All rights reserved.