简体   繁体   中英

Flask ldap3 issue with login

I am using Ldap3 with Flask python.
I am getting the error when I tried to login:

TypeError: Object of type 'Attribute' is not JSON serializable

Here is the code:

def ADlogin(username,password):
    server = ldap3.Server(server_uri, get_info=ldap3.ALL)
    connection = ldap3.Connection(server, user=username+base, password=password)
    if not connection.bind():
        return False
    else:
        connection.search(username+base, '(objectClass=*)', attributes=ldap3.ALL_ATTRIBUTES)
        session['username'] = username
        session['uid'] = connection.entries[0].uid
        connection.search('dc=example,dc=com', '(objectclass=posixGroup)',
                          attributes=[ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES])
        for entry in connection.entries:
            if entry.cn == 'service':
                for user in entry.memberUid:
                    if user == session['uid']:
                        session['group_service'] = True
                        break
            elif entry.cn == 'management':
                for user in entry.memberUid:
                    if user == session['uid']:
                        session['group_management'] = True
                        break
        return True

and the Login Page:

@app.route("/login", methods=['GET', 'POST'])
def login():
    if request.method == "POST" and request.form['submit'] == "Sign in":
        username = request.form['username']
        password = request.form['password']
        if utils.isEmpty(username) or utils.isEmpty(password):
            return render_template('login.html', status="Please Enter All Fields")

        else:
            status = ADConnect.ADlogin(username,password)
            if status:
                #return redirect(url_for('dashboard'))
                return "Success"
            else:
                return render_template('login.html', status=status)



    else:
        return render_template('login.html')

The code is working fine in python console, but when I trying it with flask it is showing me that error, I am unable to find the solution for the problem.

Find the cause of error:

Here is the line or error : session['uid'] = connection.entries[0].uid

The connection.entries[0].uid is of type ATTRIBUTE and I am trying to set it on session causing the error.

I changes the attribute type to string and it works now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM