【问题标题】:Python, Django LDAP: detecting Authentication failed reasoningPython,Django LDAP:检测身份验证失败推理
【发布时间】:2017-10-17 10:42:22
【问题描述】:

我正在开发一个将 LDAP 身份验证集成到现有 Django 应用程序中的项目。使用这个站点和其他站点,我终于能够使用 django_auth_ldap 后端正确配置所有内容。其中:

AUTH_LDAP_REQUIRE_GROUP = "CN=myGroup,CN=groups [...] " 因此只有“myGroup”组中的用户才能登录。

现在 settings.py 中的所有内容都已正确配置,而在 user_login 视图中只有:

...
user = authenticate(username=username, password=password)
if user:
    if user.is_active:
        login(request, user)
        return redirect('index')
    else:
        message = "Your account is disabled."
else:
    message = "Invalid username or password supplied."
...

现在最后一步必须通知用户登录失败的原因。现在,失败消息将始终是:“提供的用户名或密码无效。” 这应该是:
- 用户名/密码错误
- 不在正确的组中

类似:

if user:
...
else: 
    if (LDAP auth failed reason == user does not satisfy AUTH_LDAP_REQUIRE_GROUP):
       message = "You are not in the right user group."
    else:
       message = "Invalid username or password supplied."
...


如何在我的 user_login 视图中知道 LDAP 身份验证失败的原因?


P.S.:在 django_auth_ldap 日志中,我确实看到“username 的调试验证失败:用户不满足 AUTH_LDAP_REQUIRE_GROUP”
但是如何在 user_login 视图中知道这一点?

【问题讨论】:

    标签: python django ldap django-auth-ldap


    【解决方案1】:

    好的,回答我自己的问题。现在我刚刚从我的配置中删除了AUTH_LDAP_REQUIRE_GROUP = "CN=myGroup,CN=groups [...] ",并将以下内容添加到views.py:

    from django_auth_ldap.backend import populate_user
    def populate_user_callback(sender, **kwargs):
        global isLdapUser;
        global isInRightGroup;
    
        isLdapUser = True;
        if "myGroup" in kwargs["ldap_user"].group_names:
            isInRightGroup = True;
    
    populate_user.connect(populate_user_callback)   
    

    并且在user_login中:

        isLdapUser = False;
        isInRightGroup = False;
    
        user = authenticate(username=username, password=password)
    
        if (isLdapUser and not isInRightGroup):
            user = None
            message = "User is not in the right AD group."
            ...
            return ...
    

    authenticate() 如果使用 ldap 后端,将调用 populate_user_callback() 函数。所以我只是自己检查正确的组。


    可能对此有一个更清洁/更好的答案,但这目前正在工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 2020-04-14
      • 2015-04-19
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多