【问题标题】:django-auth-ldap members groups not wokingdjango-auth-ldap 成员组不工作
【发布时间】:2018-12-17 21:15:50
【问题描述】:

我设法让 ldap 身份验证工作,但用户组没有。当用户通过身份验证时,用户名、名字、电子邮件等会被复制到会话中,但布尔值(从用户所属的组中获取)不会。

这是我的 settings.py :

AUTHENTICATION_BACKENDS = [
    'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
]


import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType, GroupOfUniqueNamesType

AUTH_LDAP_SERVER_URI = "ldap://openldap"
AUTH_LDAP_BIND_DN = "cn=admin,dc=openldap"
AUTH_LDAP_BIND_PASSWORD = "admin"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=django,dc=openldap",
    ldap.SCOPE_SUBTREE, "(cn=%(user)s)")

AUTH_LDAP_USER_ATTR_MAP = {
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail",
}

AUTH_LDAP_CACHE_TIMEOUT = 0
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 0
AUTH_LDAP_PROFILE_ATTR_MAP = {"home_directory": "homeDirectory"}
AUTH_LDAP_MIRROR_GROUPS = True
AUTH_LDAP_FIND_GROUP_PERMS = True
AUTH_LDAP_ALWAYS_UPDATE_USER = True

AUTH_LDAP_GROUP_SEARCH = LDAPSearch("dc=openldap",
    ldap.SCOPE_SUBTREE, "(objectClass=*)"
)
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr='cn')

AUTH_LDAP_USER_FLAGS_BY_GROUP = {
    'is_active': 'cn=active,ou=groups,dc=openldap',
    'is_staff': 'cn=staff,ou=groups,dc=openldap',
    'is_superuser': 'cn=superuser,ou=groups,dc=openldap',
}

# # Simple group restrictions
# AUTH_LDAP_REQUIRE_GROUP = 'cn=enabled,ou=groups,dc=openldap',
# AUTH_LDAP_DENY_GROUP = 'cn=disabled,ou=groups,dc=openldap',


### ERROR LOGGING

import logging
logger = logging.getLogger('django_auth_ldap')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)

这是我的 ldap 方案:

root 是 active、staff 和 superuser 的一部分。
user1 是活动的一部分。
这是我从视图对用户进行身份验证时得到的结果: 我得到错误 -> 不是成员

openldap        | 5b444c1f conn=1015 fd=13 ACCEPT from IP=172.23.0.4:47230 (IP=0.0.0.0:389)
openldap        | 5b444c1f conn=1015 op=0 BIND dn="cn=admin,dc=openldap" method=128
openldap        | 5b444c1f conn=1015 op=0 BIND dn="cn=admin,dc=openldap" mech=SIMPLE ssf=0
openldap        | 5b444c1f conn=1015 op=0 RESULT tag=97 err=0 text=
openldap        | 5b444c1f conn=1015 op=1 SRCH base="ou=django,dc=openldap" scope=2 deref=0 filter="(cn=root)"
openldap        | 5b444c1f <= mdb_equality_candidates: (cn) not indexed
openldap        | 5b444c1f conn=1015 op=1 SEARCH RESULT tag=101 err=0 nentries=1 text=
django          | search_s('ou=django,dc=openldap', 2, '(cn=%(user)s)') returned 1 objects: cn=root,ou=django,dc=openldap
openldap        | 5b444c1f conn=1015 op=2 BIND anonymous mech=implicit ssf=0
openldap        | 5b444c1f conn=1015 op=2 BIND dn="cn=root,ou=django,dc=openldap" method=128
openldap        | 5b444c1f conn=1015 op=2 BIND dn="cn=root,ou=django,dc=openldap" mech=SIMPLE ssf=0
openldap        | 5b444c1f conn=1015 op=2 RESULT tag=97 err=0 text=
django          | Populating Django user root
openldap        | 5b444c1f conn=1015 op=3 BIND anonymous mech=implicit ssf=0
openldap        | 5b444c1f conn=1015 op=3 BIND dn="cn=admin,dc=openldap" method=128
openldap        | 5b444c1f conn=1015 op=3 BIND dn="cn=admin,dc=openldap" mech=SIMPLE ssf=0
openldap        | 5b444c1f conn=1015 op=3 RESULT tag=97 err=0 text=
openldap        | 5b444c1f conn=1015 op=4 CMP dn="cn=active,ou=groups,dc=openldap" attr="member"
openldap        | 5b444c1f conn=1015 op=4 RESULT tag=111 err=16 text=
django          | cn=root,ou=django,dc=openldap is not a member of cn=active,ou=groups,dc=openldap
openldap        | 5b444c1f conn=1015 op=5 CMP dn="cn=staff,ou=groups,dc=openldap" attr="member"
openldap        | 5b444c1f conn=1015 op=5 RESULT tag=111 err=16 text=
openldap        | 5b444c1f conn=1015 op=6 CMP dn="cn=superuser,ou=groups,dc=openldap" attr="member"
openldap        | 5b444c1f conn=1015 op=6 RESULT tag=111 err=16 text=
django          | cn=root,ou=django,dc=openldap is not a member of cn=staff,ou=groups,dc=openldap
django          | cn=root,ou=django,dc=openldap is not a member of cn=superuser,ou=groups,dc=openldap
openldap        | 5b444c1f conn=1015 op=7 SRCH base="dc=openldap" scope=2 deref=0 filter="(&(objectClass=*)(member=cn=root,ou=django,dc=openldap))"
openldap        | 5b444c1f <= mdb_equality_candidates: (member) not indexed
openldap        | 5b444c1f conn=1015 op=7 SEARCH RESULT tag=101 err=0 nentries=0 text=
django          | search_s('dc=openldap', 2, '(&(objectClass=*)(member=cn=root,ou=django,dc=openldap))') returned 0 objects:

PS:用户通过身份验证没有问题,但是当我打印user.is_staffuser.is_activeuser.is_superuser 时,我得到了所有这些False

【问题讨论】:

  • 有人吗?有想法吗?

标签: django ldap openldap ldap-query django-auth-ldap


【解决方案1】:

我知道我迟到了几年才回复,但是当我试图解决我自己的问题时,我偶然发现了你的问题,让 django-auth-ldap 与我的 AD 服务器一起工作。

从您的日志中,我可以看到一些类似这样的消息:

django          | cn=root,ou=django,dc=openldap is not a member of cn=superuser,ou=groups,dc=openldap

为我解决了这个问题的是 here 的以下引用:

如果您的过滤器过于具体,中间的组将 被忽略,导致组列表不完整。

不要让您的过滤器过于具体。反正也无所谓 因为只有当用户属于他们时才会返回组, 甚至是间接的。

虽然具体不适用于您的情况,并且可能与您的情况相反:我认为这是您问题的症结django-auth-ldap 找不到您的群组。我认为您是从 DC 的根开始的。尝试将您的 AUTH_LDAP_GROUP_SEARCH 更改为更具体,如下所示,看看是否有帮助:

AUTH_LDAP_GROUP_SEARCH = LDAPSearch(
    "OU=groups,DC=openldap",
    ldap.SCOPE_SUBTREE,
    "(objectClass=group)",
)

无论如何,我都不是 LDAP / AD 方面的专家,所以 YMMV。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多