使用 Django 在 LDAP 中创建用户

发布于 2024-11-16 04:02:54 字数 2207 浏览 5 评论 0原文

我在使用 LDAP 身份验证模块 django-auth-ldap 时遇到问题。我正在使用此站点中的示例配置: http://packages.python.org/django- auth-ldap/

我想做两件事:

1)针对 LDAP 进行身份验证: 目前,我的 LDAP 数据库是空的,我没有向其中添加任何内容,事实上我不知道如何添加。但是,我仍然可以使用 django 数据库中存储的旧登录名/密码登录基于 django 的网站。这是为什么?这是否应该被忽略,登录过程是否应该使用 LDAP 用户/密码进行?换句话说,如果我的 LDAP 数据库为空,我的每次登录是否都应该失败?然而,事实并非如此,我的印象是 django 完全忽略了 django-auth-ldap 模块。

2) 将 LDAP 与 django 同步(而不是相反) 我不想使用现有的用户数据库进行身份验证。我希望能够在 Django 中创建新用户并将这些用户传播到 LDAP,以便它们可以由其他服务(在我的例子中为 openfire 服务器)共享。如何使用 django-auth-ldap 做到这一点?

这是我的配置的复制/粘贴:

# Baseline configuration.
AUTH_LDAP_SERVER_URI = "127.0.0.1"

AUTH_LDAP_BIND_DN = "cn=admin,dc=nodomain"
AUTH_LDAP_BIND_PASSWORD = "admin"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=users,dc=nodomain",
    ldap.SCOPE_SUBTREE, "(uid=%(user)s)")

# Set up the basic group parameters.
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=django,ou=groups,dc=nodomain",
    ldap.SCOPE_SUBTREE, "(objectClass=groupOfNames)"
)
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr="cn")

# Only users in this group can log in.
AUTH_LDAP_REQUIRE_GROUP = "cn=enabled,ou=django,ou=groups,dc=nodomain"

# 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=nodomain",
    "is_staff": "cn=staff,ou=django,ou=groups,dc=nodomain",
    "is_superuser": "cn=superuser,ou=django,ou=groups,dc=nodomain"
}

AUTH_LDAP_ALWAYS_UPDATE_USER = True

AUTH_LDAP_FIND_GROUP_PERMS = True

AUTH_LDAP_CACHE_GROUPS = True
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 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',
)

抱歉,我对 LDAP 不太了解,我今天早上刚刚安装了它,所以我的问题可能听起来很天真。我只需要一个集中的用户群,我可以在多个服务器之间更新和共享。

非常感谢您的帮助。

I am having trouble with the LDAP authentification module django-auth-ldap. I am using the example configuration from this site: http://packages.python.org/django-auth-ldap/

I'd like to do two things:

1) Authentificate against LDAP:
For the moment, my LDAP database is empty, I didn't add anything to it, in fact I don't know how to. However, I still am able to log in into my django-based site with my old logins/passwords stored in my django database. Why is that? Shouldn't this be ignored, shouldn't the login process occur with LDAP user/passwords instead? In other words, if my LDAP database is empty, shouldn't every single of my login fail? However, it doesn't, I have the impression that django completly ignores the django-auth-ldap module.

2) Synchronize LDAP with django (and not the other way around)
I don't want to use an existing user database to authentificate against. I want to be able to create new users in Django and propagate these users to LDAP so they can be shared by other services, in my case, an openfire server. How do you do that with django-auth-ldap?

Here is the copy/paste of my configuration:

# Baseline configuration.
AUTH_LDAP_SERVER_URI = "127.0.0.1"

AUTH_LDAP_BIND_DN = "cn=admin,dc=nodomain"
AUTH_LDAP_BIND_PASSWORD = "admin"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=users,dc=nodomain",
    ldap.SCOPE_SUBTREE, "(uid=%(user)s)")

# Set up the basic group parameters.
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=django,ou=groups,dc=nodomain",
    ldap.SCOPE_SUBTREE, "(objectClass=groupOfNames)"
)
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr="cn")

# Only users in this group can log in.
AUTH_LDAP_REQUIRE_GROUP = "cn=enabled,ou=django,ou=groups,dc=nodomain"

# 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=nodomain",
    "is_staff": "cn=staff,ou=django,ou=groups,dc=nodomain",
    "is_superuser": "cn=superuser,ou=django,ou=groups,dc=nodomain"
}

AUTH_LDAP_ALWAYS_UPDATE_USER = True

AUTH_LDAP_FIND_GROUP_PERMS = True

AUTH_LDAP_CACHE_GROUPS = True
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 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',
)

Sorry I don't know much about LDAP, I just installed it this morning so my question may sound naive. I just need a centralized user base that I would be able to update and share between several servers.

Thanks very much for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

跨年 2024-11-23 04:02:55

1) 您的配置安装了两个身份验证后端:

AUTHENTICATION_BACKENDS = ( 'django_auth_ldap.backend.LDAPBackend', 'django.contrib.auth.backends.ModelBackend', )

Django 将尝试对其中的每一个进行身份验证转动,直到找到一个成功的(或直到用完)。由于您的 LDAP 目录是空的,因此它可能总是会失败,因此 ModelBackend 总是会得到尝试。如果您不想根据 Django 用户数据库对用户进行身份验证,则必须从列表中删除 ModelBackend。

2) django-auth-ldap 不会将 Django 用户传播到 LDAP,只有相反的方式。它旨在允许 Django 部署针对单独管理的现有 LDAP 服务进行身份验证。要从 Django 应用程序操作 LDAP 目录的内容,您可能需要查看 django-ldapdb< /a>.

1) Your configuration has two authentication backends installed:

AUTHENTICATION_BACKENDS = ( 'django_auth_ldap.backend.LDAPBackend', 'django.contrib.auth.backends.ModelBackend', )

Django will attempt to authenticate against each one in turn until it finds one that succeeds (or until it runs out). Since your LDAP directory is empty, it will presumably always fail, so ModelBackend will always get a shot. If you don't want to authenticate users against the Django user database, you have to remove ModelBackend from the list.

2) django-auth-ldap doesn't propagate Django users up to LDAP, only the other way around. It's designed to allow Django deployments to authenticate against existing LDAP services that are managed separately. To manipulate the contents of an LDAP directory from a Django app you might want to look at django-ldapdb.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文