【问题标题】:Django 2.1.3 LDAP authentication not authenticating to backendDjango 2.1.3 LDAP 身份验证未对后端进行身份验证
【发布时间】:2018-11-20 06:43:36
【问题描述】:

我正在尝试使用简单的 LDAP 身份验证创建一个 django(v2.1.3) Web 应用程序。代码运行,我不完全知道它为什么不工作。它似乎没有向我连接到的 LDAP 后端验证用户信息。当我填写表格时,当我知道用户在测试服务器上时,它总是会返回“非活动用户”。我想要的只是让它认识到它是一个“有效用户”

我正在针对此处找到的 LDAP 测试服务器运行它http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/

这是我在项目中所做的更改:

settings.py

import ldap
from django_auth_ldap.config import LDAPSearch


AUTH_LDAP_SERVER_URI = "ldap://ldap.forumsys.com:389"
AUTH_LDAP_CONNECTION_OPTIONS = {
    ldap.OPT_REFERRALS: 0
}

AUTH_LDAP_BIND_DN = "cn=read-only-admin,dc=example,dc=com"
AUTH_LDAP_BIND_PASSWORD = "password"
AUTH_LDAP_USER_SEARCH = LDAPSearch(
    "dc=example,dc=com",
    ldap.SCOPE_SUBTREE, "(uid=%(user)s)")

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

AUTHENTICATION_BACKENDS = [
    'django_auth_ldap.backend.LDAPBackend',
]

views.py

from django.contrib.auth import authenticate, login
from django.shortcuts import render    

def login_user(request):

    email = password = ""
    state = ""

    if request.POST:
        email = request.POST.get('email')
        password = request.POST.get('password')

        print (email, password)

        user = authenticate(username=request.POST.get('email'), password=request.POST.get('password'))
        if user is not None:
            login(request, user)
            state = "Valid account"
        else:
            state = "Inactive account"

    return render(request, 'KPI/auth.html', {'state': state, 'email': email})

auth.html

<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        {{state}}
        <form action="" method="post"> {% csrf_token %}
            Email address: <input type="text" name="email" value="{{ email }}" />
            Password: <input type="password" name="password" value="" />
            <input type="submit" value="Log in" />
        </form>
    </body>
</html>

编辑:

我知道设置配置是正确的,因为当我运行 ldapsearch -W -h ldap.forumsys.com -p 389 -D "cn=read-only-admin,dc=example,dc=com" -b "dc=example,dc=com" -s sub "uid=boyle" 时,它只会返回“boyle”信息

编辑2:

当用户返回无用户时,我使用了记录器来获取此错误

Caught LDAPError while authenticating tesla: CONNECT_ERROR({'desc': 'Connect error', 'info': 'error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed (self signed certificate in certificate chain)'},)

【问题讨论】:

  • 我看到您使用的是“电子邮件”,您真的是在传递电子邮件:“boyle@foo.com”还是只是“boyle”?在 Django 中启用调试日志记录可能会有所帮助:django-auth-ldap.readthedocs.io/en/latest/logging.html
  • authenticate(username=request.POST.get('email'), password=request.POST.get('password')) 这会将电子邮件变量设置为我相信的 uid,我会尝试调试它.. 谢谢
  • 发现调试错误,不知道怎么回事,请检查最新编辑。
  • 看起来它正在尝试使用 TLS 连接到 LDAP 服务器。不知道为什么,因为您指定了 ldap://.. 您可以尝试在设置中设置:AUTH_LDAP_START_TLS = False。查看那个站点,他们没有在他们的 ldap 服务器上配置 TLS。
  • 我将它设置为 false 并且它起作用了!谢谢你!编辑您的答案以添加该内容并奖励您赏金。

标签: python django ldap


【解决方案1】:

您似乎没有为服务器使用正确的主机名。

ldap://ldap.forumsys:389 应该是:ldap://ldap.forumsys.com:389

使用 ldapsearch 等 ldap 搜索工具可以帮助验证服务器是否正确响应:

$ ldapsearch -LLL -h ldap.forumsys -p 389 -D 'cn=read-only-admin,dc=example,dc=com' 
-w password -b 'ou=mathematicians,dc=example,dc=com' -s sub "(objectclass=*)"
ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)

$ ldapsearch -LLL -h ldap.forumsys.com -p 389 -D 'cn=read-only- 
admin,dc=example,dc=com' -w password -b 'ou=mathematicians,dc=example,dc=com' 
-s sub "(objectclass=*)"
dn: ou=mathematicians,dc=example,dc=com
uniqueMember: uid=euclid,dc=example,dc=com
uniqueMember: uid=riemann,dc=example,dc=com
uniqueMember: uid=euler,dc=example,dc=com
uniqueMember: uid=gauss,dc=example,dc=com
uniqueMember: uid=test,dc=example,dc=com
ou: mathematicians
cn: Mathematicians
objectClass: groupOfUniqueNames
objectClass: top

如果您取回数据,则意味着它正在寻找用户。结果:0 成功仅表示 ldap 服务器能够成功搜索树。

看起来 Django 模块有两种验证用户的方法。您配置的设置首先尝试查找记录(通过 uid),然后使用找到的 DN 再绑定(再次)提供的密码。例如,它将搜索 (uid=boyle),然后找到 DN:uid=boyle,dc=example,dc=com。然后它将通过登录页面提供的 DN:uid=boyle,dc=example,dc=compassword 绑定到 LDAP 服务器。

针对上述编辑 2:

以下错误表示库正在尝试协商 TLS 连接:

Caught LDAPError while authenticating tesla: CONNECT_ERROR({'desc': 
'Connect error', 'info': 'error:14090086:SSL 
routines:ssl3_get_server_certificate:certificate verify failed (self 
signed certificate in certificate chain)'},)

如果您在端口 389 (ldap://) 上连接到 ldap,则不应协商 TLS,可以通过以下设置禁用:

AUTH_LDAP_START_TLS = False

在 settings.py 中。

出于安全原因,最好使用 ldaps,并通过 AUTH_LDAP_CONNECTION_OPTIONS 字典配置 TLS 选项。

【讨论】:

  • 感谢您的回复,是的,它仍然无法识别任何用户名,我尝试了很多网址,即使 ldapsearch 确实返回,它们似乎也不起作用。我认为这与我的代码有关
  • 当我运行ldapsearch -W -h ldap.forumsys.com -D "cn=read-only-admin,dc=example,dc=com" -b "dc=example,dc=com" -s sub "uid=boyle" 时,它会返回所有的boyle 信息,但会显示# search result search: 2 result: 0 Success result: 0 Success 是否意味着当我进行身份验证时我的用户可以返回为“none”?
  • 您可以使用通过该搜索找到的 DN 来验证您是否可以与其绑定,这就是 Django 在找到条目后将执行的操作。 -D "uid=boyle,dc=example,dc=com" 绑定对我有用,所以看起来它可能是您的代码(或 django 设置)的问题
猜你喜欢
  • 2017-06-03
  • 2011-09-23
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
相关资源
最近更新 更多