Flask ldap3登录和OpenLDAP

时间:2019-04-05 08:26:25

标签: python flask openldap ldap3

登录烧瓶应用程序时,我尝试使用ldap3验证用户。在过去的几天里,我尝试了ldap3中可用的每个LDAP配置设置,但无法正常工作。当我以不存在的用户身份登录时,我已经从openldap获得正确的验证失败返回。但是,如果我使用现有用户,则我还会收到验证失败的返回信息,并结合其他一些错误消息,具体取决于我尝试的配置设置。任何帮助将不胜感激。

class Config(object):
    ...
    LDAP_HOST = 'xxx'
    LDAP_BASE_DN = 'xxx'
    LDAP_PORT = 7389
    LDAP_USER_DN = 'cn=users'
    LDAP_GROUP_DN = 'ou=groups'
    LDAP_USER_RDN_ATTR = 'uid'
    LDAP_USER_LOGIN_ATTR = 'uid'
    LDAP_BIND_USER_DN = 'uid'
    LDAP_BIND_USER_PASSWORD = 'pwd'


class User(UserMixin,db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    dn = db.Column(db.String(128), index=True)

    def __repr__(self):
        return '<User {}>'.format(self.username)


@login_manager.user_loader
def load_user(user_id):
    return User.query.filter_by(id=user_id).first()

@ldap_manager.save_user
def save_user(dn, username, data, memberships):
    user_id=int(data.get("uidNumber"))

    user=User.query.filter_by(id=user_id).first()
    if not user:
        user=User(
            id=int(user_id),
            username=username,
            dn=dn
        )
        db.session.add(user)
        db.session.commit()

    return user

@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LDAPLoginForm()
    if form.validate_on_submit():
        print('Valid')
        login_user(form.user)
        return redirect(url_for('index'))
    else:
        print('Invalid')
    return render_template('login.html', title='Sign In', form=form)

@app.route('/logout')
@login_required
def logout():
    db.session.delete(current_user)
    db.session.commit()
    logout_user()
    return redirect(url_for('login'))

0 个答案:

没有答案