1

我正在使用 flask_ldap3_login 通过 Active Directory 的身份验证登录。下面提供代码供参考。

from flask import Flask, url_for
from flask_ldap3_login import LDAP3LoginManager, AuthenticationResponseStatus
from flask_login import LoginManager, login_user, UserMixin, current_user
from flask import render_template_string, redirect
from flask_ldap3_login.forms import LDAPLoginForm

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
app.config['DEBUG'] = True

# Setup LDAP Configuration Variables. Change these to your own settings.
# All configuration directives can be found in the documentation.

# Hostname of your LDAP Server
app.config['LDAP_HOST'] = 'ldap://x.x.x.x'

# Base DN of your directory 
app.config['LDAP_BASE_DN'] = 'OU=City Name,OU=Team Name,OU=Users,OU=Country,OU=Sites,DC=domain,DC=com'
# app.config['LDAP_BASE_DN'] = 'DC=domain,DC=com'

# Users DN to be prepended to the Base DN
# app.config['LDAP_USER_DN'] = 'OU=Users,OU=Country'

# Groups DN to be prepended to the Base DN
# app.config['LDAP_GROUP_DN'] = 'OU=City,OU=Team Name'

# The RDN attribute for your user schema on LDAP
# app.config['LDAP_USER_RDN_ATTR'] = 'cn'

# The Attribute you want users to authenticate to LDAP with.
app.config['LDAP_USER_LOGIN_ATTR'] = 'sAMAccountName'

# The Username to bind to LDAP with
app.config['LDAP_BIND_USER_DN'] = 'CN=Name,OU=city,OU=Team,OU=Users,OU=country,OU=Sites,DC=domain,DC=com'

# The Password to bind to LDAP with
app.config['LDAP_BIND_USER_PASSWORD'] = 'password'

login_manager = LoginManager(app)              # Setup a Flask-Login Manager
ldap_manager = LDAP3LoginManager(app)          # Setup a LDAP3 Login Manager.

@app.route('/manual_login', methods=['POST'])
def manual_login():
      result = app.ldap3_login_manager.authenticate('user`enter code here`', 'password')
    print(result.status)
    return 'fail' if result.status == AuthenticationResponseStatus.fail else 'success'

if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0")

然后问题是,当我使用上面提到的完整app.config['LDAP_BASE_DN']时,它工作正常,但如果我只使用DC=domain,DC=com作为 Base_DN 来验证来自整个组织的用户,那么让我失败而不是成功

4

1 回答 1

3

您需要将范围设置为SUBTREE

app.config['LDAP_USER_SEARCH_SCOPE'] = 'SUBTREE'

默认值LEVEL在 ldap3 中(参见文档- 似乎 flask-ldap3 实现没有提到它):

  • BASE:检索在 search_base 中指定的条目的属性。
  • LEVEL:检索包含在 search_base 中的条目的属性。基础必须引用容器对象。
  • SUBTREE: 检索 search_base 中指定的条目的属性以及向下的所有从属容器。

对于那些即使在正确设置后仍然存在问题的人LDAP_USER_SEARCH_SCOPE,您需要设置LDAP_ALWAYS_SEARCH_BIND为非空值(#undocumented):

app.config['LDAP_ALWAYS_SEARCH_BIND'] = 1

否则 ldap3 会做出“由于用户的 RDN 与登录字段相同,所以可以直接绑定”的假设。仅当用户的RDNLEVEL位于.<user-base-dn>,<base-dn>SUBTREE

于 2019-10-25T14:56:05.083 回答