2

I'm new to Flask and I'm trying out Flask-LDAP3-Login.

I've followed the documentation here and i have it working which is great: https://flask-ldap3-login.readthedocs.io/en/latest/index.html

How would i go about authenticating a user based on whether they are a member of a specific group? I see the docs mention group filtering but i'm not sure how to put it all together.

4

2 回答 2

2

如果有人好奇,我自己解决了这个问题:

首先,我使用此处的步骤将 flask-ldap3-login 与 Flask-SQLAlchemy 集成 - https://github.com/nickw444/flask-ldap3-login/issues/26

我的保存用户方法现在如下所示:

@ldap_manager.save_user
def save_user(dn, username, data, memberships):
    id=int(data.get("uidNumber"))
    if 'group-goes-here' in data.get("memberOf"):
        user=User.query.filter_by(id=id).first()
        if not user:
            user=User(
                id=int(id),
                dn=dn,
                username=username,
                email=data['mail'],
                firstname=data['givenName'],
                lastname=data['sn']
            )
            db.session.add(user)
            db.session.commit()

        return user

因此,基本上只要用户输入有效的 LDAP 凭据,它就会转到 AD 以检索他们的组成员身份,并且它是一个简单的 if 'group-goes-here' in data.get("memberOf"): 确定是否将用户保存在我的用户模型中并将其返回给处理程序。

@auth.route('/login', methods=['GET', 'POST'])
def login():
    # Redirect users who are not logged in.
    form = LDAPLoginForm()
    if form.validate_on_submit():
        if form.user:
            login_user(form.user)
        else:
            flash('Login Failed', 'warning')
            return redirect(url_for('auth.login'))
        return redirect(url_for('main.home'))

希望这可以帮助!

于 2019-02-02T08:30:08.823 回答
-1

你有 ldap 服务器吗?如果不去https://www.openldap.org/并按照有关如何安装 openldap 服务器的说明进行操作,如果您更喜欢 docker 容器,请转到此处 https://github.com/osixia/docker-openldap 并按照步骤获取容器启动并运行然后去这里https://ldap3.readthedocs.io/tutorial.html

pip install ldap3 在有你的 python env 或另一个 python 容器的机器上(与你的 ldap 容器相同的桥接网络)

打开 python 控制台并输入以下命令

>>> from ldap3 import Server, Connection, ALL
>>> server = Server('ipa.demo1.freeipa.org')
>>> conn = Connection(server)
>>> conn.bind()
True 

首先使用这个免费的 ldap 服务器 ipa.demo1.freeipa.org,然后使用您自己的 ldap 服务器 IP 地址

于 2018-11-16T17:22:14.137 回答