跳转到主要内容

Django LDAP认证后端

项目描述

https://readthedocs.org/projects/django-auth-ldap/badge/?version=latest https://img.shields.io/pypi/v/django-auth-ldap.svg https://github.com/django-auth-ldap/django-auth-ldap/workflows/Test/badge.svg https://img.shields.io/pypi/l/django-auth-ldap.svg

这是一个针对LDAP服务的身份验证后端,用于Django。配置可以非常简单,只需一个唯一的名称模板,但提供了许多丰富的配置选项,用于处理用户、组和权限。

安装

使用pip安装此包

$ pip install django-auth-ldap

它需要python-ldap >= 3.1。您需要在系统上安装OpenLDAP库和头文件。

要在Django项目中使用auth后端,请将'django_auth_ldap.backend.LDAPBackend'添加到AUTHENTICATION_BACKENDS。不要将任何内容添加到INSTALLED_APPS

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

LDAPBackend应与自定义用户模型一起使用,但它假定存在数据库。

示例配置

以下是从settings.py中获取的完整示例配置,该配置使用了几乎所有功能。在这个例子中,我们正在对目录中的全局用户池进行身份验证,但我们为Django组保留了一个特殊区域(ou=django,ou=groups,dc=example,dc=com)。请记住,如果您只需要简单的身份验证,那么其中大部分都是可选的。为了完整性,包含了一些默认设置和参数。

import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType


# Baseline configuration.
AUTH_LDAP_SERVER_URI = 'ldap://ldap.example.com'

AUTH_LDAP_BIND_DN = 'cn=django-agent,dc=example,dc=com'
AUTH_LDAP_BIND_PASSWORD = 'phlebotinum'
AUTH_LDAP_USER_SEARCH = LDAPSearch(
    'ou=users,dc=example,dc=com',
    ldap.SCOPE_SUBTREE,
    '(uid=%(user)s)',
)
# Or:
# AUTH_LDAP_USER_DN_TEMPLATE = 'uid=%(user)s,ou=users,dc=example,dc=com'

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

# Simple group restrictions
AUTH_LDAP_REQUIRE_GROUP = 'cn=enabled,ou=django,ou=groups,dc=example,dc=com'
AUTH_LDAP_DENY_GROUP = 'cn=disabled,ou=django,ou=groups,dc=example,dc=com'

# Populate the Django user from the LDAP directory.
AUTH_LDAP_USER_ATTR_MAP = {
    'first_name': 'givenName',
    'last_name': 'sn',
    'email': 'mail',
}

AUTH_LDAP_USER_FLAGS_BY_GROUP = {
    'is_active': 'cn=active,ou=django,ou=groups,dc=example,dc=com',
    'is_staff': 'cn=staff,ou=django,ou=groups,dc=example,dc=com',
    'is_superuser': 'cn=superuser,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 distinguished names and group memberships for an hour to minimize
# LDAP traffic.
AUTH_LDAP_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',
)

贡献

如果您想贡献,最佳做法是发送格式良好的pull request,包括测试和文档。pull request应该专注于单一目标:在单个请求中尝试完成多件事将使处理变得更加困难。

如果您有错误或功能请求,您可以尝试记录问题

创建问题并提交pull request以解决它是无害的。这可以是一种很好的开始对话的方式,也可以作为一个锚点。

项目详情


发布历史 发布通知 | RSS源

下载文件

下载适合您平台的文件。如果您不确定该选择哪个,请了解更多关于安装包的信息。

源代码分发

django-auth-ldap-4.8.0.tar.gz (53.9 kB 查看哈希值)

上传时间 源代码

构建分发

django_auth_ldap-4.8.0-py3-none-any.whl (20.6 kB 查看哈希值)

上传时间 Python 3

由以下支持