简体   繁体   English

Django 1.5和Python 3中的LDAP

[英]LDAP in Django 1.5 & Python 3

I am using Django 1.5 and Python 3. I am new to Django & LDAP in general. 我正在使用Django 1.5和Python3。一般来说,我是Django&LDAP的新手。

I am including the following configuration (modified for my case) in my settings.py file, which was taken from here : 我在settings.py文件中包含以下配置(针对我的情况进行了修改),该配置来自此处

AUTH_LDAP_SERVER_URI = "ldap://example.fr"

AUTH_LDAP_BIND_DN = 'cn=a_user,dc=example,dc=fr'
AUTH_LDAP_BIND_PASSWORD=''
AUTH_LDAP_USER_SEARCH = LDAPSearch('ou=users,dc=example,dc=fr', ldap.SCOPE_SUBTREE, '(uid=%(user)s)')
AUTH_LDAP_GROUP_SEARCH = LDAPSearch('ou=groups,dc=example,dc=fr', ldap.SCOPE_SUBTREE, '(objectClass=groupOfNames)')

AUTH_LDAP_GROUP_TYPE = ActiveDirectoryGroupType()

#Populate the Django user from the LDAP directory
AUTH_LDAP_USER_ATTR_MAP = {
    'first_name': 'sAMAccountName',
    'last_name': 'displayName',
    'email': 'mail'
}


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

However, I have also followed the discussion here where it says that, the django-auth-ldap module is not ported to Python 3. 但是,我也按照此处的讨论进行了讨论,该讨论说django-auth-ldap模块未移植到Python 3。

I have a couple of questions: 我有一些问题:

  1. What are the module imports that need to go into the settings.py file in order to get the configuration above to work? 为了使上述配置生效,需要将哪些模块导入到settings.py文件中?

  2. Which of those imports are not available for Python 3? 其中哪些导入不适用于Python 3?

  3. Is it possible to get LDAP-based authentication going for Django with Python 3? 是否可以将基于LDAP的身份验证用于Django与Python 3? Is it some modification of the approach above, or something different? 它是对上述方法的某种修改,还是有所不同?

Any advice appreciated. 任何建议表示赞赏。

A late follow up, but a better option for Python 3 may now be available. 后续工作较晚,但现在可能会提供Python 3更好的选择。 I have started using this Django app for my needs: 我已经开始使用此Django应用来满足我的需求:

https://github.com/etianen/django-python3-ldap https://github.com/etianen/django-python3-ldap

It runs great with Python 3 and the package ldap3. 它可以在Python 3和ldap3软件包中很好地运行。 I've only tested against OpenLDAP so far. 到目前为止,我仅针对OpenLDAP进行了测试。

It looks like you got your answer through the linked discussion, but I'll post it here to help anyone who comes across this question. 看起来您通过链接的讨论获得了答案,但我将在此处将其发布,以帮助遇到此问题的任何人。

python-ldap does not support Python3 yet, but there is a fork which does: python-ldap目前还不支持Python3,但是有一个fork可以:

pip install git+https://github.com/rbarrois/python-ldap.git@py3

It looks like the changes have been accepted upstream, but it was easier to follow the instructions at django-auth-ldap to install that fork than to figure out what version at pypi would have the changes. 看起来上游已经接受了更改,但是遵循django-auth-ldap上的指示来安装该fork比确定pypi上的哪个版本具有更改要容易得多。

You also need to install django-auth-ldap: 您还需要安装django-auth-ldap:

pip install django-auth-ldap

At this point, most of your code should work with two imports: 在这一点上,您的大多数代码应与两个导入一起使用:

import ldap
from django_auth_ldap.config import LDAPSearch

You will probably also need to import the ActiveDirectoryGroupType from somewhere, but I haven't ever used that. 您可能还需要从某个位置导入ActiveDirectoryGroupType,但我从未使用过。

Based on the discussion, python-ldap is an old and fragile code base. 根据讨论,python-ldap是一个古老而脆弱的代码库。 I have been very happy with python3-ldap, and hope that django-auth-ldap will move to that module in the future. 我对python3-ldap感到非常满意,并希望django-auth-ldap将来会转移到该模块。

http://pythonhosted.org/python3-ldap/ http://pythonhosted.org/python3-ldap/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM