0
votes

I am trying to find a way to retrieve information such as 'mail', 'displayName', 'telephoneNumber' from an LDAP authenticated/logged in user in Flask.

My user can successfully authenticate and log in using LDAP. However how would I be able to get additional information about the user?

I am using standard python-ldap and flask-login modules and I'm trying to create a string variable for these attributes so I can call on them later in the session. When trying to run the query I am getting ldap.FILTER_ERROR: {'desc': u'Bad search filter'} . Any ideas on how to get this working?

class UserInfo():
    def whoami(username, password):
        conn = get_ldap_connection()
        conn.simple_bind_s(username,password)
        basedn = 'OU=...,OU=...,DC=...,DC=...'
        userfilter = "(|(SamAccountName=\*" + username + "\*)"
        userattribute = ['displayName']
        userresults = conn.search_s(basedn,ldap.SCOPE_SUBTREE,userfilter, userattribute)

userinfos = UserInfo()

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100))

    def __init__(self, username, password):
        self.username = username

    @staticmethod
    def try_login(username, password):
        conn = get_ldap_connection()
        conn.simple_bind_s(domain+username, password)

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.id)

@login_manager.user_loader
def load_user(id):
    return User.query.get(int(id))

@app.before_request
def get_current_user():
    g.user = current_user
1
Once the LDAP login is complete you need use the Flask Login login_user util to pass a User object with the details of the user. Checkout the login example to learn how to do it.kiran.koduru
Thanks, I do have login_user setup though. It passes the username right now, I'm trying to find a way to pass other attributes from LDAP.Infinity8
So you are trying to say you are facing an error near conn.search_s ?kiran.koduru
yes essentially the search needs to search for the currently logged in user and get information from there. Right now no search works except if I use userfilter = '(objectclass=person)' but this gives me the results of the entire LDAP.Infinity8
Can you refer us to the LDAP package that you are using?kiran.koduru

1 Answers

0
votes

I needed to use ldap.filter.filter_format for proper character escaping.

import ldap.filter

criteria= ldap.filter.filter_format('(&(objectClass=user)(sAMAccountName=%s))', [username])