问题的定义身份验证后端Django的 [英] Problem with custom Authentication Backend for Django

查看:216
本文介绍了问题的定义身份验证后端Django的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的认证后端我通过LDAP认证内置的Active Directory的一个问题。

问题是,它正确地验证从管理登录页面后,并在数据库中创建新的用户(或从LDAP服务器更新其信息),但随后返回我要表示我没有管理员登录页面输入有效的用户名和密码。

考虑到这身份验证并会在Django的数据库/更新用户,我究竟做错了什么?

在code:

 导入LDAP
进口重
从django.conf进口ad_settings
GRPS = re.compile(r'CN =(\\ w +))。的findall高清anyof(short_group_list,ADU):
    all_groups_of_user =集(克在GRPS克adu.get('的memberOf',())GS(GS))
    返回任何(在short_group_list克g若克all_groups_of_user)类ActiveDirectoryBackend(ModelBackend):
    
    这个后端通过LDAP验证利用了ActiveDirectory的服务器
    用户在Django创造他们,如果他们不存在的。
        DEF认证(个体经营,用户名=无,密码=无):
        CON =无
        ldap.set_option(ldap.OPT_REFERRALS,0)
        尝试:
            CON = ldap.initialize('LDAP://%S:%s'的%(ad_settings.AD_DNS_NAME,
                  ad_settings.AD_LDAP_PORT))
            con.simple_bind_s(用户名+@+ ad_settings.AD_DNS_NAME,密码)
            ADUser便有= con.search_ext_s(ad_settings.AD_SEARCH_DN,
                                      ldap.SCOPE_SUBTREE,
                                      sAMAccountName赋=%的%用户名,
                                      ad_settings.AD_SEARCH_FIELDS)[0] [1]
            con.unbind()
        除了ldap.LDAPError:
            返回None
        #用户是否属于相应的广告组?
        如果不是anyof(ad_settings.PROJECT code,ADUser便有):
            返回None        #用户是否已经在Django存在?
        尝试:
            用户= User.objects.get(用户名=用户名)
        除了User.DoesNotExist:
            #创建用户的Django
            用户=用户(用户名=用户名,is_staff = TRUE,is_superuser = FALSE)
        从公元#UPDATE用户信息
        如果ADUser.has_key('给定名称'):
            user.first_name = ADUser.get('给定名称')[0]
        如果ADUser.has_key('SN'):
            user.last_name = ADUser.get('SN')[0]
        如果ADUser.has_key('邮件'):
            user.email = ADUser.get('邮件')[0​​]        #不保存密码在Django。
        user.set_unusable_password()
        user.save()
        回报用户

编辑:想通。用户无法登录,除非他们是积极的(即使文档不说)。因此,在给定code,创建新用户的行应该是这样的:

 用户=用户(用户名=用户名,is_staff = TRUE,IS_ACTIVE = TRUE,
                    is_superuser =假)


解决方案

想通了。用户无法登录,除非他们是积极的(即使文档不说)。因此,在给定code,创建新用户的行应该是这样的:

 用户=用户(用户名=用户名,is_staff = TRUE,IS_ACTIVE = TRUE,
                is_superuser =假)

I'm having a problem with a custom Authentication Backend I've built for an Active Directory via LDAP authentication.

The problem is that from the admin login page, after it properly authenticates and creates the new user in the database (or updates their info from the LDAP server), but then returns me to the admin login page indicating that I failed to enter a valid username and password.

Considering it authenticates and creates/updates the user in the django database, what am I doing wrong?

The code:

import ldap
import re
from django.conf import ad_settings
grps = re.compile(r'CN=(\w+)').findall

def anyof(short_group_list, adu):
    all_groups_of_user = set(g for gs in adu.get('memberOf',()) for g in grps(gs))
    return any(g for g in short_group_list if g in all_groups_of_user)

class ActiveDirectoryBackend(ModelBackend):
    """
    This backend utilizes an ActiveDirectory server via LDAP to authenticate
    users, creating them in Django if they don't already exist.
    """

    def authenticate(self, username=None, password=None):
        con = None
        ldap.set_option(ldap.OPT_REFERRALS, 0)
        try:
            con = ldap.initialize('ldap://%s:%s' % (ad_settings.AD_DNS_NAME,
                  ad_settings.AD_LDAP_PORT))
            con.simple_bind_s(username+"@"+ad_settings.AD_DNS_NAME, password)
            ADUser = con.search_ext_s(ad_settings.AD_SEARCH_DN,
                                      ldap.SCOPE_SUBTREE,
                                      "sAMAccountName=%s" % username,
                                      ad_settings.AD_SEARCH_FIELDS)[0][1]
            con.unbind()
        except ldap.LDAPError:
            return None
        # Does user belong to appropriate AD group?
        if not anyof(ad_settings.PROJECTCODE,ADUser):
            return None

        # Does user already exist in Django?
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            #create Django user
            user = User(username=username, is_staff = True, is_superuser = False)
        #Update User info from AD
        if ADUser.has_key('givenName'):
            user.first_name = ADUser.get('givenName')[0]
        if ADUser.has_key('sn'):
            user.last_name = ADUser.get('sn')[0]
        if ADUser.has_key('mail'):
            user.email = ADUser.get('mail')[0]

        # Does not store password in Django.
        user.set_unusable_password()
        user.save()
        return user

EDIT: Figured out. Users cannot log in unless they are active (even though the documentation does not say that). Therefore, in the code given, the line that creates the new user should look like:

        user = User(username=username, is_staff = True, is_Active = True, 
                    is_superuser = False)

解决方案

Figured out. Users cannot log in unless they are active (even though the documentation does not say that). Therefore, in the code given, the line that creates the new user should look like:

    user = User(username=username, is_staff = True, is_Active = True, 
                is_superuser = False)

这篇关于问题的定义身份验证后端Django的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆