编辑:tl;dr - search_filter
SEARCH 中使用的参数可能不符合 RFC4515。
我有一个在 Ubuntu 14.04 上运行 1.8.4 版的 Django 服务器。我正在使用 Python 3.4,为此我正在尝试使用ldap3配置 LDAP 身份验证。
这是分别从 1.6.2、12.04 和 2.7.3 升级到上述版本的一部分。一切正常,所以我认为问题出在我身上,而不是身份验证服务器。
它的工作方式是我有一个名为authenticate_user.py的文件,它接收通过 HTML 表单传递的用户名和密码,如下所示。
def authenticateStudent(request):
username = request.POST.get('username','')
logger.info("User " + username + " has logged in.")
password = request.POST.get('password','')
x = Auth(username, password)
retVal = x.AuthenticatePy()
logger.info('retVale is '+str(retVal)) #this returns False
#more code and more logging
该方法从 Auth 类实例化一个对象(如下所示),在其中存储用户名和密码,然后调用该类中的 AuthenticatePy() 方法。
import logging
import sys
import os.path,subprocess
import ldap3 as ldap
from ldap3 import Connection, Server, SIMPLE, SYNC, SUBTREE, ALL
logger = logging.getLogger('Submission')
class Auth():
studentName = ""
studentEmail = ""
studentMatrik = ""
def __init__(self, username, password):
self.username = username
self.password = password
def AuthenticatePy(self):
user_dn = "cn="+self.username+",ou=users,ou=data,ou=prod,ou=authserver,dc=domain,dc=tld"
base_dn = "dc=domain,dc=tld"
server = Server("authserver.domain.tld", port=636, use_ssl=True)
filter = "uid="+self.username #might be incorrect
try:
#if authentication successful, get the full user data
connect = Connection(server, user=user_dn, password=self.password)
connect.bind()
logger.info('Connection Bind Complete!') #the last logged message from this method
result = connect.search(search_base=base_dn, search_filter=filter, search_scope=SUBTREE)
logger.info('SEARCHING COMPLETE') #does not appear in the log
# return all user data results
connect.unbind()
uname = result[0][1]['cn'][0]
studentName = result[0][1]['fullName'][0]
studentEmail = result[0][1]['imHauptEMail'][0]
studentMatrik = result[0][1]['imMatrikelNr'][0]
logger.info('studentName is '+str(studentName))
if uname == self.username :
return studentName + '$' + studentEmail + '$' + studentMatrik
else:
return False
except ldap.LDAPExceptionError:
connect.unbind()
return False
我看到的最后一条日志消息是“连接绑定完成!” 而且我不确定发生了什么。知道我做错了什么吗?
编辑:我已经对此进行了一段时间的故障排除,并且我开始认为问题可能出在search_filter
我正在传递搜索功能的参数中。SEARCH 操作的ldap3文档指出过滤器字符串应符合 RFC4515,我不确定是否提供。