我正在使用
django-ldap-auth
针对 LDAP
服务器 (ActiveDirectory
) 对用户进行身份验证。用户能够登录并且每个用户的标志(例如 is_staff)已正确设置。
我还想根据 ldap-users 组将 django-groups 添加到我的 django-users 中。这是我的设置:
import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType, ActiveDirectoryGroupType
AUTH_LDAP_SERVER_URI = "ldap://XXX"
AUTH_LDAP_BIND_AS_AUTHENTICATING_USER = True
AUTH_LDAP_BIND_DN = ""
AUTH_LDAP_BIND_PASSWORD = ""
# I somewhere read that this should help, but it didn't:
#AUTH_LDAP_GLOBAL_OPTIONS = {
# ldap.OPT_REFERRALS: 0
#}
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=Benutzer,ou=Konten,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX",
ldap.SCOPE_SUBTREE, "(cn=%(user)s)")
AUTH_LDAP_USER_DN_TEMPLATE = "CN=%(user)s,OU=Benutzer,OU=Konten,OU=XXX,OU=XXX,DC=XXX,DC=XXX,DC=XXX"
# Set up the basic group parameters.
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("OU=AnwenderRollen,OU=Gruppen,OU=XXX,OU=XXX,DC=XXX,DC=XXX,DC=XXX",
ldap.SCOPE_SUBTREE, "(objectClass=groupOfNames)"
)
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr="CN")
# also tried various possibilities for objectClass and AUTH_LDAP_GROUP_TYPE
#AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr="cn")
#AUTH_LDAP_GROUP_TYPE = ActiveDirectoryGroupType(name_attr="cn")
# Populate the Django user from the LDAP directory.
AUTH_LDAP_USER_ATTR_MAP = {
"first_name": "givenname",
"last_name": "sn",
"email": "mail"
}
AUTH_LDAP_PROFILE_ATTR_MAP = {
#"employee_number": "employeeNumber"
}
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
#"is_active": "cn=active,ou=django,ou=groups,dc=example,dc=com",
"is_staff": "CN=GROUPNAME,OU=AnwenderRollen,OU=Gruppen,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX",
"is_superuser": "CN=GROUPNAME,OU=AnwenderRollen,OU=Gruppen,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX"
}
AUTH_LDAP_PROFILE_FLAGS_BY_GROUP = {
#"is_awesome": "cn=awesome,ou=django,ou=groups,dc=example,dc=com",
}
# This is the default, but I like to be explicit.
AUTH_LDAP_ALWAYS_UPDATE_USER = True
# Use LDAP group membership to calculate group permissions.
AUTH_LDAP_FIND_GROUP_PERMS = True
# Cache group memberships for an hour to minimize LDAP traffic
AUTH_LDAP_CACHE_GROUPS = True
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 1 #3600
# Keep ModelBackend around for per-user permissions and maybe a local
# superuser.
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)
此设置仅部分有效:登录有效,创建 django 用户,从 LDAP (AUTH_LDAP_USER_ATTR_MAP) 获取属性,并使用与 AUTH_LDAP_GROUP_SEARCH 中相同的组路径设置标志 (AUTH_LDAP_USER_FLAGS_BY_GROUP)。但是由于这个错误,这个组搜索确实不工作:
DEBUG Populating Django user USERNAME
DEBUG search_s('CN=USERNAME,OU=Benutzer,OU=Konten,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX', 0, '(objectClass=*)') returned 1 objects: cn=USERNAME,ou=benutzer,ou=konten,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX
DEBUG CN=USERNAME,OU=Benutzer,OU=Konten,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX is a member of cn=GROUPNAME,ou=anwenderrollen,ou=gruppen,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX
DEBUG CN=USERNAME,OU=Benutzer,OU=Konten,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX is a member of cn=GROUPNAME,ou=anwenderrollen,ou=gruppen,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX
DEBUG Django user USERNAMEdoes not have a profile to populate
ERROR search_s('OU=AnwenderRollen,OU=Gruppen,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX', 2, '(&(objectClass=groupOfNames)(member=CN=USERNAME,OU=Benutzer,OU=Konten,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX))') raised OPERATIONS_ERROR({'info': '00000000: LdapErr: DSID-0C090627, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, vece', 'desc': 'Operations error'},)
DEBUG search_s('OU=AnwenderRollen,OU=Gruppen,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX', 2, '(&(objectClass=groupOfNames)(member=CN=USERNAME,OU=Benutzer,ou=XXX,ou=XXX,dc=XXX,dc=XXX,dc=XXX))') returned 0 objects:
由于标志和组搜索的组路径相同,我假设它应该有效。绑定认证用户时搜索群组有问题吗?
我错过了什么?
这是我为使其发挥作用所做的事情:
AUTH_LDAP_BIND_AS_AUTHENTICATING_USER = True
AUTH_LDAP_BIND_DN = "existing_user"
AUTH_LDAP_BIND_PASSWORD = "existing_password"
我将
(objectClass=groupOfNames)
更改为 (objectClass=top)
似乎
django_auth_ldap
使用绑定用户来检查标志(is_staff,...),但不检查组。因此,我向这些变量添加了凭据,这些变量现在用于搜索组。
但是,它有效!
要将组从 ldap 镜像到 django,您可以将
AUTH_LDAP_MIRROR_GROUPS
标志设置为 True
。这些组将在 Groups
下创建,但您必须自行设置权限。这是我的示例配置:
"""
Ldap config. Have to set user as admin in django for successful login.
Using django_auth_ldap module
local settings should be loaded first to import env var settings
"""
import ldap
import os
import logging
from django_auth_ldap.config import LDAPSearch, PosixGroupType, LDAPGroupQuery
# Uncomment to enable verbose logging for ldap
# ldap.set_option(ldap.OPT_DEBUG_LEVEL, 4095)
AUTH_LDAP_SERVER_URI = os.getenv('LDAP_HOSTS', 'ldap://localhost')
logging.warning("LDAP host(s) is {}".format(AUTH_LDAP_SERVER_URI))
# Options
ldap_timeout = float(os.getenv('LDAP_TIMEOUT') or 10.0)
AUTH_LDAP_CONNECTION_OPTIONS = {
ldap.OPT_REFERRALS: 0,
ldap.OPT_NETWORK_TIMEOUT: ldap_timeout,
ldap.OPT_TIMEOUT: ldap_timeout,
}
AUTH_LDAP_START_TLS = False
AUTH_LDAP_USER_DN_TEMPLATE = "cn=%(user)s,ou=employee,dc=mycompany,dc=com"
LDAP_AUTH_USER_LOOKUP_FIELDS = ("username", )
# Populate the Django user from the LDAP directory.
AUTH_LDAP_USER_ATTR_MAP = {
'first_name': 'givenName',
'last_name': 'sn',
'email': 'mail',
}
IS_SUPER_USER_FLAG = (
LDAPGroupQuery("cn=ldap-wheel,ou=groups,dc=mycompany,dc=com") |
LDAPGroupQuery("cn=ldap-admin,ou=groups,dc=mycompany,dc=com")
)
IS_STAFF_FLAG = (
LDAPGroupQuery("cn=ldap-product,ou=groups,dc=mycompany,dc=com")
)
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
'is_active': IS_SUPER_USER_FLAG | IS_STAFF_FLAG,
'is_staff': IS_SUPER_USER_FLAG | IS_STAFF_FLAG,
'is_superuser': IS_SUPER_USER_FLAG,
}
AUTH_LDAP_GROUP_TYPE = PosixGroupType(name_attr='cn')
AUTH_LDAP_GROUP_SEARCH = LDAPSearch(
'ou=groups,dc=mycompany,dc=com',
ldap.SCOPE_SUBTREE,
'(objectClass=posixGroup)',
)
AUTH_LDAP_ALWAYS_UPDATE_USER = True
AUTH_LDAP_FIND_GROUP_PERMS = True
AUTH_LDAP_CACHE_GROUPS = True
# Cache names and group memberships for an hour to minimize LDAP traffic.
AUTH_LDAP_CACHE_TIMEOUT = 3600
AUTH_LDAP_MIRROR_GROUPS = True # Will sync ldap groups to django, if not exist
# AUTH_LDAP_MIRROR_GROUPS_EXCEPT except some groups we don't want to mirror in django
希望有帮助!
为了获取成员的正确组(或组的嵌套成员),您需要设置
AUTH_LDAP_GROUP_TYPE = NestedGroupOfNamesType
。为了正确处理继承,您需要正确的 DN 来查找您正在查看的所有组。
AUTH_LDAP_GROUP_SEARCH = LDAPSearch(
"OU=Gruppen,OU=xxx,DC=xxx,DC=xxx,DC=xx",
ldap.SCOPE_SUBTREE,
"(objectClass=top)",
)
就我而言,我需要按照操作所述将
"(objectClass=top)"
设置为过滤字符串。
如果设置正确,您应该能够找到嵌套属性。