Django LDAP身份验证后端
django-auth-ldap的Python项目详细描述
这是一个django身份验证后端,根据ldap进行身份验证 服务。配置可以简单到一个可分辨名称 模板,但是有许多丰富的配置选项可供用户使用, 组和权限。
- 文档:https://django-auth-ldap.readthedocs.io/
- PYPI:https://pypi.org/project/django-auth-ldap/
- 储存库:https://github.com/django-auth-ldap/django-auth-ldap
- 测试:http://travis-ci.org/django-auth-ldap/django-auth-ldap
- 许可证:BSD 2条款
python 2.7和3.4+以及django 1.11+支持此版本。它需要 python-ldap>;=3.0。
安装
使用pip安装软件包:
$ pip install django-auth-ldap
它需要python-ldap>;=3.0。你需要OpenLDAP库和 系统上有可用的标题。
要在django项目中使用auth后端,请添加 'django_auth_ldap.backend.LDAPBackend'到AUTHENTICATION_BACKENDS。做 不向INSTALLED_APPS添加任何内容。
AUTHENTICATION_BACKENDS=['django_auth_ldap.backend.LDAPBackend',]
LDAPBackend应该适用于自定义用户模型,但它确实假定 数据库存在。
注意
LDAPBackend不是从ModelBackend继承的。有可能 以独占方式使用LDAPBackend,方法是将其配置为绘制组成员身份 从LDAP服务器。但是,如果要将权限分配给 个人用户或将用户添加到django中的组中,您需要 已安装两个后端:
AUTHENTICATION_BACKENDS=['django_auth_ldap.backend.LDAPBackend','django.contrib.auth.backends.ModelBackend',]
示例配置
下面是一个来自settings.py的完整配置示例 几乎所有的特征。在本例中,我们将根据 目录中的全局用户池,但我们为 django组(ou=django,ou=groups,dc=example,dc=com)。记住最重要的 如果您只需要简单的身份验证,则这是可选的。一些违约 为了完整起见,还包括了设置和参数。
importldapfromdjango_auth_ldap.configimportLDAPSearch,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 restrictionsAUTH_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 distinguised 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 请求,包括测试和文档。拉取请求应该是 专注:在一个请求中尝试做多件事会使它更 难以处理。
如果有bug或功能请求,可以尝试logging an issue。
创建问题并将请求提交给 解决它。这是一个开始谈话的好方法,可以作为 锚点。