我正在构建一个应用程序,员工应该能够通过 Web 表单使用 LDAP 凭据登录。
我在翻译这个使用
ldap3
库的 Python 函数时遇到问题:
def authenticate_ldap(
username: str,
password: str
) -> dict[str, str, str]:
try:
conn = Connection(
server='ldapDomain', user=username,
password=password, authentication=NTLM,
auto_bind=True
)
conn.search(
'DC=smth,DC=com', f'(sAMAccountName={username})',
attributes=['department', 'displayName', 'description']
)
name = conn.entries[0].displayName
filial = conn.entries[0].department
pos = conn.entries[0].description
except LDAPBindError:
return False
data_user = {
'name': str(name),
'department': str(filial),
'position': str(pos)
}
return data_user
不幸的是,我公司里没有人熟悉 Node,我已经搜索和尝试了一段时间但没有成功。
我已经尝试使用windows身份验证策略用于
Passport.js
(非集成),但不确定如何将用户名和密码传递给函数,当我对它们进行硬编码时,它仍然会抛出52e
错误(即使使用用户名错误)。
也尝试过
express-ntlm
,但不确定传递身份验证标头的正确方法是什么(在文档中没有找到任何提及)。
在 Node 中是否可能?
你可能在第一次绑定时失败了,然后就开始走下坡路了。
我对 Node 也不太了解,但对 LDAP 我了解一二。以下是您通常的做法。
首先,您需要在配置中(以及 LDAP 服务器的主机名):
cn=myapp,ou=users,dc=smth,dc=com
有了这些,当您收到要验证的用户名密码组合时,您将执行以下操作:
SIMPLE
绑定,而不是 NTLMdn
)dn
和您收到的密码重新绑定。我没有任何东西要测试,但代码应该如下所示:
def authenticate_ldap(
username: str,
password: str
) -> dict[str, str, str]:
try:
conn = Connection(
server='ldapDomain', user=config.app_username,
password=config.app_password
)
conn.search(
'DC=smth,DC=com', f'(sAMAccountName={username})',
attributes=['department', 'displayName', 'description']
)
user_dn = conn.entries[0].entry_dn
conn.unbind() #YMMV, but I prefer to unbind the app
except LDAPBindError:
# Save yourself a lot of blind debugging with two try statements
print("The application cannot connect or something")
return False
try:
conn = Connection(
server='ldapDomain', user=user_dn,
password=password
)
# Validate the user's password
name = conn.entries[0].displayName
filial = conn.entries[0].department
pos = conn.entries[0].description
except LDAPBindError:
print("The application cannot validate the password somehow")
return False
return {
'name': str(name),
'department': str(filial),
'position': str(pos)
}