ASKBOT / askbot-devel

Askbot is a Django/Python Q&A forum. **Contributors README**: https://github.com/ASKBOT/askbot-devel#how-to-contribute. Commercial hosting of Askbot and support are available at https://askbot.com
Other
1.56k stars 628 forks source link

LDAP login doesn't works if a master login or passwd isn't provided. #768

Open kas0109 opened 6 years ago

kas0109 commented 6 years ago

Hi ! Not sure if this is a new issue but If someone is using LDAP without a master login/passwd then this line tries to search the active directory before even binding https://github.com/ASKBOT/askbot-devel/blob/master/askbot/deps/django_authopenid/ldap_auth.py#L123.

The binding is done later at https://github.com/ASKBOT/askbot-devel/blob/master/askbot/deps/django_authopenid/ldap_auth.py#L132 and thus the previous line results in an exception since we cannot search LDAP dir without binding (https://www.python-ldap.org/en/latest/reference/ldap.html#ldap.LDAPObject.simple_bind_s ).

So i made a small change, i.e bind before you search and then was able to successfully connect to the server and retrieve search results.

   user_dn = login_template % username
    ldap_session.simple_bind_s(user_dn,password.encode(encoding))

    # search ldap directory for user
    user_search_result = ldap_session.search_s(
        askbot_settings.LDAP_BASE_DN.encode(encoding),
        ldap.SCOPE_SUBTREE,
        user_filter.encode(encoding),
        get_attrs
    )
    if user_search_result: # User found in LDAP Directory
        user_dn = user_search_result[0][0]
        user_information = user_search_result[0][1]
        # ldap_session.simple_bind_s(user_dn, password.encode(encoding)) #raises INVALID_CREDENTIALS
        ldap_session.unbind_s()

After this, the default function to create a user takes in two arguments https://github.com/ASKBOT/askbot-devel/blob/master/askbot/deps/django_authopenid/ldap_auth.py#L173. Whereas when we are calling this from https://github.com/ASKBOT/askbot-devel/blob/master/askbot/deps/django_authopenid/views.py#L520 we just pass one which results in an exception again.

Am i missing something important in all of this ( some conf ) or it's a bug ?

kas0109 commented 6 years ago

Also this https://github.com/ASKBOT/askbot-devel/blob/master/askbot/deps/django_authopenid/ldap_auth.py#L89, results in following error:

File "/usr/local/lib/python2.7/site-packages/askbot-0.10.2-py2.7.egg/askbot/deps/django_authopenid/ldap_auth.py", line 75, in ldap_authenticate_default for key, value in options: ValueError: too many values to unpack

Should be options.items()